github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/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  * `lower` - Lowercases the string.
    59  * `pwd` - The working directory while executing Packer.
    60  * `isotime [FORMAT]` - UTC time, which can be [formatted](http://golang.org/pkg/time/#example_Time_Format).
    61     See more examples below.
    62  * `timestamp` - The current Unix timestamp in UTC.
    63  * `uuid` - Returns a random UUID.
    64  * `upper` - Uppercases the string.
    65  
    66  ### isotime Format
    67  
    68  Formatting for the function `isotime` uses the magic reference date
    69  **Mon Jan 2 15:04:05 -0700 MST 2006**, which breaks down to the following:
    70  
    71  <div class="table-responsive">
    72  <table class="table table-bordered table-condensed">
    73  <thead>
    74  <tr>
    75  <th></th>
    76  <th align="center">Day of Week</th>
    77  <th align="center">Month</th>
    78  <th align="center">Date</th>
    79  <th align="center">Hour</th>
    80  <th align="center">Minute</th>
    81  <th align="center">Second</th>
    82  <th align="center">Year</th>
    83  <th align="center">Timezone</th>
    84  </tr>
    85  </thead>
    86  <tr>
    87  <th>Numeric</th>
    88  <td align="center">-</td>
    89  <td align="center">01</td>
    90  <td align="center">02</td>
    91  <td align="center">03 (15)</td>
    92  <td align="center">04</td>
    93  <td align="center">05</td>
    94  <td align="center">06</td>
    95  <td align="center">-0700</td>
    96  </tr>
    97  <tr>
    98  <th>Textual</th>
    99  <td align="center">Monday (Mon)</td>
   100  <td align="center">January (Jan)</td>
   101  <td align="center">-</td>
   102  <td align="center">-</td>
   103  <td align="center">-</td>
   104  <td align="center">-</td>
   105  <td align="center">-</td>
   106  <td align="center">MST</td>
   107  </tr>
   108  </table>
   109  </div>
   110  
   111   _The values in parentheses are the abbreviated, or 24-hour clock values_
   112  
   113   Here are some example formated time, using the above format options:
   114  
   115  ```liquid
   116  isotime = June 7, 7:22:43pm 2014
   117  
   118  {{isotime "2006-01-02"}} = 2014-06-07
   119  {{isotime "Mon 1506"}} = Sat 1914
   120  {{isotime "01-Jan-06 03\_04\_05"}} = 07-Jun-2014 07\_22\_43
   121  {{isotime "Hour15Year200603"}} = Hour19Year201407
   122  ```
   123  
   124  ## Amazon Specific Functions
   125  
   126  Specific to Amazon builders:
   127  
   128  * ``clean_ami_name`` - AMI names can only contain certain characters. This
   129    function will replace illegal characters with a '-" character. Example usage
   130    since ":" is not a legal AMI name is: `{{isotime | clean_ami_name}}`.