github.com/ttysteale/packer@v0.8.2-0.20150708160520-e5f8ea386ed8/website/source/docs/provisioners/shell.html.markdown (about)

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