2013-06-10 18:08:14 -04:00
|
|
|
---
|
2017-06-14 21:04:16 -04:00
|
|
|
description: |
|
|
|
|
All strings within templates are processed by a common Packer templating
|
|
|
|
engine, where variables and functions can be used to modify the value of a
|
|
|
|
configuration parameter at runtime.
|
2015-07-22 22:31:00 -04:00
|
|
|
layout: docs
|
2017-06-14 21:04:16 -04:00
|
|
|
page_title: 'Template Engine - Templates'
|
|
|
|
sidebar_current: 'docs-templates-engine'
|
2017-03-25 18:13:52 -04:00
|
|
|
---
|
2013-06-10 18:08:14 -04:00
|
|
|
|
2017-03-25 18:13:52 -04:00
|
|
|
# Template Engine
|
2013-06-10 18:08:14 -04:00
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
All strings within templates are processed by a common Packer templating
|
|
|
|
engine, where variables and functions can be used to modify the value of a
|
2017-05-15 16:59:12 -04:00
|
|
|
configuration parameter at runtime.
|
2013-06-10 18:08:14 -04:00
|
|
|
|
2017-05-09 14:37:49 -04:00
|
|
|
The syntax of templates uses the following conventions:
|
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
- Anything template related happens within double-braces: `{{ }}`.
|
2018-10-26 20:02:51 -04:00
|
|
|
- Functions are specified directly within the braces, such as
|
|
|
|
`{{timestamp}}`.
|
2017-06-14 21:04:16 -04:00
|
|
|
- Template variables are prefixed with a period and capitalized, such as
|
|
|
|
`{{.Variable}}`.
|
2013-08-08 19:56:09 -04:00
|
|
|
|
2017-05-15 16:59:12 -04:00
|
|
|
## Functions
|
2013-06-10 18:08:14 -04:00
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
Functions perform operations on and within strings, for example the
|
|
|
|
`{{timestamp}}` function can be used in any string to generate the current
|
|
|
|
timestamp. This is useful for configurations that require unique 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 need greater than one second granularity, you should use `{{uuid}}`, for
|
2017-05-15 16:59:12 -04:00
|
|
|
example when you have multiple builders in the same template.
|
2013-08-08 19:56:09 -04:00
|
|
|
|
2017-05-15 16:59:12 -04:00
|
|
|
Here is a full list of the available functions for reference.
|
2013-08-08 19:56:09 -04:00
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
- `build_name` - The name of the build being run.
|
|
|
|
- `build_type` - The type of the builder being used currently.
|
2018-10-26 20:02:51 -04:00
|
|
|
- `env` - Returns environment variables. See example in [using home
|
|
|
|
variable](/docs/templates/user-variables.html#using-home-variable)
|
2017-06-14 21:04:16 -04:00
|
|
|
- `isotime [FORMAT]` - UTC time, which can be
|
2016-01-14 15:31:19 -05:00
|
|
|
[formatted](https://golang.org/pkg/time/#example_Time_Format). See more
|
2018-10-26 20:02:51 -04:00
|
|
|
examples below in [the `isotime` format
|
|
|
|
reference](/docs/templates/engine.html#isotime-function-format-reference).
|
2017-06-14 21:04:16 -04:00
|
|
|
- `lower` - Lowercases the string.
|
|
|
|
- `pwd` - The working directory while executing Packer.
|
2018-11-05 17:30:51 -05:00
|
|
|
- `sed` - Use [a golang implementation of sed](https://github.com/rwtodd/Go.Sed) to parse an input string.
|
2018-10-26 20:02:51 -04:00
|
|
|
- `split` - Split an input string using separator and return the requested
|
|
|
|
substring.
|
2017-06-14 21:04:16 -04:00
|
|
|
- `template_dir` - The directory to the template for the build.
|
|
|
|
- `timestamp` - The current Unix timestamp in UTC.
|
|
|
|
- `uuid` - Returns a random UUID.
|
|
|
|
- `upper` - Uppercases the string.
|
|
|
|
- `user` - Specifies a user variable.
|
2017-11-21 15:59:27 -05:00
|
|
|
- `packer_version` - Returns Packer version.
|
2017-05-15 16:59:12 -04:00
|
|
|
|
|
|
|
#### Specific to Amazon builders:
|
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
- `clean_ami_name` - AMI names can only contain certain characters. This
|
2018-10-26 20:02:51 -04:00
|
|
|
function will replace illegal characters with a '-" character. Example
|
|
|
|
usage since ":" is not a legal AMI name is: `{{isotime | clean_ami_name}}`.
|
2017-05-15 16:59:12 -04:00
|
|
|
|
2017-10-16 15:05:18 -04:00
|
|
|
#### Specific to Google Compute builders:
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
- `clean_image_name` - GCE image names can only contain certain characters
|
|
|
|
and the maximum length is 63. This function will convert upper cases to
|
|
|
|
lower cases and replace illegal characters with a "-" character. Example:
|
2017-10-20 17:06:02 -04:00
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
`"mybuild-{{isotime | clean_image_name}}"` will become
|
2017-10-20 17:06:02 -04:00
|
|
|
`mybuild-2017-10-18t02-06-30z`.
|
|
|
|
|
|
|
|
Note: Valid GCE image names must match the regex
|
|
|
|
`(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)`
|
|
|
|
|
|
|
|
This engine does not guarantee that the final image name will match the
|
|
|
|
regex; it will not truncate your name if it exceeds 63 characters, and it
|
2018-03-13 23:28:29 -04:00
|
|
|
will not validate that the beginning and end of the engine's output are
|
2018-10-26 20:02:51 -04:00
|
|
|
valid. For example, `"image_name": {{isotime | clean_image_name}}"` will
|
|
|
|
cause your build to fail because the image name will start with a number,
|
|
|
|
which is why in the above example we prepend the isotime with "mybuild".
|
2017-10-16 15:05:18 -04:00
|
|
|
|
2018-08-01 02:35:29 -04:00
|
|
|
#### Specific to Azure builders:
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
- `clean_image_name` - Azure managed image names can only contain certain
|
|
|
|
characters and the maximum length is 80. This function will replace illegal
|
|
|
|
characters with a "-" character. Example:
|
2018-08-01 02:35:29 -04:00
|
|
|
|
|
|
|
`"mybuild-{{isotime | clean_image_name}}"` will become
|
|
|
|
`mybuild-2017-10-18t02-06-30z`.
|
|
|
|
|
|
|
|
Note: Valid Azure image names must match the regex
|
|
|
|
`^[^_\\W][\\w-._)]{0,79}$`
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
This engine does not guarantee that the final image name will match the
|
|
|
|
regex; it will not truncate your name if it exceeds 80 characters, and it
|
|
|
|
will not validate that the beginning and end of the engine's output are
|
|
|
|
valid. It will truncate invalid characters from the end of the name when
|
|
|
|
converting illegal characters. For example,
|
|
|
|
`"managed_image_name: "My-Name::"` will be converted to
|
|
|
|
`"managed_image_name: "My-Name"`
|
2018-08-01 02:35:29 -04:00
|
|
|
|
2017-05-15 16:59:12 -04:00
|
|
|
## Template variables
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
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.
|
2017-05-15 16:59:12 -04:00
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
``` liquid
|
2017-05-15 16:59:12 -04:00
|
|
|
{
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "shell",
|
|
|
|
"execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'",
|
|
|
|
"scripts": [
|
|
|
|
"scripts/bootstrap.sh"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
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.
|
2013-09-07 21:50:01 -04:00
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
-> **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.
|
2017-05-15 16:59:12 -04:00
|
|
|
|
|
|
|
# isotime Function Format Reference
|
2014-09-05 19:21:30 -04:00
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
Formatting for the function `isotime` uses the magic reference date **Mon Jan 2
|
|
|
|
15:04:05 -0700 MST 2006**, which breaks down to the following:
|
2014-05-07 02:51:33 -04:00
|
|
|
|
2014-10-20 13:55:16 -04:00
|
|
|
<table class="table table-bordered table-condensed">
|
2017-03-25 18:13:52 -04:00
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>
|
|
|
|
</th>
|
|
|
|
<th align="center">
|
|
|
|
Day of Week
|
|
|
|
</th>
|
|
|
|
<th align="center">
|
|
|
|
Month
|
|
|
|
</th>
|
|
|
|
<th align="center">
|
|
|
|
Date
|
|
|
|
</th>
|
|
|
|
<th align="center">
|
|
|
|
Hour
|
|
|
|
</th>
|
|
|
|
<th align="center">
|
|
|
|
Minute
|
|
|
|
</th>
|
|
|
|
<th align="center">
|
|
|
|
Second
|
|
|
|
</th>
|
|
|
|
<th align="center">
|
|
|
|
Year
|
|
|
|
</th>
|
|
|
|
<th align="center">
|
|
|
|
Timezone
|
|
|
|
</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tr>
|
|
|
|
<th>
|
|
|
|
Numeric
|
|
|
|
</th>
|
|
|
|
<td align="center">
|
2017-10-11 22:36:54 -04:00
|
|
|
-
|
2017-03-25 18:13:52 -04:00
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
01
|
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
02
|
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
03 (15)
|
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
04
|
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
05
|
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
06
|
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
-0700
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<th>
|
|
|
|
Textual
|
|
|
|
</th>
|
|
|
|
<td align="center">
|
|
|
|
Monday (Mon)
|
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
January (Jan)
|
|
|
|
</td>
|
|
|
|
<td align="center">
|
2017-10-11 22:36:54 -04:00
|
|
|
-
|
2017-03-25 18:13:52 -04:00
|
|
|
</td>
|
|
|
|
<td align="center">
|
2017-10-11 22:36:54 -04:00
|
|
|
-
|
2017-03-25 18:13:52 -04:00
|
|
|
</td>
|
|
|
|
<td align="center">
|
2017-10-11 22:36:54 -04:00
|
|
|
-
|
2017-03-25 18:13:52 -04:00
|
|
|
</td>
|
|
|
|
<td align="center">
|
2017-10-11 22:36:54 -04:00
|
|
|
-
|
2017-03-25 18:13:52 -04:00
|
|
|
</td>
|
|
|
|
<td align="center">
|
2017-10-11 22:36:54 -04:00
|
|
|
-
|
2017-03-25 18:13:52 -04:00
|
|
|
</td>
|
|
|
|
<td align="center">
|
|
|
|
MST
|
|
|
|
</td>
|
|
|
|
</tr>
|
2014-10-20 13:55:16 -04:00
|
|
|
</table>
|
2015-07-22 22:31:00 -04:00
|
|
|
*The values in parentheses are the abbreviated, or 24-hour clock values*
|
2014-09-05 19:21:30 -04:00
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
Note that "-0700" is always formatted into "+0000" because `isotime` is always
|
|
|
|
UTC time.
|
2016-03-08 12:35:06 -05:00
|
|
|
|
2017-05-15 16:59:12 -04:00
|
|
|
Here are some example formatted time, using the above format options:
|
2014-09-05 19:21:30 -04:00
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
``` liquid
|
2014-05-07 02:51:33 -04:00
|
|
|
isotime = June 7, 7:22:43pm 2014
|
|
|
|
|
|
|
|
{{isotime "2006-01-02"}} = 2014-06-07
|
2015-04-09 16:43:12 -04:00
|
|
|
{{isotime "Mon 1504"}} = Sat 1922
|
2015-06-06 16:43:06 -04:00
|
|
|
{{isotime "02-Jan-06 03\_04\_05"}} = 07-Jun-2014 07\_22\_43
|
2014-05-07 02:51:33 -04:00
|
|
|
{{isotime "Hour15Year200603"}} = Hour19Year201407
|
2014-10-20 13:55:16 -04:00
|
|
|
```
|
2014-05-07 02:51:33 -04:00
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
Please note that double quote characters need escaping inside of templates (in
|
|
|
|
this case, on the `ami_name` value):
|
2015-01-17 04:20:19 -05:00
|
|
|
|
2017-06-14 21:04:16 -04:00
|
|
|
``` json
|
2015-01-17 04:20:19 -05:00
|
|
|
{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"type": "amazon-ebs",
|
|
|
|
"access_key": "...",
|
|
|
|
"secret_key": "...",
|
|
|
|
"region": "us-east-1",
|
2016-03-16 22:28:10 -04:00
|
|
|
"source_ami": "ami-fce3c696",
|
2016-01-13 16:52:17 -05:00
|
|
|
"instance_type": "t2.micro",
|
2015-01-17 04:20:19 -05:00
|
|
|
"ssh_username": "ubuntu",
|
|
|
|
"ami_name": "packer {{isotime \"2006-01-02\"}}"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
-> **Note:** See the [Amazon builder](/docs/builders/amazon.html)
|
|
|
|
documentation for more information on how to correctly configure the Amazon
|
|
|
|
builder in this example.
|
2018-06-08 01:41:57 -04:00
|
|
|
|
|
|
|
# split Function Format Reference
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
The function `split` takes an input string, a seperator string, and a numeric
|
|
|
|
component value and returns the requested substring.
|
2018-06-08 01:41:57 -04:00
|
|
|
|
2018-12-17 14:40:27 -05:00
|
|
|
Please note that you cannot use the `split` function on user variables,
|
|
|
|
because we can't nest the functions currently. This function is indended to
|
|
|
|
be used on builder variables like build_name. If you need a split user
|
|
|
|
variable, the best way to do it is to create a separate variable.
|
|
|
|
|
2018-06-08 01:41:57 -04:00
|
|
|
Here are some examples using the above options:
|
|
|
|
|
|
|
|
``` liquid
|
|
|
|
build_name = foo-bar-provider
|
|
|
|
|
|
|
|
{{split build_name "-" 0}} = foo
|
|
|
|
{{split "fixed-string" "-" 1}} = string
|
|
|
|
```
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
Please note that double quote characters need escaping inside of templates (in
|
|
|
|
this case, on the `fixed-string` value):
|
2018-06-08 01:41:57 -04:00
|
|
|
|
|
|
|
``` json
|
|
|
|
{
|
|
|
|
"post-processors": [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "vagrant",
|
|
|
|
"compression_level": 9,
|
|
|
|
"keep_input_artifact": false,
|
2018-12-17 14:40:27 -05:00
|
|
|
"vagrantfile_template": "tpl/{{split build_name \"-\" 1}}.rb",
|
2018-06-08 01:41:57 -04:00
|
|
|
"output": "output/{{build_name}}.box",
|
|
|
|
"only": [
|
|
|
|
"org-name-provider"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
2018-11-05 17:30:51 -05:00
|
|
|
|
|
|
|
# sed Function Format Reference
|
|
|
|
|
|
|
|
See the library documentation https://github.com/rwtodd/Go.Sed for notes about
|
|
|
|
the difference between this golang implementation of sed and the regexes you may
|
|
|
|
be used to. A very simple example of this functionality:
|
|
|
|
|
|
|
|
```
|
|
|
|
"provisioners": [
|
|
|
|
{
|
|
|
|
"type": "shell-local",
|
|
|
|
"environment_vars": ["EXAMPLE={{ sed \"s/null/awesome/\" build_type}}"],
|
|
|
|
"inline": ["echo build_type is $EXAMPLE\n"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|