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