2013-06-10 18:08:14 -04:00
|
|
|
---
|
|
|
|
layout: "docs"
|
2013-08-09 19:25:13 -04:00
|
|
|
page_title: "Configuration Templates"
|
2013-06-10 18:08:14 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
# Configuration Templates
|
|
|
|
|
2013-08-08 19:56:09 -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 configuration parameter at runtime.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
In addition to globally available functions like timestamp shown before,
|
|
|
|
some configurations have special local variables that are available only
|
|
|
|
for that configuration. These are recognizable because they're prefixed by
|
|
|
|
a period, such as `{{.Name}}`.
|
|
|
|
|
|
|
|
The complete syntax is covered in the next section, followed by a reference
|
|
|
|
of globally available functions.
|
2013-06-10 18:08:14 -04:00
|
|
|
|
|
|
|
## Syntax
|
|
|
|
|
2013-08-08 19:56:09 -04:00
|
|
|
The syntax of templates is extremely simple. Anything template related
|
|
|
|
happens within double-braces: `{{ }}`. Variables are prefixed with a period
|
|
|
|
and capitalized, such as `{{.Variable}}` and functions are just directly
|
|
|
|
within the braces, such as `{{timestamp}}`.
|
|
|
|
|
|
|
|
Here is an example from the VMware VMX template that shows configuration
|
|
|
|
templates in action:
|
2013-06-10 18:08:14 -04:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
.encoding = "UTF-8"
|
|
|
|
displayName = "{{ .Name }}"
|
|
|
|
guestOS = "{{ .GuestOS }}"
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
In this case, the "Name" and "GuestOS" variables will be replaced, potentially
|
|
|
|
resulting in a VMX that looks like this:
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
.encoding = "UTF-8"
|
|
|
|
displayName = "packer"
|
|
|
|
guestOS = "otherlinux"
|
|
|
|
</pre>
|
2013-08-08 19:56:09 -04:00
|
|
|
|
|
|
|
## Global Functions
|
|
|
|
|
|
|
|
While some configuration settings have local variables specific to only that
|
|
|
|
configuration, a set of functions are available globally for use in _any string_
|
|
|
|
in Packer templates. These are listed below for reference.
|
|
|
|
|
2014-09-05 19:17:59 -04:00
|
|
|
* `lower` - Lowercases the string.
|
|
|
|
* `pwd` - The working directory while executing Packer.
|
2014-09-05 19:21:30 -04:00
|
|
|
* `isotime [FORMAT]` - UTC time, which can be [formatted](http://golang.org/pkg/time/#example_Time_Format).
|
|
|
|
See more examples below.
|
2013-12-27 22:44:26 -05:00
|
|
|
* `timestamp` - The current Unix timestamp in UTC.
|
|
|
|
* `uuid` - Returns a random UUID.
|
2014-09-05 19:17:59 -04:00
|
|
|
* `upper` - Uppercases the string.
|
2013-09-07 21:50:01 -04:00
|
|
|
|
2014-05-07 02:51:33 -04:00
|
|
|
### isotime Format
|
2014-09-05 19:21:30 -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
|
|
|
|
|
|
|
<table border="1" cellpadding="5" width="100%">
|
|
|
|
<tr bgcolor="lightgray">
|
|
|
|
<td></td>
|
|
|
|
<td align="center"><strong>Day of Week</strong></td>
|
|
|
|
<td align="center"><strong>Month</strong></td>
|
|
|
|
<td align="center"><strong>Date</strong></td>
|
|
|
|
<td align="center"><strong>Hour</strong></td>
|
|
|
|
<td align="center"><strong>Minute</strong></td>
|
|
|
|
<td align="center"><strong>Second</strong></td>
|
|
|
|
<td align="center"><strong>Year</strong></td>
|
|
|
|
<td align="center"><strong>Timezone</strong></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td><strong>Numeric</strong></td>
|
|
|
|
<td align="center">-</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>
|
|
|
|
<td><strong>Textual</strong></td>
|
|
|
|
<td align="center">Monday (Mon)</td>
|
|
|
|
<td align="center">January (Jan)</td>
|
|
|
|
<td align="center">-</td>
|
|
|
|
<td align="center">-</td>
|
|
|
|
<td align="center">-</td>
|
|
|
|
<td align="center">-</td>
|
|
|
|
<td align="center">-</td>
|
|
|
|
<td align="center">MST</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2014-09-05 19:21:30 -04:00
|
|
|
|
2014-05-07 02:51:33 -04:00
|
|
|
_The values in parentheses are the abbreviated, or 24-hour clock values_
|
2014-09-05 19:21:30 -04:00
|
|
|
|
2014-05-07 02:51:33 -04:00
|
|
|
Here are some example formated time, using the above format options:
|
2014-09-05 19:21:30 -04:00
|
|
|
|
2014-05-07 02:51:33 -04:00
|
|
|
<pre>
|
|
|
|
isotime = June 7, 7:22:43pm 2014
|
|
|
|
|
|
|
|
{{isotime "2006-01-02"}} = 2014-06-07
|
|
|
|
{{isotime "Mon 1506"}} = Sat 1914
|
|
|
|
{{isotime "01-Jan-06 03\_04\_05"}} = 07-Jun-2014 07\_22\_43
|
|
|
|
{{isotime "Hour15Year200603"}} = Hour19Year201407
|
|
|
|
</pre>
|
|
|
|
|
2013-09-07 21:50:01 -04:00
|
|
|
## Amazon Specific Functions
|
|
|
|
|
|
|
|
Specific to Amazon builders:
|
|
|
|
|
|
|
|
* ``clean_ami_name`` - AMI names can only contain certain characters. This
|
|
|
|
function will replace illegal characters with a '-" character. Example usage
|
|
|
|
since ":" is not a legal AMI name is: `{{isotime | clean_ami_name}}`.
|