github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/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 47 #### Specific to Amazon builders: 48 49 - `clean_ami_name` - AMI names can only contain certain characters. This 50 function will replace illegal characters with a '-" character. Example usage 51 since ":" is not a legal AMI name is: `{{isotime | clean_ami_name}}`. 52 53 ## Template variables 54 55 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. 56 57 ``` liquid 58 { 59 "provisioners": [ 60 { 61 "type": "shell", 62 "execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'", 63 "scripts": [ 64 "scripts/bootstrap.sh" 65 ] 66 } 67 ] 68 } 69 ``` 70 71 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. 72 73 -> **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. 74 75 # isotime Function Format Reference 76 77 Formatting for the function `isotime` uses the magic reference date **Mon Jan 2 78 15:04:05 -0700 MST 2006**, which breaks down to the following: 79 80 <table class="table table-bordered table-condensed"> 81 <thead> 82 <tr> 83 <th> 84 </th> 85 <th align="center"> 86 Day of Week 87 </th> 88 <th align="center"> 89 Month 90 </th> 91 <th align="center"> 92 Date 93 </th> 94 <th align="center"> 95 Hour 96 </th> 97 <th align="center"> 98 Minute 99 </th> 100 <th align="center"> 101 Second 102 </th> 103 <th align="center"> 104 Year 105 </th> 106 <th align="center"> 107 Timezone 108 </th> 109 </tr> 110 </thead> 111 <tr> 112 <th> 113 Numeric 114 </th> 115 <td align="center"> 116 - 117 </td> 118 <td align="center"> 119 01 120 </td> 121 <td align="center"> 122 02 123 </td> 124 <td align="center"> 125 03 (15) 126 </td> 127 <td align="center"> 128 04 129 </td> 130 <td align="center"> 131 05 132 </td> 133 <td align="center"> 134 06 135 </td> 136 <td align="center"> 137 -0700 138 </td> 139 </tr> 140 <tr> 141 <th> 142 Textual 143 </th> 144 <td align="center"> 145 Monday (Mon) 146 </td> 147 <td align="center"> 148 January (Jan) 149 </td> 150 <td align="center"> 151 - 152 </td> 153 <td align="center"> 154 - 155 </td> 156 <td align="center"> 157 - 158 </td> 159 <td align="center"> 160 - 161 </td> 162 <td align="center"> 163 - 164 </td> 165 <td align="center"> 166 MST 167 </td> 168 </tr> 169 </table> 170 *The values in parentheses are the abbreviated, or 24-hour clock values* 171 172 Note that "-0700" is always formatted into "+0000" because `isotime` is always UTC time. 173 174 Here are some example formatted time, using the above format options: 175 176 ``` liquid 177 isotime = June 7, 7:22:43pm 2014 178 179 {{isotime "2006-01-02"}} = 2014-06-07 180 {{isotime "Mon 1504"}} = Sat 1922 181 {{isotime "02-Jan-06 03\_04\_05"}} = 07-Jun-2014 07\_22\_43 182 {{isotime "Hour15Year200603"}} = Hour19Year201407 183 ``` 184 185 Please note that double quote characters need escaping inside of templates (in this case, on the `ami_name` value): 186 187 ``` json 188 { 189 "builders": [ 190 { 191 "type": "amazon-ebs", 192 "access_key": "...", 193 "secret_key": "...", 194 "region": "us-east-1", 195 "source_ami": "ami-fce3c696", 196 "instance_type": "t2.micro", 197 "ssh_username": "ubuntu", 198 "ami_name": "packer {{isotime \"2006-01-02\"}}" 199 } 200 ] 201 } 202 ``` 203 204 -> **Note:** See the [Amazon builder](/docs/builders/amazon.html) documentation for more information on how to correctly configure the Amazon builder in this example.