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

     1  ---
     2  description: |
     3    The `provisioners` block contains provisioners that Packer uses to install and configure software on machines. Learn how to configure provisioners in JSON templates.
     4  page_title: provisioners reference for JSON templates
     5  ---
     6  
     7  # `provisioners` reference for JSON templates
     8  
     9  This topic provides reference information about the `provisioners` block in JSON templates for Packer. Refer to the documentation for specific provisioner types to learn about their configuration options.
    10  
    11  `@include 'from-1.5/legacy-json-warning.mdx'`
    12  
    13  ## Description
    14  
    15  The `provisioners` block contains provisioners that Packer uses to install and configure software within running machines prior to turning them into machine images.
    16  
    17  Provisioners are optional. When you omit the `provisioners` block, Packer installs only the default software within the resulting machine images. 
    18  
    19  Use the following syntax to add the `provisioners` block to your JSON template:
    20  
    21  ```json
    22  {
    23    "provisioners": [
    24      // ... one or more provisioner definitions here
    25    ]
    26  }
    27  ```
    28  
    29  For each of the definitions, Packer will run the provisioner for each of the
    30  configured builds. The provisioners will be run in the order they are defined
    31  within the template.
    32  
    33  ## Provisioner Definition
    34  
    35  A provisioner definition is a JSON object that must contain at least the `type`
    36  key. This key specifies the name of the provisioner to use. Additional keys
    37  within the object are used to configure the provisioner, with the exception of
    38  a handful of special keys, covered later.
    39  
    40  As an example, the "shell" provisioner requires a key such as `script` which
    41  specifies a path to a shell script to execute within the machines being
    42  created.
    43  
    44  An example provisioner definition is shown below, configuring the shell
    45  provisioner to run a local script within the machines:
    46  
    47  ```json
    48  {
    49    "type": "shell",
    50    "script": "script.sh"
    51  }
    52  ```
    53  
    54  ## Run on Specific Builds
    55  
    56  You can use the `only` or `except` configurations to run a provisioner only
    57  with specific builds. These two configurations do what you expect: `only` will
    58  only run the provisioner on the specified builds and `except` will run the
    59  provisioner on anything other than the specified builds.
    60  
    61  An example of `only` being used is shown below, but the usage of `except` is
    62  effectively the same:
    63  
    64  ```json
    65  {
    66    "type": "shell",
    67    "script": "script.sh",
    68    "only": ["virtualbox-iso"]
    69  }
    70  ```
    71  
    72  The values within `only` or `except` are _build names_, not builder types. If
    73  you recall, build names by default are just their builder type, but if you
    74  specify a custom `name` parameter, then you should use that as the value
    75  instead of the type.
    76  Values within `except` could also be a _post-processor_ name.
    77  
    78  ## On Error Provisioner
    79  
    80  You can optionally create a single specialized provisioner field called an
    81  `error-cleanup-provisioner`. This provisioner will not run unless the normal
    82  provisioning run fails. If the normal provisioning run does fail, this special
    83  error provisioner will run _before the instance is shut down_. This allows you
    84  to make last minute changes and clean up behaviors that Packer may not be able
    85  to clean up on its own.
    86  
    87  For examples, users may use this provisioner to make sure that the instance is
    88  properly unsubscribed from any services that it connected to during the build
    89  run.
    90  
    91  Toy usage example for the error cleanup script:
    92  
    93  ```json
    94  {
    95    "builders": [
    96      {
    97        "type": "null",
    98        "communicator": "none"
    99      }
   100    ],
   101    "provisioners": [
   102      {
   103        "type": "shell-local",
   104        "inline": ["exit 2"]
   105      }
   106    ],
   107    "error-cleanup-provisioner": {
   108      "type": "shell-local",
   109      "inline": ["echo 'rubber ducky'> ducky.txt"]
   110    }
   111  }
   112  ```
   113  
   114  ## Build-Specific Overrides
   115  
   116  While the goal of Packer is to produce identical machine images, it sometimes
   117  requires periods of time where the machines are different before they
   118  eventually converge to be identical. In these cases, different configurations
   119  for provisioners may be necessary depending on the build. This can be done
   120  using build-specific overrides.
   121  
   122  An example of where this might be necessary is when building both an EC2 AMI
   123  and a VMware machine. The source EC2 AMI may setup a user with administrative
   124  privileges by default, whereas the VMware machine doesn't have these
   125  privileges. In this case, the shell script may need to be executed differently.
   126  Of course, the goal is that hopefully the shell script converges these two
   127  images to be identical. However, they may initially need to be run differently.
   128  
   129  This example is shown below:
   130  
   131  ```json
   132  {
   133    "type": "shell",
   134    "script": "script.sh",
   135    "override": {
   136      "vmware-iso": {
   137        "execute_command": "echo 'password' | sudo -S bash {{.Path}}"
   138      }
   139    }
   140  }
   141  ```
   142  
   143  As you can see, the `override` key is used. The value of this key is another
   144  JSON object where the key is the name of a [builder
   145  definition](/packer/docs/templates/legacy_json_templates/builders). The value of this is in turn
   146  another JSON object. This JSON object simply contains the provisioner
   147  configuration as normal. This configuration is merged into the default
   148  provisioner configuration.
   149  
   150  ## Pausing Before Running
   151  
   152  With certain provisioners it is sometimes desirable to pause for some period of
   153  time before running it. Specifically, in cases where a provisioner reboots the
   154  machine, you may want to wait for some period of time before starting the next
   155  provisioner.
   156  
   157  Every provisioner definition in a Packer template can take a special
   158  configuration `pause_before` that is the amount of time to pause before running
   159  that provisioner. By default, there is no pause. An example is shown below:
   160  
   161  ```json
   162  {
   163    "type": "shell",
   164    "script": "script.sh",
   165    "pause_before": "10s"
   166  }
   167  ```
   168  
   169  For the above provisioner, Packer will wait 10 seconds before uploading and
   170  executing the shell script.
   171  
   172  ## Retry on error
   173  
   174  With certain provisioners it is sometimes desirable to retry when it fails.
   175  Specifically, in cases where the provisioner depends on external processes that are not done yet.
   176  
   177  Every provisioner definition in a Packer template can take a special
   178  configuration `max_retries` that is the maximum number of times a provisioner will retry on error.
   179  By default, there `max_retries` is zero and there is no retry on error. An example is shown below:
   180  
   181  ```json
   182  {
   183    "type": "shell",
   184    "script": "script.sh",
   185    "max_retries": 5
   186  }
   187  ```
   188  
   189  For the above provisioner, Packer will retry maximum five times until stops failing.
   190  If after five retries the provisioner still fails, then the complete build will fail.
   191  
   192  ## Timeout
   193  
   194  Sometimes a command can take much more time than expected
   195  
   196  Every provisioner definition in a Packer template can take a special
   197  configuration `timeout` that is the amount of time to wait before
   198  considering that the provisioner failed. By default, there is no timeout. An
   199  example is shown below:
   200  
   201  ```json
   202  {
   203    "type": "shell",
   204    "script": "script.sh",
   205    "timeout": "5m"
   206  }
   207  ```
   208  
   209  For the above provisioner, Packer will cancel the script if it takes more than
   210  5 minutes.
   211  
   212  Timeout has no effect in debug mode.