github.com/supr/packer@v0.3.10-0.20131015195147-7b09e24ac3c1/website/source/docs/templates/provisioners.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  ---
     4  
     5  # Templates: Provisioners
     6  
     7  Within the template, the provisioners section contains an array of all the
     8  provisioners that Packer should use to install and configure software within
     9  running machines prior to turning them into machine images.
    10  
    11  Provisioners are _optional_. If no provisioners are defined within a template,
    12  then no software other than the defaults will be installed within the
    13  resulting machine images. This is not typical, however, since much of the
    14  value of Packer is to produce multiple identical images
    15  of pre-configured software.
    16  
    17  This documentation page will cover how to configure a provisioner in a template.
    18  The specific configuration options available for each provisioner, however,
    19  must be referenced from the documentation for that specific provisioner.
    20  
    21  Within a template, a section of provisioner definitions looks like this:
    22  
    23  <pre class="prettyprint">
    24  {
    25    "provisioners": [
    26      ... one or more provisioner definitions here ...
    27    ]
    28  }
    29  </pre>
    30  
    31  For each of the definitions, Packer will run the provisioner for each
    32  of the configured builds. The provisioners will be run in the order
    33  they are defined within the template.
    34  
    35  ## Provisioner Definition
    36  
    37  A provisioner definition is a JSON object that must contain at least
    38  the `type` key. This key specifies the name of the provisioner to use.
    39  Additional keys within the object are used to configure the provisioner,
    40  with the exception of a handful of special keys, covered later.
    41  
    42  As an example, the "shell" provisioner requires a key such as `script`
    43  which specifies a path to a shell script to execute within the machines
    44  being created.
    45  
    46  An example provisioner definition is shown below, configuring the shell
    47  provisioner to run a local script within the machines:
    48  
    49  <pre class="prettyprint">
    50  {
    51    "type": "shell",
    52    "script": "script.sh"
    53  }
    54  </pre>
    55  
    56  ## Run on Specific Builds
    57  
    58  You can use the `only` or `except` configurations to run a provisioner
    59  only with specific builds. These two configurations do what you expect:
    60  `only` will only run the provisioner on the specified builds and
    61  `except` will run the provisioner on anything other than the specified
    62  builds.
    63  
    64  An example of `only` being used is shown below, but the usage of `except`
    65  is effectively the same:
    66  
    67  <pre class="prettyprint">
    68  {
    69    "type": "shell",
    70    "script": "script.sh",
    71    "only": ["virtualbox"]
    72  }
    73  </pre>
    74  
    75  The values within `only` or `except` are _build names_, not builder
    76  types. If you recall, build names by default are just their builder type,
    77  but if you specify a custom `name` parameter, then you should use that
    78  as the value instead of the type.
    79  
    80  ## Build-Specific Overrides
    81  
    82  While the goal of Packer is to produce identical machine images, it
    83  sometimes requires periods of time where the machines are different before
    84  they eventually converge to be identical. In these cases, different configurations
    85  for provisioners may be necessary depending on the build. This can be done
    86  using build-specific overrides.
    87  
    88  An example of where this might be necessary is when building both an EC2 AMI
    89  and a VMware machine. The source EC2 AMI may setup a user with administrative
    90  privileges by default, whereas the VMware machine doesn't have these privileges.
    91  In this case, the shell script may need to be executed differently. Of course,
    92  the goal is that hopefully the shell script converges these two images to be
    93  identical. However, they may initially need to be run differently.
    94  
    95  This example is shown below:
    96  
    97  <pre class="prettyprint">
    98  {
    99    "type": "shell",
   100    "script": "script.sh",
   101  
   102    "override": {
   103      "vmware": {
   104        "execute_command": "echo 'password' | sudo -S bash {{.Path}}"
   105      }
   106    }
   107  }
   108  </pre>
   109  
   110  As you can see, the `override` key is used. The value of this key is another
   111  JSON object where the key is the name of a [builder definition](/docs/templates/builders.html).
   112  The value of this is in turn another JSON object. This JSON object simply
   113  contains the provisioner configuration as normal. This configuration is merged
   114  into the default provisioner configuration.