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

     1  ---
     2  description: |
     3      User variables allow your templates to be further configured with variables from
     4      the command-line, environment variables, or files. This lets you parameterize
     5      your templates so that you can keep secret tokens, environment-specific data,
     6      and other types of information out of your templates. This maximizes the
     7      portability and shareability of the template.
     8  layout: docs
     9  page_title: User Variables in Templates
    10  ...
    11  
    12  # User Variables
    13  
    14  User variables allow your templates to be further configured with variables from
    15  the command-line, environment variables, or files. This lets you parameterize
    16  your templates so that you can keep secret tokens, environment-specific data,
    17  and other types of information out of your templates. This maximizes the
    18  portability and shareability of the template.
    19  
    20  Using user variables expects you know how [configuration
    21  templates](/docs/templates/configuration-templates.html) work. If you don't know
    22  how configuration templates work yet, please read that page first.
    23  
    24  ## Usage
    25  
    26  User variables must first be defined in a `variables` section within your
    27  template. Even if you want a variable to default to an empty string, it must be
    28  defined. This explicitness makes it easy for newcomers to your template to
    29  understand what can be modified using variables in your template.
    30  
    31  The `variables` section is a simple key/value mapping of the variable name to a
    32  default value. A default value can be the empty string. An example is shown
    33  below:
    34  
    35  ``` {.javascript}
    36  {
    37    "variables": {
    38      "aws_access_key": "",
    39      "aws_secret_key": ""
    40    },
    41  
    42    "builders": [{
    43      "type": "amazon-ebs",
    44      "access_key": "{{user `aws_access_key`}}",
    45      "secret_key": "{{user `aws_secret_key`}}",
    46      // ...
    47    }]
    48  }
    49  ```
    50  
    51  In the above example, the template defines two variables: `aws_access_key` and
    52  `aws_secret_key`. They default to empty values. Later, the variables are used
    53  within the builder we defined in order to configure the actual keys for the
    54  Amazon builder.
    55  
    56  If the default value is `null`, then the user variable will be *required*. This
    57  means that the user must specify a value for this variable or template
    58  validation will fail.
    59  
    60  Using the variables is extremely easy. Variables are used by calling the user
    61  function in the form of <code>{{user \`variable\`}}</code>. This function can be
    62  used in *any value* within the template, in builders, provisioners, *anything*.
    63  The user variable is available globally within the template.
    64  
    65  ## Environment Variables
    66  
    67  Environment variables can be used within your template using user variables.
    68  The `env` function is available *only* within the default value of a user
    69  variable, allowing you to default a user variable to an environment variable.
    70  An example is shown below:
    71  
    72  ``` {.javascript}
    73  {
    74    "variables": {
    75      "my_secret": "{{env `MY_SECRET`}}",
    76    },
    77  
    78    // ...
    79  }
    80  ```
    81  
    82  This will default "my\_secret" to be the value of the "MY\_SECRET" environment
    83  variable (or the empty string if it does not exist).
    84  
    85  -&gt; **Why can't I use environment variables elsewhere?** User variables are
    86  the single source of configurable input to a template. We felt that having
    87  environment variables used *anywhere* in a template would confuse the user
    88  about the possible inputs to a template. By allowing environment variables
    89  only within default values for user variables, user variables remain as the
    90  single source of input to a template that a user can easily discover using
    91  `packer inspect`.
    92  
    93  -&gt; **Why can't I use `~` for home variable?** `~` is an special variable
    94  that is evaluated by shell during a variable expansion. As packer doesn't run
    95  inside a shell, it won't expand `~`.
    96  
    97  ## Setting Variables
    98  
    99  Now that we covered how to define and use variables within a template, the next
   100  important point is how to actually set these variables. Packer exposes two
   101  methods for setting variables: from the command line or from a file.
   102  
   103  ### From the Command Line
   104  
   105  To set variables from the command line, the `-var` flag is used as a parameter
   106  to `packer build` (and some other commands). Continuing our example above, we
   107  could build our template using the command below. The command is split across
   108  multiple lines for readability, but can of course be a single line.
   109  
   110  ``` {.text}
   111  $ packer build \
   112      -var 'aws_access_key=foo' \
   113      -var 'aws_secret_key=bar' \
   114      template.json
   115  ```
   116  
   117  As you can see, the `-var` flag can be specified multiple times in order to set
   118  multiple variables. Also, variables set later on the command-line override
   119  earlier set variables if it has already been set.
   120  
   121  Finally, variables set from the command-line override all other methods of
   122  setting variables. So if you specify a variable in a file (the next method
   123  shown), you can override it using the command-line.
   124  
   125  ### From a File
   126  
   127  Variables can also be set from an external JSON file. The `-var-file` flag reads
   128  a file containing a basic key/value mapping of variables to values and sets
   129  those variables. The JSON file is simple:
   130  
   131  ``` {.javascript}
   132  {
   133    "aws_access_key": "foo",
   134    "aws_secret_key": "bar"
   135  }
   136  ```
   137  
   138  It is a single JSON object where the keys are variables and the values are the
   139  variable values. Assuming this file is in `variables.json`, we can build our
   140  template using the following command:
   141  
   142  ``` {.text}
   143  $ packer build -var-file=variables.json template.json
   144  ```
   145  
   146  The `-var-file` flag can be specified multiple times and variables from multiple
   147  files will be read and applied. As you'd expect, variables read from files
   148  specified later override a variable set earlier if it has already been set.
   149  
   150  And as mentioned above, no matter where a `-var-file` is specified, a `-var`
   151  flag on the command line will always override any variables from a file.
   152  
   153  # Recipes
   154  
   155  ## Making a provisioner step conditional on the value of a variable
   156  
   157  There is no specific syntax in Packer templates for making a provisioner
   158  step conditional, depending on the value of a variable. However, you may
   159  be able to do this by referencing the variable within a command that
   160  you execute. For example, here is how to make a `shell-local`
   161  provisioner only run if the `do_nexpose_scan` variable is non-empty.
   162  
   163  ``` {.javascript}
   164  {
   165    "type": "shell-local",
   166    "command": "if [ ! -z \"{{user `do_nexpose_scan`}}\" ]; then python -u trigger_nexpose_scan.py; fi"
   167  }
   168  ```
   169  
   170  ## Using HOME Variable
   171  
   172  In order to use `$HOME` variable, you can create a `home` variable in packer:
   173  
   174  ``` {.javascript}
   175  "variables" {
   176    "home": "{{env `HOME`}}"
   177  }
   178  ```
   179  
   180  And this will be available to be used in the rest of the template, ie:
   181  
   182  ``` {.javascript}
   183  {
   184    "builders": [{
   185      "type":"google",
   186      "account_file": "{{ user `home` }}/.secrets/gcp-{{ user `env` }}.json"
   187    }]
   188  }
   189  ```