github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/website/source/docs/templates/engine.html.md (about) 1 --- 2 description: | 3 All strings within templates are processed by a common Packer templating 4 engine, where variables and functions can be used to modify the value of a 5 configuration parameter at runtime. 6 layout: docs 7 page_title: 'Template Engine - Templates' 8 sidebar_current: 'docs-templates-engine' 9 --- 10 11 # Template Engine 12 13 All strings within templates are processed by a common Packer templating engine, 14 where variables and functions can be used to modify the value of a 15 configuration parameter at runtime. 16 17 The syntax of templates uses the following conventions: 18 19 - Anything template related happens within double-braces: `{{ }}`. 20 - Functions are specified directly within the braces, such as `{{timestamp}}`. 21 - Template variables are prefixed with a period and capitalized, such as 22 `{{.Variable}}`. 23 24 ## Functions 25 26 Functions perform operations on and within strings, for example the `{{timestamp}}` function can be used in any string to generate 27 the current timestamp. This is useful for configurations that require unique 28 keys, such as AMI names. By setting the AMI name to something like `My Packer AMI {{timestamp}}`, the AMI name will be unique down to the second. If you 29 need greater than one second granularity, you should use `{{uuid}}`, for 30 example when you have multiple builders in the same template. 31 32 Here is a full list of the available functions for reference. 33 34 - `build_name` - The name of the build being run. 35 - `build_type` - The type of the builder being used currently. 36 - `isotime [FORMAT]` - UTC time, which can be 37 [formatted](https://golang.org/pkg/time/#example_Time_Format). See more 38 examples below in [the `isotime` format reference](/docs/templates/engine.html#isotime-function-format-reference). 39 - `lower` - Lowercases the string. 40 - `pwd` - The working directory while executing Packer. 41 - `template_dir` - The directory to the template for the build. 42 - `timestamp` - The current Unix timestamp in UTC. 43 - `uuid` - Returns a random UUID. 44 - `upper` - Uppercases the string. 45 - `user` - Specifies a user variable. 46 - `packer_version` - Returns Packer version. 47 48 #### Specific to Amazon builders: 49 50 - `clean_ami_name` - AMI names can only contain certain characters. This 51 function will replace illegal characters with a '-" character. Example usage 52 since ":" is not a legal AMI name is: `{{isotime | clean_ami_name}}`. 53 54 #### Specific to Google Compute builders: 55 56 - `clean_image_name` - GCE image names can only contain certain characters and 57 the maximum length is 63. This function will convert upper cases to lower cases 58 and replace illegal characters with a "-" character. 59 Example: 60 61 `"mybuild-{{isotime | clean_image_name}}"` 62 will become 63 `mybuild-2017-10-18t02-06-30z`. 64 65 Note: Valid GCE image names must match the regex 66 `(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)` 67 68 This engine does not guarantee that the final image name will match the 69 regex; it will not truncate your name if it exceeds 63 characters, and it 70 will not validate that the beginning and end of the engine's output are 71 valid. For example, 72 `"image_name": {{isotime | clean_image_name}}"` will cause your build to 73 fail because the image name will start with a number, which is why in the 74 above example we prepend the isotime with "mybuild". 75 76 ## Template variables 77 78 Template variables are special variables automatically set by Packer at build time. Some builders, provisioners and other components have template variables that are available only for that component. Template variables are recognizable because they're prefixed by a period, such as `{{ .Name }}`. For example, when using the [`shell`](/docs/builders/vmware-iso.html) builder template variables are available to customize the [`execute_command`](/docs/provisioners/shell.html#execute_command) parameter used to determine how Packer will run the shell command. 79 80 ``` liquid 81 { 82 "provisioners": [ 83 { 84 "type": "shell", 85 "execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'", 86 "scripts": [ 87 "scripts/bootstrap.sh" 88 ] 89 } 90 ] 91 } 92 ``` 93 94 The `{{ .Vars }}` and `{{ .Path }}` template variables will be replaced with the list of the environment variables and the path to the script to be executed respectively. 95 96 -> **Note:** In addition to template variables, you can specify your own user variables. See the [user variable](/docs/templates/user-variables.html) documentation for more information on user variables. 97 98 # isotime Function Format Reference 99 100 Formatting for the function `isotime` uses the magic reference date **Mon Jan 2 101 15:04:05 -0700 MST 2006**, which breaks down to the following: 102 103 <table class="table table-bordered table-condensed"> 104 <thead> 105 <tr> 106 <th> 107 </th> 108 <th align="center"> 109 Day of Week 110 </th> 111 <th align="center"> 112 Month 113 </th> 114 <th align="center"> 115 Date 116 </th> 117 <th align="center"> 118 Hour 119 </th> 120 <th align="center"> 121 Minute 122 </th> 123 <th align="center"> 124 Second 125 </th> 126 <th align="center"> 127 Year 128 </th> 129 <th align="center"> 130 Timezone 131 </th> 132 </tr> 133 </thead> 134 <tr> 135 <th> 136 Numeric 137 </th> 138 <td align="center"> 139 - 140 </td> 141 <td align="center"> 142 01 143 </td> 144 <td align="center"> 145 02 146 </td> 147 <td align="center"> 148 03 (15) 149 </td> 150 <td align="center"> 151 04 152 </td> 153 <td align="center"> 154 05 155 </td> 156 <td align="center"> 157 06 158 </td> 159 <td align="center"> 160 -0700 161 </td> 162 </tr> 163 <tr> 164 <th> 165 Textual 166 </th> 167 <td align="center"> 168 Monday (Mon) 169 </td> 170 <td align="center"> 171 January (Jan) 172 </td> 173 <td align="center"> 174 - 175 </td> 176 <td align="center"> 177 - 178 </td> 179 <td align="center"> 180 - 181 </td> 182 <td align="center"> 183 - 184 </td> 185 <td align="center"> 186 - 187 </td> 188 <td align="center"> 189 MST 190 </td> 191 </tr> 192 </table> 193 *The values in parentheses are the abbreviated, or 24-hour clock values* 194 195 Note that "-0700" is always formatted into "+0000" because `isotime` is always UTC time. 196 197 Here are some example formatted time, using the above format options: 198 199 ``` liquid 200 isotime = June 7, 7:22:43pm 2014 201 202 {{isotime "2006-01-02"}} = 2014-06-07 203 {{isotime "Mon 1504"}} = Sat 1922 204 {{isotime "02-Jan-06 03\_04\_05"}} = 07-Jun-2014 07\_22\_43 205 {{isotime "Hour15Year200603"}} = Hour19Year201407 206 ``` 207 208 Please note that double quote characters need escaping inside of templates (in this case, on the `ami_name` value): 209 210 ``` json 211 { 212 "builders": [ 213 { 214 "type": "amazon-ebs", 215 "access_key": "...", 216 "secret_key": "...", 217 "region": "us-east-1", 218 "source_ami": "ami-fce3c696", 219 "instance_type": "t2.micro", 220 "ssh_username": "ubuntu", 221 "ami_name": "packer {{isotime \"2006-01-02\"}}" 222 } 223 ] 224 } 225 ``` 226 227 -> **Note:** See the [Amazon builder](/docs/builders/amazon.html) documentation for more information on how to correctly configure the Amazon builder in this example.