packer-cn/website/source/docs/templates/configuration-templates.htm...

4.6 KiB

layout page_title description
docs Configuration Templates 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.

Configuration Templates

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.

Syntax

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:

.encoding = "UTF-8"
displayName = "{{ .Name }}"
guestOS = "{{ .GuestOS }}"

In this case, the "Name" and "GuestOS" variables will be replaced, potentially resulting in a VMX that looks like this:

.encoding = "UTF-8"
displayName = "packer"
guestOS = "otherlinux"

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.

  • build_name - The name of the build being run.
  • build_type - The type of the builder being used currently.
  • isotime [FORMAT] - UTC time, which can be formatted. See more examples below.
  • lower - Lowercases the string.
  • pwd - The working directory while executing Packer.
  • 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.

isotime Format

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:

Day of Week Month Date Hour Minute Second Year Timezone
Numeric - 01 02 03 (15) 04 05 06 -0700
Textual Monday (Mon) January (Jan) - - - - - MST

The values in parentheses are the abbreviated, or 24-hour clock values

Here are some example formated time, using the above format options:

isotime = June 7, 7:22:43pm 2014

{{isotime "2006-01-02"}} = 2014-06-07
{{isotime "Mon 1504"}} = Sat 1922
{{isotime "02-Jan-06 03\_04\_05"}} = 07-Jun-2014 07\_22\_43
{{isotime "Hour15Year200603"}} = Hour19Year201407

Please note that double quote characters need escaping inside of templates:

{
  "builders": [
    {
      "type": "amazon-ebs",
      "access_key": "...",
      "secret_key": "...",
      "region": "us-east-1",
      "source_ami": "ami-de0d9eb7",
      "instance_type": "t1.micro",
      "ssh_username": "ubuntu",
      "ami_name": "packer {{isotime \"2006-01-02\"}}"
    }
  ]
}

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}}.