github.com/jerryclinesmith/packer@v0.3.7/website/source/docs/provisioners/shell.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Shell Provisioner"
     4  ---
     5  
     6  # Shell Provisioner
     7  
     8  Type: `shell`
     9  
    10  The shell provisioner provisions machines built by Packer using shell scripts.
    11  Shell provisioning is the easiest way to get software installed and configured
    12  on a machine.
    13  
    14  ## Basic Example
    15  
    16  The example below is fully functional.
    17  
    18  <pre class="prettyprint">
    19  {
    20    "type": "shell",
    21    "inline": ["echo foo"]
    22  }
    23  </pre>
    24  
    25  ## Configuration Reference
    26  
    27  The reference of available configuration options is listed below. The only
    28  required element is either "inline" or "script". Every other option is optional.
    29  
    30  Exactly _one_ of the following is required:
    31  
    32  * `inline` (array of strings) - This is an array of commands to execute.
    33    The commands are concatenated by newlines and turned into a single file,
    34    so they are all executed within the same context. This allows you to
    35    change directories in one command and use something in the directory in
    36    the next and so on. Inline scripts are the easiest way to pull of simple
    37    tasks within the machine.
    38  
    39  * `script` (string) - The path to a script to upload and execute in the machine.
    40    This path can be absolute or relative. If it is relative, it is relative
    41    to the working directory when Packer is executed.
    42  
    43  * `scripts` (array of strings) - An array of scripts to execute. The scripts
    44    will be uploaded and executed in the order specified. Each script is executed
    45    in isolation, so state such as variables from one script won't carry on to
    46    the next.
    47  
    48  Optional parameters:
    49  
    50  * `environment_vars` (array of strings) - An array of key/value pairs
    51    to inject prior to the execute_command. The format should be
    52    `key=value`. Packer injects some environmental variables by default
    53    into the environment, as well, which are covered in the section below.
    54  
    55  * `execute_command` (string) - The command to use to execute the script.
    56    By default this is `chmod +x {{ .Path }}; {{ .Vars }} {{ .Path }}`. The value of this is
    57    treated as [configuration template](/docs/templates/configuration-templates.html). There are two available variables: `Path`, which is
    58    the path to the script to run, and `Vars`, which is the list of
    59    `environment_vars`, if configured.
    60  
    61  * `inline_shebang` (string) - The
    62    [shebang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29) value to use when
    63    running commands specified by `inline`. By default, this is `/bin/sh`.
    64    If you're not using `inline`, then this configuration has no effect.
    65  
    66  * `remote_path` (string) - The path where the script will be uploaded to
    67    in the machine. This defaults to "/tmp/script.sh". This value must be
    68    a writable location and any parent directories must already exist.
    69  
    70  * `start_retry_timeout` (string) - The amount of time to attempt to
    71    _start_ the remote process. By default this is "5m" or 5 minutes. This
    72    setting exists in order to deal with times when SSH may restart, such as
    73    a system reboot. Set this to a higher value if reboots take a longer
    74    amount of time.
    75  
    76  ## Execute Command Example
    77  
    78  To many new users, the `execute_command` is puzzling. However, it provides
    79  an important function: customization of how the command is executed. The
    80  most common use case for this is dealing with **sudo password prompts**.
    81  
    82  For example, if the default user of an installed operating system is "packer"
    83  and has the password "packer" for sudo usage, then you'll likely want to
    84  change `execute_command` to be:
    85  
    86  ```
    87  "echo 'packer' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'"
    88  ```
    89  
    90  The `-S` flag tells `sudo` to read the password from stdin, which in this
    91  case is being piped in with the value of "packer". The `-E` flag tells `sudo`
    92  to preserve the environment, allowing our environmental variables to work
    93  within the script.
    94  
    95  By setting the `execute_command` to this, your script(s) can run with
    96  root privileges without worrying about password prompts.
    97  
    98  ## Default Environmental Variables
    99  
   100  In addition to being able to specify custom environmental variables using
   101  the `environmental_vars` configuration, the provisioner automatically
   102  defines certain commonly useful environmental variables:
   103  
   104  * `PACKER_BUILD_NAME` is set to the name of the build that Packer is running.
   105    This is most useful when Packer is making multiple builds and you want to
   106    distinguish them slightly from a common provisioning script.
   107  
   108  * `PACKER_BUILDER_TYPE` is the type of the builder that was used to create
   109    the machine that the script is running on. This is useful if you want to
   110    run only certain parts of the script on systems built with certain builders.
   111  
   112  ## Handling Reboots
   113  
   114  Provisioning sometimes involves restarts, usually when updating the operating
   115  system. Packer is able to tolerate restarts via the shell provisioner.
   116  
   117  Packer handles this by retrying to start scripts for a period of time
   118  before failing. This allows time for the machine to start up and be ready
   119  to run scripts. The amount of time the provisioner will wait is configured
   120  using `start_retry_timeout`, which defaults to a few minutes.
   121  
   122  Sometimes, when executing a command like `reboot`, the shell script will
   123  return and Packer will start executing the next one before SSH actually
   124  quits and the machine restarts. For this, put a long `sleep` after the
   125  reboot so that SSH will eventually be killed automatically:
   126  
   127  ```
   128  reboot
   129  sleep 60
   130  ```
   131  
   132  Some OS configurations don't properly kill all network connections on
   133  reboot, causing the provisioner to hang despite a reboot occuring.
   134  In this case, make sure you shut down the network interfaces
   135  on reboot or in your shell script. For example, on Gentoo:
   136  
   137  ```
   138  /etc/init.d/net.eth0 stop
   139  ```
   140  
   141  ## Troubleshooting
   142  
   143  *My shell script doesn't work correctly on Ubuntu*
   144  
   145  * On Ubuntu the /bin/sh shell is
   146  [dash](http://en.wikipedia.org/wiki/Debian_Almquist_shell). If your script has
   147  [bash](http://en.wikipedia.org/wiki/Bash_(Unix_shell\)) specific commands in it
   148  then put `#!/bin/bash` at the top of your script. Differences
   149  between dash and bash can be found on the [DashAsBinSh](https://wiki.ubuntu.com/DashAsBinSh) Ubuntu wiki page.
   150  
   151  *My shell works when I login but fails with the shell provisioner*
   152  
   153  * See the above tip. More than likely your login shell is using /bin/bash
   154  while the provisioner is using /bin/sh.
   155  
   156  *My installs hang when using `apt-get` or `yum`*
   157  
   158  * Make sure you add a "-y" to the command to prevent it from requiring
   159  user input before proceeding.
   160  
   161  *How do I tell what my shell script is doing?*
   162  
   163  * Adding a `-x` flag to the shebang at the top of the script (`#!/bin/sh -x`)
   164  will echo the script statements as it is executing.
   165  
   166  *My builds don't always work the same*
   167  
   168  * Some distributions start the SSH daemon before other core services which
   169  can create race conditions. Your first provisoner can tell the machine to
   170  wait until it completely boots.
   171  
   172  <pre class="prettyprint">
   173      {
   174         "type": "shell",
   175         "inline": [ "sleep 10" ]
   176      }
   177  </pre>