github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/website/source/docs/templates/user-variables.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  page_title: "User Variables in Templates"
     4  ---
     5  
     6  # User Variables
     7  
     8  User variables allow your templates to be further configured with variables
     9  from the command-line, environmental variables, or files. This lets you
    10  parameterize your templates so that you can keep secret tokens,
    11  environment-specific data, and other types of information out of your
    12  templates. This maximizes the portablility and shareability of the template.
    13  
    14  Using user variables expects you know how
    15  [configuration templates](/docs/templates/configuration-templates.html) work.
    16  If you don't know how configuration templates work yet, please read that
    17  page first.
    18  
    19  ## Usage
    20  
    21  User variables must first be defined in a `variables` section within your
    22  template. Even if you want a variable to default to an empty string, it
    23  must be defined. This explicitivity makes it easy for newcomers to your
    24  template to understand what can be modified using variables in your template.
    25  
    26  The `variables` section is a simple key/value mapping of the variable
    27  name to a default value. A default value can be the empty string. An
    28  example is shown below:
    29  
    30  <pre class="prettyprint">
    31  {
    32    "variables": {
    33      "aws_access_key": "",
    34      "aws_secret_key": ""
    35    },
    36  
    37    "builders": [{
    38      "type": "amazon-ebs",
    39      "access_key": "{{user `aws_access_key`}}",
    40      "secret_key": "{{user `aws_secret_key`}}",
    41      ...
    42    }]
    43  }
    44  </pre>
    45  
    46  In the above example, the template defines two variables: `aws_access_key` and
    47  `aws_secret_key`. They default to empty values.
    48  Later, the variables are used within the builder we defined in order to
    49  configure the actual keys for the Amazon builder.
    50  
    51  Using the variables is extremely easy. Variables are used by calling
    52  the user function in the form of <code>{{user &#96;variable&#96;}}</code>.
    53  This function can be used in _any string_ within the template, in
    54  builders, provisioners, _anything_. The user variable is available globally
    55  within the template.
    56  
    57  ## Setting Variables
    58  
    59  Now that we covered how to define and use variables within a template,
    60  the next important point is how to actually set these variables. Packer
    61  exposes two methods for setting variables: from the command line or
    62  from a file.
    63  
    64  ### From the Command Line
    65  
    66  To set variables from the command line, the `-var` flag is used as
    67  a parameter to `packer build` (and some other commands). Continuing our example
    68  above, we could build our template using the command below. The command
    69  is split across multiple lines for readability, but can of course be a single
    70  line.
    71  
    72  ```
    73  $ packer build \
    74      -var 'aws_access_key=foo' \
    75      -var 'aws_secret_key=bar' \
    76      template.json
    77  ```
    78  
    79  As you can see, the `-var` flag can be specified multiple times in order
    80  to set multiple variables. Also, variables set later on the command-line
    81  override earlier set variables if it has already been set.
    82  
    83  Finally, variables set from the command-line override all other methods
    84  of setting variables. So if you specify a variable in a file (the next
    85  method shown), you can override it using the command-line.
    86  
    87  ### From a File
    88  
    89  Variables can also be set from an external JSON file. The `-var-file`
    90  flag reads a file containing a basic key/value mapping of variables to
    91  values and sets those variables. The JSON file is simple:
    92  
    93  <pre class="prettyprint">
    94  {
    95    "aws_access_key": "foo",
    96    "aws_secret_key": "bar"
    97  }
    98  </pre>
    99  
   100  It is a single JSON object where the keys are variables and the values are
   101  the variable values. Assuming this file is in `variables.json`, we can
   102  build our template using the following command:
   103  
   104  ```
   105  $ packer build -var-file=variables.json template.json
   106  ```
   107  
   108  The `-var-file` flag can be specified multiple times and variables from
   109  multiple files will be read and applied. As you'd expect, variables read
   110  from files specified later override a variable set earlier if it has
   111  already been set.
   112  
   113  And as mentioned above, no matter where a `-var-file` is specified, a
   114  `-var` flag on the command line will always override any variables from
   115  a file.