github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/website/source/docs/provisioners/shell.html.md (about)

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