github.com/aspring/packer@v0.8.1-0.20150629211158-9db281ac0f89/website/source/docs/templates/configuration-templates.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Configuration Templates"
     4  description: |-
     5    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.
     6  ---
     7  
     8  # Configuration Templates
     9  
    10  All strings within templates are processed by a common Packer templating
    11  engine, where variables and functions can be used to modify the value of
    12  a configuration parameter at runtime.
    13  
    14  For example, the `{{timestamp}}` function can be used in any string to
    15  generate the current timestamp. This is useful for configurations that require
    16  unique keys, such as AMI names. By setting the AMI name to something like
    17  `My Packer AMI {{timestamp}}`, the AMI name will be unique down to the second.
    18  
    19  In addition to globally available functions like timestamp shown before,
    20  some configurations have special local variables that are available only
    21  for that configuration. These are recognizable because they're prefixed by
    22  a period, such as `{{.Name}}`.
    23  
    24  The complete syntax is covered in the next section, followed by a reference
    25  of globally available functions.
    26  
    27  ## Syntax
    28  
    29  The syntax of templates is extremely simple. Anything template related
    30  happens within double-braces: `{{ }}`. Variables are prefixed with a period
    31  and capitalized, such as `{{.Variable}}` and functions are just directly
    32  within the braces, such as `{{timestamp}}`.
    33  
    34  Here is an example from the VMware VMX template that shows configuration
    35  templates in action:
    36  
    37  ```liquid
    38  .encoding = "UTF-8"
    39  displayName = "{{ .Name }}"
    40  guestOS = "{{ .GuestOS }}"
    41  ```
    42  
    43  In this case, the "Name" and "GuestOS" variables will be replaced, potentially
    44  resulting in a VMX that looks like this:
    45  
    46  ```liquid
    47  .encoding = "UTF-8"
    48  displayName = "packer"
    49  guestOS = "otherlinux"
    50  ```
    51  
    52  ## Global Functions
    53  
    54  While some configuration settings have local variables specific to only that
    55  configuration, a set of functions are available globally for use in _any string_
    56  in Packer templates. These are listed below for reference.
    57  
    58  * `build_name` - The name of the build being run.
    59  * `build_type` - The type of the builder being used currently.
    60  * `isotime [FORMAT]` - UTC time, which can be [formatted](http://golang.org/pkg/time/#example_Time_Format).
    61     See more examples below.
    62  * `lower` - Lowercases the string.
    63  * `pwd` - The working directory while executing Packer.
    64  * `template_dir` - The directory to the template for the build.
    65  * `timestamp` - The current Unix timestamp in UTC.
    66  * `uuid` - Returns a random UUID.
    67  * `upper` - Uppercases the string.
    68  
    69  ### isotime Format
    70  
    71  Formatting for the function `isotime` uses the magic reference date
    72  **Mon Jan 2 15:04:05 -0700 MST 2006**, which breaks down to the following:
    73  
    74  <div class="table-responsive">
    75  <table class="table table-bordered table-condensed">
    76  <thead>
    77  <tr>
    78  <th></th>
    79  <th align="center">Day of Week</th>
    80  <th align="center">Month</th>
    81  <th align="center">Date</th>
    82  <th align="center">Hour</th>
    83  <th align="center">Minute</th>
    84  <th align="center">Second</th>
    85  <th align="center">Year</th>
    86  <th align="center">Timezone</th>
    87  </tr>
    88  </thead>
    89  <tr>
    90  <th>Numeric</th>
    91  <td align="center">-</td>
    92  <td align="center">01</td>
    93  <td align="center">02</td>
    94  <td align="center">03 (15)</td>
    95  <td align="center">04</td>
    96  <td align="center">05</td>
    97  <td align="center">06</td>
    98  <td align="center">-0700</td>
    99  </tr>
   100  <tr>
   101  <th>Textual</th>
   102  <td align="center">Monday (Mon)</td>
   103  <td align="center">January (Jan)</td>
   104  <td align="center">-</td>
   105  <td align="center">-</td>
   106  <td align="center">-</td>
   107  <td align="center">-</td>
   108  <td align="center">-</td>
   109  <td align="center">MST</td>
   110  </tr>
   111  </table>
   112  </div>
   113  
   114   _The values in parentheses are the abbreviated, or 24-hour clock values_
   115  
   116   Here are some example formated time, using the above format options:
   117  
   118  ```liquid
   119  isotime = June 7, 7:22:43pm 2014
   120  
   121  {{isotime "2006-01-02"}} = 2014-06-07
   122  {{isotime "Mon 1504"}} = Sat 1922
   123  {{isotime "02-Jan-06 03\_04\_05"}} = 07-Jun-2014 07\_22\_43
   124  {{isotime "Hour15Year200603"}} = Hour19Year201407
   125  ```
   126  
   127  Please note that double quote characters need escaping inside of templates:
   128  
   129  ```javascript
   130  {
   131    "builders": [
   132      {
   133        "type": "amazon-ebs",
   134        "access_key": "...",
   135        "secret_key": "...",
   136        "region": "us-east-1",
   137        "source_ami": "ami-de0d9eb7",
   138        "instance_type": "t1.micro",
   139        "ssh_username": "ubuntu",
   140        "ami_name": "packer {{isotime \"2006-01-02\"}}"
   141      }
   142    ]
   143  }
   144  ```
   145  
   146  ## Amazon Specific Functions
   147  
   148  Specific to Amazon builders:
   149  
   150  * ``clean_ami_name`` - AMI names can only contain certain characters. This
   151    function will replace illegal characters with a '-" character. Example usage
   152    since ":" is not a legal AMI name is: `{{isotime | clean_ami_name}}`.