github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/website/source/docs/templates/configuration-templates.html.md (about)

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