github.com/hashicorp/packer@v1.14.3/website/content/docs/templates/legacy_json_templates/engine.mdx (about)

     1  ---
     2  description: |
     3    The JSON template engine processes strings in JSON templates. Learn how to use JSON template engine functions and variables.
     4  page_title: JSON template engine reference
     5  ---
     6  
     7  # JSON template engine reference 
     8  
     9  This topic describes the Packer engine that processes JSON templates. 
    10  
    11  `@include 'from-1.5/legacy-json-warning.mdx'`
    12  
    13  ## Description
    14  
    15  All strings within templates are processed by a common Packer templating
    16  engine. The engine uses variables and functions to modify the value of a
    17  configuration parameter at runtime.
    18  
    19  The syntax of templates uses the following conventions:
    20  
    21  - Anything template related happens within double-braces: `{{ }}`.
    22  - Functions are specified directly within the braces, such as
    23    `{{timestamp}}`.
    24  - Template variables are prefixed with a period and capitalized, such as
    25    `{{.Variable}}`.
    26  
    27  ## Functions
    28  
    29  Functions perform operations on and within strings, for example the
    30  `{{timestamp}}` function can be used in any string to generate the current
    31  timestamp. This is useful for configurations that require unique keys, such as
    32  AMI names. By setting the AMI name to something like `My Packer AMI {{timestamp}}`, the AMI name will be unique down to the second. If you need
    33  greater than one second granularity, you should use `{{uuid}}`, for example
    34  when you have multiple builders in the same template.
    35  
    36  Here is a full list of the available functions for reference.
    37  
    38  - `build_name` - The name of the build being run.
    39  - `build_type` - The type of the builder being used currently.
    40  - `clean_resource_name` - Image names can only contain certain characters and
    41    have a maximum length, eg 63 on GCE & 80 on Azure. `clean_resource_name`
    42    will convert upper cases to lower cases and replace illegal characters with
    43    a "-" character. Example:
    44  
    45    `"mybuild-{{isotime | clean_resource_name}}"` will become `mybuild-2017-10-18t02-06-30z`.
    46  
    47    Note: Valid Azure image names must match the regex
    48    `^[^_\\W][\\w-._)]{0,79}$`
    49  
    50    Note: Valid GCE image names must match the regex
    51    `(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)`
    52  
    53    This engine does not guarantee that the final image name will match the
    54    regex; it will not truncate your name if it exceeds the maximum number of
    55    allowed characters, and it will not validate that the beginning and end of
    56    the engine's output are valid. For example, `"image_name": {{isotime | clean_resource_name}}"` will cause your build to fail because the image
    57    name will start with a number, which is why in the above example we prepend
    58    the isotime with "mybuild".
    59    Exact behavior of `clean_resource_name` will depend on which builder it is
    60    being applied to; refer to build-specific docs below for more detail on how
    61    each function will behave.
    62  
    63  - `env` - Returns environment variables. See example in [using home
    64    variable](/packer/docs/templates/legacy_json_templates/user-variables#using-home-variable)
    65  - `build` - This engine will allow you to access, from provisioners and post-processors, special variables that
    66    provide connection information and basic instance state information.
    67    Usage example:
    68  
    69    ```json
    70    {
    71      "type": "shell-local",
    72      "environment_vars": ["TESTVAR={{ build `PackerRunUUID`}}"],
    73      "inline": ["echo $TESTVAR"]
    74    }
    75    ```
    76  
    77    Valid variables to request are:
    78  
    79    - **ID**: Represents the VM being provisioned. For example, in Amazon it is the instance ID; in DigitalOcean,
    80      it is the Droplet ID; in VMware, it is the VM name.
    81  
    82    - **Host**, **Port**, **User** and **Password**: The host, port, user, and password that Packer uses to access the machine.
    83      Useful for using the shell local provisioner to run Ansible or Inspec against the provisioned instance.
    84  
    85    - **ConnType**: Type of communicator being used. For example, for SSH communicator this will be "ssh".
    86  
    87    - **PackerRunUUID**: Current build's unique ID. Can be used to specify build artifacts.
    88      An example of that, is when multiple builds runs at the same time producing the same artifact.
    89      It's possible to differentiate these artifacts by naming them with the builds' unique IDs.
    90  
    91    - **PackerHTTPIP**, **PackerHTTPPort**, and **PackerHTTPAddr**: HTTP IP, port, and address of the file server Packer creates to serve items in the "http" dir to the VM. The HTTP address is displayed in the format `IP:PORT`.
    92  
    93    - **SSHPublicKey** and **SSHPrivateKey**: The public and private key that Packer uses to connect to the instance.
    94      These are unique to the SSH communicator and are unset when using other communicators.
    95      **SSHPublicKey** and **SSHPrivateKey** can have escape sequences and special characters so their output should be single quoted to avoid surprises. For example:
    96  
    97      ```json
    98      { ... "provisioners": [{
    99        "type": "shell",
   100        "inline": [ "echo '{{ build `SSHPrivateKey`}}' > /tmp/packer-session.pem" ]
   101        }]
   102      }
   103      ```
   104  
   105    For backwards compatibility, `WinRMPassword` is also available through this
   106    engine, though it is no different than using the more general `Password`.
   107  
   108    This function is only for use within specific options inside of
   109    _provisioners_ -- these options will be listed as being template engines
   110    in the provisioner documentation.
   111  
   112    For builder-specific builder variables, please also refer to the builder docs:
   113  
   114    - Amazon EC2: [chroot](/packer/plugins/builders/amazon/chroot#build-shared-information-variables),
   115      [EBS Volume](/packer/plugins/builders/amazon/ebsvolume#build-shared-information-variables),
   116      [EBS](/packer/plugins/builders/amazon/ebs#build-shared-information-variables),
   117      [EBS Surrogate](/packer/plugins/builders/amazon/ebssurrogate#build-shared-information-variables),
   118      [Instance](/packer/plugins/builders/amazon/instance#build-shared-information-variables).
   119  
   120    This engine is in beta; please report any issues or requests on the Packer
   121    issue tracker on GitHub.
   122  
   123  - `isotime [FORMAT]` - UTC time, which can be
   124    [formatted](https://pkg.go.dev/time#example_Time_Format). See more
   125    examples below in [the `isotime` format
   126    reference](/packer/docs/templates/legacy_json_templates/engine#isotime-function-format-reference).
   127    `strftime FORMAT` - UTC time, formated using the ISO C standard format
   128    `FORMAT`. See
   129    [jehiah/go-strftime](https://github.com/jehiah/go-strftime) for a list
   130    of available format specifiers.
   131  
   132    Please note that if you are using a large number of builders,
   133    provisioners or post-processors, using the isotime engine directly in the
   134    plugin configuration may cause the timestamp to be slightly diffferent for
   135    each plugin. This is because the timestamp is generated when each plugin is
   136    launched rather than in the initial Packer process. In order to avoid this
   137    and make sure the timestamp is consistent across all plugins, set it as a user
   138    variable and then access the user variable within your plugins.
   139  
   140  - `lower` - Lowercases the string.
   141  - `packer_version` - Returns Packer version.
   142  - `pwd` - The working directory while executing Packer.
   143  - `replace` - ( old, new string, n int, s ) Replace returns a copy of the
   144    string s with the first n non-overlapping instances of old replaced by new.
   145  - `replace_all` - ( old, new string, s ) ReplaceAll returns a copy of the
   146    string s with all non-overlapping instances of old replaced by new.
   147  - `split` - Split an input string using separator and return the requested
   148    substring.
   149  - `template_dir` - The directory to the template for the build.
   150  - `timestamp` - The Unix timestamp in UTC when the Packer process was
   151    launched. Please note that if you are using a large number of builders,
   152    provisioners or post-processors, the timestamp may be slightly
   153    different for each one because it is from when the plugin is
   154    launched not the initial Packer process. In order to avoid this and make
   155    the timestamp consistent across all plugins, set it as a user variable
   156    and then access the user variable within your plugins.
   157  - `uuid` - Returns a random UUID.
   158  - `upper` - Uppercases the string.
   159  - `user` - Specifies a user variable.
   160  
   161  #### Specific to Amazon builders:
   162  
   163  - `clean_resource_name` - AMI names
   164    can only contain certain characters. This function will replace illegal
   165    characters with a '-" character. Example usage since ":" is not a legal AMI
   166    name is: `{{isotime | clean_resource_name}}`.
   167  
   168  #### Specific to Google Compute builders:
   169  
   170  - `clean_resource_name` - GCE
   171    image names can only contain certain characters and the maximum length is
   172  
   173    63. This function will convert upper cases to lower cases and replace
   174        illegal characters with a "-" character. Example:
   175  
   176    `"mybuild-{{isotime | clean_resource_name}}"` will become
   177    `mybuild-2017-10-18t02-06-30z`.
   178  
   179    Note: Valid GCE image names must match the regex
   180    `(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)`
   181  
   182    This engine does not guarantee that the final image name will match the
   183    regex; it will not truncate your name if it exceeds 63 characters, and it
   184    will not validate that the beginning and end of the engine's output are
   185    valid. For example, `"image_name": {{isotime | clean_resource_name}}"` will
   186    cause your build to fail because the image name will start with a number,
   187    which is why in the above example we prepend the isotime with "mybuild".
   188  
   189  #### Specific to Azure builders:
   190  
   191  - `clean_resource_name` - Azure
   192    managed image names can only contain certain characters and the maximum
   193    length is 80. This function will replace illegal characters with a "-"
   194    character. Example:
   195  
   196    `"mybuild-{{isotime | clean_resource_name}}"` will become
   197    `mybuild-2017-10-18t02-06-30z`.
   198  
   199    Note: Valid Azure image names must match the regex
   200    `^[^_\\W][\\w-._)]{0,79}$`
   201  
   202    This engine does not guarantee that the final image name will match the
   203    regex; it will not truncate your name if it exceeds 80 characters, and it
   204    will not validate that the beginning and end of the engine's output are
   205    valid. It will truncate invalid characters from the end of the name when
   206    converting illegal characters. For example, `"managed_image_name: "My-Name::"` will be converted to `"managed_image_name: "My-Name"`
   207  
   208  ## Template variables
   209  
   210  Template variables are special variables automatically set by Packer at build
   211  time. Some builders, provisioners and other components have template variables
   212  that are available only for that component. Template variables are recognizable
   213  because they're prefixed by a period, such as `{{ .Name }}`. For example, when
   214  using the [`shell`](/packer/plugins/builders/vmware/iso) builder template variables
   215  are available to customize the
   216  [`execute_command`](/packer/docs/provisioners/shell#execute_command) parameter
   217  used to determine how Packer will run the shell command.
   218  
   219  ```json
   220  {
   221    "provisioners": [
   222      {
   223        "type": "shell",
   224        "execute_command": "{{.Vars}} sudo -E -S bash '{{.Path}}'",
   225        "scripts": ["scripts/bootstrap.sh"]
   226      }
   227    ]
   228  }
   229  ```
   230  
   231  The `{{ .Vars }}` and `{{ .Path }}` template variables will be replaced with
   232  the list of the environment variables and the path to the script to be executed
   233  respectively.
   234  
   235  -> **Note:** In addition to template variables, you can specify your own
   236  user variables. See the [user variable](/packer/docs/templates/legacy_json_templates/user-variables)
   237  documentation for more information on user variables.
   238  
   239  ## `isotime` Function Format Reference
   240  
   241  The `isotime` template engine uses Go to generate timestamps. If you're
   242  unfamiliar with Go, then the way you format the timestamp is going to
   243  feel a bit unusual compared to how you may be used to formatting
   244  datetime strings.
   245  
   246  Full docs and examples for the Go time formatting function can be found
   247  [here](https://pkg.go.dev/time#example_Time_Format)
   248  
   249  However, the formatting basics are worth describing here. From the [Go docs](https://pkg.go.dev/time#pkg-constants):
   250  
   251  > These are predefined layouts for use in Time.Format and time.Parse. The
   252  > reference time used in the layouts is the specific time:
   253  >
   254  > Mon Jan 2 15:04:05 MST 2006
   255  >
   256  > which is Unix time 1136239445. Since MST is GMT-0700, the reference time
   257  > can be thought of as
   258  >
   259  > 01/02 03:04:05PM '06 -0700
   260  >
   261  > To define your own format, write down what the reference time would look like
   262  > formatted your way; see the values of constants like ANSIC, StampMicro or
   263  > Kitchen for examples. The model is to demonstrate what the reference time
   264  > looks like so that the Format and Parse methods can apply the same
   265  > transformation to a general time value.
   266  
   267  So what does that look like in a Packer template function? Here's an example
   268  of how you'd declare a variable using the isotime function.
   269  
   270  ```json
   271  "variables": {
   272    "myvar": "packer-{{isotime `2006-01-02 03:04:05`}}"
   273  }
   274  ```
   275  
   276  You can try and modify the following examples in a packer template or in
   277  `packer console` to get an idea of how to set different timestamps:
   278  
   279  | Input                                            | Output                       |
   280  | ------------------------------------------------ | ---------------------------- |
   281  | `` "packer-{{isotime `2006-01-02`}}" ``          | "packer-2021-05-17"          |
   282  | `` "packer-{{isotime `Jan-_2-15:04:05.000`}}" `` | "packer-May-17-23:40:16.786" |
   283  | `` "packer-{{isotime `3:04PM`}}" ``              | "packer-11:40PM"             |
   284  | `"{{ isotime }}"`                                | "June 7, 7:22:43pm 2014"     |
   285  | `` "{{isotime `2006-01-02`}}" ``                 | "2014-06-07"                 |
   286  | `` "{{isotime `Mon 1504`}}" ``                   | "Sat 1922"                   |
   287  | `` "{{isotime `02-Jan-06 03\_04\_05`}}" ``       | "07-Jun-2014 07_22_43"       |
   288  | `` "{{isotime `Hour15Year200603`}}" ``           | "Hour19Year201407"           |
   289  
   290  Formatting for the function `isotime` uses the magic reference date **Mon Jan 2
   291  15:04:05 -0700 MST 2006**, which breaks down to the following:
   292  
   293  <table class="table table-bordered table-condensed">
   294    <thead>
   295      <tr>
   296        <th></th>
   297        <th align="center">Day of Week</th>
   298        <th align="center">Month</th>
   299        <th align="center">Date</th>
   300        <th align="center">Hour</th>
   301        <th align="center">Minute</th>
   302        <th align="center">Second</th>
   303        <th align="center">Year</th>
   304        <th align="center">Timezone</th>
   305      </tr>
   306    </thead>
   307    <tr>
   308      <th>Numeric</th>
   309      <td align="center">-</td>
   310      <td align="center">01</td>
   311      <td align="center">02</td>
   312      <td align="center">03 (15)</td>
   313      <td align="center">04</td>
   314      <td align="center">05</td>
   315      <td align="center">06</td>
   316      <td align="center">-0700</td>
   317    </tr>
   318    <tr>
   319      <th>Textual</th>
   320      <td align="center">Monday (Mon)</td>
   321      <td align="center">January (Jan)</td>
   322      <td align="center">-</td>
   323      <td align="center">-</td>
   324      <td align="center">-</td>
   325      <td align="center">-</td>
   326      <td align="center">-</td>
   327      <td align="center">MST</td>
   328    </tr>
   329  </table>
   330  
   331  _The values in parentheses are the abbreviated, or 24-hour clock values_
   332  
   333  Note that "-0700" is always formatted into "+0000" because `isotime` is always
   334  UTC time.
   335  
   336  ## `split` Function Format Reference
   337  
   338  The function `split` takes an input string, a seperator string, and a numeric
   339  component value and returns the requested substring.
   340  
   341  Please note that you cannot use the `split` function on user variables, because
   342  we can't nest the functions currently. This function is intended to be used on
   343  builder variables like build_name. If you need a split user variable, the best
   344  way to do it is to create a separate variable.
   345  
   346  Here are some examples using the above options:
   347  
   348  ```liquid
   349  build_name = foo-bar-provider
   350  
   351  {{split build_name "-" 0}} = foo
   352  {{split "fixed-string" "-" 1}} = string
   353  ```
   354  
   355  Please note that double quote characters need escaping inside of templates (in
   356  this case, on the `fixed-string` value):
   357  
   358  ```json
   359  {
   360    "post-processors": [
   361      [
   362        {
   363          "type": "vagrant",
   364          "compression_level": 9,
   365          "keep_input_artifact": false,
   366          "vagrantfile_template": "tpl/{{split build_name \"-\" 1}}.rb",
   367          "output": "output/{{build_name}}.box",
   368          "only": ["org-name-provider"]
   369        }
   370      ]
   371    ]
   372  }
   373  ```
   374  
   375  ## `replace` Function Format Reference
   376  
   377  Here are some examples using the `replace` options:
   378  
   379  ```liquid
   380  build_name = foo-bar-provider
   381  
   382  {{ replace_all "-" "/" build_name }}  = foo/bar/provider
   383  {{ build_name | replace "-" "/" 1 }} = foo/bar-provider
   384  ```