github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/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_path` (string) - The filename where the script will be uploaded
    82      to in the machine. This defaults to `/tmp/script_nnn.sh` where "nnn" is
    83      a randomly generated number. This value must be a writable location and
    84      any parent directories must already exist.
    85  
    86  -   `skip_clean` (boolean) - If true, specifies that the helper scripts 
    87      uploaded to the system will not be removed by Packer. This defaults to 
    88      false (clean scripts from the system).
    89  
    90  -   `start_retry_timeout` (string) - The amount of time to attempt to *start*
    91      the remote process. By default this is `5m` or 5 minutes. This setting
    92      exists in order to deal with times when SSH may restart, such as a
    93      system reboot. Set this to a higher value if reboots take a longer amount
    94      of time.
    95  
    96  ## Execute Command Example
    97  
    98  To many new users, the `execute_command` is puzzling. However, it provides an
    99  important function: customization of how the command is executed. The most
   100  common use case for this is dealing with **sudo password prompts**. You may also
   101  need to customize this if you use a non-POSIX shell, such as `tcsh` on FreeBSD.
   102  
   103  ### Sudo Example
   104  
   105  Some operating systems default to a non-root user. For example if you login as
   106  `ubuntu` and can sudo using the password `packer`, then you'll want to change
   107  `execute_command` to be:
   108  
   109  ``` {.text}
   110  "echo 'packer' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'"
   111  ```
   112  
   113  The `-S` flag tells `sudo` to read the password from stdin, which in this case
   114  is being piped in with the value of `packer`. The `-E` flag tells `sudo` to
   115  preserve the environment, allowing our environmental variables to work within
   116  the script.
   117  
   118  By setting the `execute_command` to this, your script(s) can run with root
   119  privileges without worrying about password prompts.
   120  
   121  ### FreeBSD Example
   122  
   123  FreeBSD's default shell is `tcsh`, which deviates from POSIX sematics. In order
   124  for packer to pass environment variables you will need to change the
   125  `execute_command` to:
   126  
   127      chmod +x {{ .Path }}; env {{ .Vars }} {{ .Path }}
   128  
   129  Note the addition of `env` before `{{ .Vars }}`.
   130  
   131  ## Default Environmental Variables
   132  
   133  In addition to being able to specify custom environmental variables using the
   134  `environment_vars` configuration, the provisioner automatically defines certain
   135  commonly useful environmental variables:
   136  
   137  -   `PACKER_BUILD_NAME` is set to the name of the build that Packer is running.
   138      This is most useful when Packer is making multiple builds and you want to
   139      distinguish them slightly from a common provisioning script.
   140  
   141  -   `PACKER_BUILDER_TYPE` is the type of the builder that was used to create the
   142      machine that the script is running on. This is useful if you want to run
   143      only certain parts of the script on systems built with certain builders.
   144  
   145  ## Handling Reboots
   146  
   147  Provisioning sometimes involves restarts, usually when updating the operating
   148  system. Packer is able to tolerate restarts via the shell provisioner.
   149  
   150  Packer handles this by retrying to start scripts for a period of time before
   151  failing. This allows time for the machine to start up and be ready to run
   152  scripts. The amount of time the provisioner will wait is configured using
   153  `start_retry_timeout`, which defaults to a few minutes.
   154  
   155  Sometimes, when executing a command like `reboot`, the shell script will return
   156  and Packer will start executing the next one before SSH actually quits and the
   157  machine restarts. For this, put a long `sleep` after the reboot so that SSH will
   158  eventually be killed automatically:
   159  
   160  ``` {.text}
   161  reboot
   162  sleep 60
   163  ```
   164  
   165  Some OS configurations don't properly kill all network connections on reboot,
   166  causing the provisioner to hang despite a reboot occurring. In this case, make
   167  sure you shut down the network interfaces on reboot or in your shell script. For
   168  example, on Gentoo:
   169  
   170  ``` {.text}
   171  /etc/init.d/net.eth0 stop
   172  ```
   173  
   174  ## SSH Agent Forwarding
   175  
   176  Some provisioning requires connecting to remote SSH servers from within the
   177  packer instance. The below example is for pulling code from a private git
   178  repository utilizing openssh on the client. Make sure you are running
   179  `ssh-agent` and add your git repo ssh keys into it using `ssh-add /path/to/key`.
   180  When the packer instance needs access to the ssh keys the agent will forward the
   181  request back to your `ssh-agent`.
   182  
   183  Note: when provisioning via git you should add the git server keys into the
   184  `~/.ssh/known_hosts` file otherwise the git command could hang awaiting input.
   185  This can be done by copying the file in via the [file
   186  provisioner](/docs/provisioners/file.html) (more secure) or using `ssh-keyscan`
   187  to populate the file (less secure). An example of the latter accessing github
   188  would be:
   189  
   190  { "type": "shell", "inline": \[ "sudo apt-get install -y git", "ssh-keyscan
   191  github.com >> \~/.ssh/known\_hosts", "git clone
   192  git@github.com:exampleorg/myprivaterepo.git" \] }
   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](https://en.wikipedia.org/wiki/Debian_Almquist_shell). If your script
   200      has [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell))-specific commands
   201      in it, then put `#!/bin/bash` at the top of your script. Differences between
   202      dash and bash can be found on the
   203      [DashAsBinSh](https://wiki.ubuntu.com/DashAsBinSh) Ubuntu wiki page.
   204  
   205  *My shell works when I login but fails with the shell provisioner*
   206  
   207  -   See the above tip. More than likely, your login shell is using `/bin/bash`
   208      while the provisioner is using `/bin/sh`.
   209  
   210  *My installs hang when using `apt-get` or `yum`*
   211  
   212  -   Make sure you add a `-y` to the command to prevent it from requiring user
   213      input before proceeding.
   214  
   215  *How do I tell what my shell script is doing?*
   216  
   217  -   Adding a `-x` flag to the shebang at the top of the script (`#!/bin/sh -x`)
   218      will echo the script statements as it is executing.
   219  
   220  *My builds don't always work the same*
   221  
   222  -   Some distributions start the SSH daemon before other core services which can
   223      create race conditions. Your first provisioner can tell the machine to wait
   224      until it completely boots.
   225  
   226  ``` {.javascript}
   227  {
   228    "type": "shell",
   229    "inline": [ "sleep 10" ]
   230  }
   231  ```