packer-cn/website/pages/docs/from-1.5/contextual-variables.mdx

72 lines
3.2 KiB
Plaintext

---
layout: docs
page_title: Contextual Variables - HCL Configuration Language
sidebar_title: Contextual Variables
description: |-
Special variables provide connection information and basic instance state information.
This page covers all existing special variables.
---
# Build Variables
`@include 'from-1.5/beta-hcl2-note.mdx'`
Build variables will allow you to access connection information and basic instance state information for a builder.
All special build variables are stored in the `build` variable:
```hcl
provisioner "shell" {
environment_vars = ["TESTVAR=${build.PackerRunUUID}"]
inline = ["echo $TESTVAR"]
}
```
Here is the list of available build variables:
- **ID**: Represents the vm being provisioned. For example, in Amazon it is the instance id; in digitalocean,
it is the droplet id; in Vmware, it is the vm name.
- **Host**, **Port**, **User** and **Password**: The host, port, user, and password that Packer uses to access the machine.
Useful for using the shell local provisioner to run Ansible or Inspec against the provisioned instance.
- **ConnType**: Type of communicator being used. For example, for SSH communicator this will be "ssh".
- **PackerRunUUID**: Current build's unique id. Can be used to specify build artifacts.
An example of that, is when multiple builds runs at the same time producing the same artifact.
It's possible to differentiate these artifacts by naming them with the builds' unique ids.
- **PackerHTTPIP**, **PackerHTTPPort**, and **PackerHTTPAddr**: HTTP IP, port, and address of the file server Packer creates to serve items in the "http" dir to the vm. The HTTP address is displayed in the format `IP:PORT`.
- **SSHPublicKey** and **SSHPrivateKey**: The public and private key that Packer uses to connect to the instance.
These are unique to the SSH communicator and are unset when using other communicators.
**SSHPublicKey** and **SSHPrivateKey** can have escape sequences and special characters so their output should be single quoted to avoid surprises. For example:
```hcl
provisioner "shell" {
inline = ["echo '${build.SSHPrivateKey}' > /tmp/packer-session.pem"]
}
```
For backwards compatibility, `WinRMPassword` is also available through this
engine, though it is no different than using the more general `Password`.
All build variables are valid to use with any of the [HCL2 functions](/docs/from-1.5/functions).
Example of using [upper](/docs/from-1.5/functions/string/upper) to upper case the build ID:
```hcl
post-processor "shell-local" {
inline = ["echo ${upper(build.ID)}"]
}
```
For builder-specific builder variables, please also refer to the builder docs:
- Amazon EC2: [chroot](/docs/builders/amazon/chroot#build-shared-information-variables),
[EBS Volume](/docs/builders/amazon/ebsvolume#build-shared-information-variables),
[EBS](/docs/builders/amazon/ebs#build-shared-information-variables),
[EBS Surrogate](/docs/builders/amazon/ebssurrogate#build-shared-information-variables),
[Instance](/docs/builders/amazon/instance#build-shared-information-variables).
The HCL2 Special Build Variables is in beta; please report any issues or requests on the Packer
issue tracker on GitHub.