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