github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/website/source/docs/templates/provisioners.html.markdown (about)

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