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