github.com/hashicorp/packer@v1.14.3/website/content/docs/templates/hcl_templates/contextual-variables.mdx (about) 1 --- 2 page_title: Contextual variables reference 3 description: >- 4 Source variables, build variables, and other special variables are context dependent. Learn about contextual variables that return connection and instance state information. 5 --- 6 7 # Contextual variables 8 9 This topic provides reference information about special variables that return connection and basic instance state information based on the configuration context. 10 11 `@include 'from-1.5/contextual-source-variables.mdx'` 12 13 ## Build Variables 14 15 Build variables will allow you to access connection information and basic instance state information for a builder. 16 All special build variables are stored in the `build` variable: 17 18 ```hcl 19 source "null" "first-example" { 20 communicator = "none" 21 } 22 23 build { 24 name = "my-build-name" 25 sources = ["null.first-example"] 26 27 provisioner "shell-local" { 28 environment_vars = ["TESTVAR=${build.PackerRunUUID}"] 29 inline = ["echo source.name is ${source.name}.", 30 "echo build.name is ${build.name}.", 31 "echo build.PackerRunUUID is $TESTVAR"] 32 } 33 } 34 ``` 35 36 Here is the list of available build variables: 37 38 - **name** Represents the name of the build block being run. This is different 39 than the name of the source block being run. 40 41 - **ID**: Represents the VM being provisioned. For example, in Amazon it is the instance ID; in DigitalOcean, 42 it is the Droplet ID; in VMware, it is the VM name. 43 44 - **Host**, **Port**, **User** and **Password**: The host, port, user, and password that Packer uses to access the machine. 45 Useful for using the shell local provisioner to run Ansible or Inspec against the provisioned instance. 46 47 - **ConnType**: Type of communicator being used. For example, for SSH communicator this will be "ssh". 48 49 - **PackerRunUUID**: Current build's unique ID. Can be used to specify build artifacts. 50 An example of that, is when multiple builds runs at the same time producing the same artifact. 51 It's possible to differentiate these artifacts by naming them with the builds' unique IDs. 52 53 - **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`. 54 55 - **SSHPublicKey** and **SSHPrivateKey**: The public and private key that Packer uses to connect to the instance. 56 These are unique to the SSH communicator and are unset when using other communicators. 57 **SSHPublicKey** and **SSHPrivateKey** can have escape sequences and special characters so their output should be single quoted to avoid surprises. For example: 58 59 ```hcl 60 provisioner "shell" { 61 inline = ["echo '${build.SSHPrivateKey}' > /tmp/packer-session.pem"] 62 } 63 ``` 64 65 For backwards compatibility, `WinRMPassword` is also available through this 66 engine, though it is no different than using the more general `Password`. 67 68 All build variables are valid to use with any of the [HCL2 functions](/packer/docs/templates/hcl_templates/functions). 69 Example of using [upper](/packer/docs/templates/hcl_templates/functions/string/upper) to upper case the build ID: 70 71 ```hcl 72 post-processor "shell-local" { 73 inline = ["echo ${upper(build.ID)}"] 74 } 75 ``` 76 77 For builder-specific builder variables, please also refer to the builder docs: 78 79 - Amazon EC2: [chroot](/packer/plugins/builders/amazon/chroot#build-shared-information-variables), 80 [EBS Volume](/packer/plugins/builders/amazon/ebsvolume#build-shared-information-variables), 81 [EBS](/packer/plugins/builders/amazon/ebs#build-shared-information-variables), 82 [EBS Surrogate](/packer/plugins/builders/amazon/ebssurrogate#build-shared-information-variables), 83 [Instance](/packer/plugins/builders/amazon/instance#build-shared-information-variables). 84 85 The HCL2 Special Build Variables is in beta; please report any issues or requests on the Packer 86 issue tracker on GitHub. 87 88 ## Packer Version 89 90 This variable is set to the Packer version currently running. 91 92 ```hcl 93 source "null" "first-example" { 94 communicator = "none" 95 } 96 97 build { 98 sources = ["null.first-example"] 99 100 provisioner "shell-local" { 101 inline = ["echo packer_version is '${packer.version}'"] 102 } 103 } 104 ``` 105 106 If you are running a development version of Packer, the version variable will 107 contain the released version number, dev flag, and current commit. 108 109 ```shell-session 110 PACKER_LOG=0 packer build packer_version_demo.pkr.hcl 111 null.first-example: output will be in this color. 112 113 ==> null.first-example: Running local shell script: /var/folders/8t/0yb5q0_x6mb2jldqq_vjn3lr0000gn/T/packer-shell083160352 114 null.first-example: packer_version is 1.6.5-dev (a69392129+CHANGES) 115 ``` 116 117 If you are running a released version of Packer, the version variable will 118 contain the released version number only: 119 120 ```shell-session 121 PACKER_LOG=0 packer build packer_version_demo.pkr.hcl 122 null.first-example: output will be in this color. 123 124 ==> null.first-example: Running local shell script: /var/folders/8t/0yb5q0_x6mb2jldqq_vjn3lr0000gn/T/packer-shell718995312 125 null.first-example: packer_version is 1.6.5 126 ``` 127 128 Make sure to wrap your variable in single quotes in order to escape the 129 string that is returned; if you are running a dev version of packer the 130 parenthesis may through off your shell escaping otherwise. 131 132 ## HCP Packer Iteration ID 133 134 ~> **Note**: The `packer.iterationID` variable is now deprecated and will be removed in a future version of Packer. HCP Packer Versions should be accessed with their fingerprint instead. The `packer.versionFingerprint` variable is now exposed to be used in its stead with the new HCP Packer data sources. 135 136 If your build is pushing metadata to the HCP Packer registry, this variable is 137 set to the value of the Iteration ID associated with this run. 138 139 ```hcl 140 source "null" "example" { 141 communicator = "none" 142 } 143 144 data "hcp-packer-version" "hardened-source" { 145 bucket_name = "example" 146 channel_name = "latest" 147 } 148 149 data "hcp-packer-artifact" "file" { 150 bucket_name = "example" 151 version_fingerprint = "${data.hcp-packer-version.hardened-source.fingerprint}" 152 platform = "aws" 153 region = "us-east-1" 154 } 155 156 build { 157 hcp_packer_registry { 158 bucket_name = "simple" 159 } 160 sources = [ 161 "source.null.example" 162 ] 163 164 provisioner "shell-local" { 165 inline = [ 166 "echo data is ${packer.iterationID}" 167 ] 168 } 169 } 170 171 ``` 172 173 ```shell-session 174 ==> mybuild.null.example: Running local shell script: /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-shell842419427 175 mybuild.null.example: data is 01HN3KCRPVKR5PBQ28TS6B12W0 176 ``` 177 178 ## HCP Packer Version Fingerprint 179 180 If your build is pushing metadata to the HCP Packer registry, this variable is 181 set to the value of the Version Fingerprint associated with this run. 182 183 ```hcl 184 source "null" "example" { 185 communicator = "none" 186 } 187 188 data "hcp-packer-version" "hardened-source" { 189 bucket_name = "example" 190 channel_name = "latest" 191 } 192 193 data "hcp-packer-artifact" "file" { 194 bucket_name = "example" 195 version_fingerprint = "${data.hcp-packer-version.hardened-source.fingerprint}" 196 platform = "aws" 197 region = "us-east-1" 198 } 199 200 build { 201 hcp_packer_registry { 202 bucket_name = "simple" 203 } 204 sources = [ 205 "source.null.example" 206 ] 207 208 provisioner "shell-local" { 209 inline = [ 210 "echo data is ${packer.versionFingerprint}" 211 ] 212 } 213 } 214 215 ``` 216 217 ```shell-session 218 ==> mybuild.null.example: Running local shell script: /var/folders/cz/q3cr3tld2457gtlgw7qs1kqc0000gq/T/packer-shell842419427 219 mybuild.null.example: data is 01HN3KCRPVKR5PBQ28TS6B12W0 220 ``` 221 222 You can also add this value to post-processors, for example to add to a manifest file: 223 224 ```hcl 225 post-processor "manifest" { 226 output = "manifest.json" 227 strip_path = true 228 custom_data = { 229 version_fingerprint = "${packer.versionFingerprint}" 230 iteration = "${packer.iterationID}" 231 } 232 } 233 234 ```