github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/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` (boolean) - 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 154 [name of the build](/docs/templates/builders.html#named-builds) that Packer is running. 155 This is most useful when Packer is making multiple builds and you want to 156 distinguish them slightly from a common provisioning script. 157 158 - `PACKER_BUILDER_TYPE` is the type of the builder that was used to create the 159 machine that the script is running on. This is useful if you want to run 160 only certain parts of the script on systems built with certain builders. 161 162 - `PACKER_HTTP_ADDR` If using a builder that provides an http server for file 163 transfer (such as hyperv, parallels, qemu, virtualbox, and vmware), this 164 will be set to the address. You can use this address in your provisioner to 165 download large files over http. This may be useful if you're experiencing 166 slower speeds using the default file provisioner. A file provisioner using 167 the `winrm` communicator may experience these types of difficulties. 168 169 ## Handling Reboots 170 171 Provisioning sometimes involves restarts, usually when updating the operating 172 system. Packer is able to tolerate restarts via the shell provisioner. 173 174 Packer handles this by retrying to start scripts for a period of time before 175 failing. This allows time for the machine to start up and be ready to run 176 scripts. The amount of time the provisioner will wait is configured using 177 `start_retry_timeout`, which defaults to a few minutes. 178 179 Sometimes, when executing a command like `reboot`, the shell script will return 180 and Packer will start executing the next one before SSH actually quits and the 181 machine restarts. For this, put use "pause\_before" to make Packer wait before executing the next script: 182 183 ``` json 184 { 185 "type": "shell", 186 "script": "script.sh", 187 "pause_before": "10s" 188 } 189 ``` 190 191 Some OS configurations don't properly kill all network connections on reboot, 192 causing the provisioner to hang despite a reboot occurring. In this case, make 193 sure you shut down the network interfaces on reboot or in your shell script. For 194 example, on Gentoo: 195 196 ``` text 197 /etc/init.d/net.eth0 stop 198 ``` 199 200 ## SSH Agent Forwarding 201 202 Some provisioning requires connecting to remote SSH servers from within the 203 packer instance. The below example is for pulling code from a private git 204 repository utilizing openssh on the client. Make sure you are running 205 `ssh-agent` and add your git repo ssh keys into it using `ssh-add /path/to/key`. 206 When the packer instance needs access to the ssh keys the agent will forward the 207 request back to your `ssh-agent`. 208 209 Note: when provisioning via git you should add the git server keys into the 210 `~/.ssh/known_hosts` file otherwise the git command could hang awaiting input. 211 This can be done by copying the file in via the [file 212 provisioner](/docs/provisioners/file.html) (more secure) or using `ssh-keyscan` 213 to populate the file (less secure). An example of the latter accessing github 214 would be: 215 216 ``` json 217 { 218 "type": "shell", 219 "inline": [ 220 "sudo apt-get install -y git", 221 "ssh-keyscan github.com >> ~/.ssh/known_hosts", 222 "git clone git@github.com:exampleorg/myprivaterepo.git" 223 ] 224 } 225 ``` 226 227 ## Troubleshooting 228 229 *My shell script doesn't work correctly on Ubuntu* 230 231 - On Ubuntu, the `/bin/sh` shell is 232 [dash](https://en.wikipedia.org/wiki/Debian_Almquist_shell). If your script 233 has [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell))-specific 234 commands in it, then put `#!/bin/bash -e` at the top of your script. 235 Differences between dash and bash can be found on the 236 [DashAsBinSh](https://wiki.ubuntu.com/DashAsBinSh) Ubuntu wiki page. 237 238 *My shell works when I login but fails with the shell provisioner* 239 240 - See the above tip. More than likely, your login shell is using `/bin/bash` 241 while the provisioner is using `/bin/sh`. 242 243 *My installs hang when using `apt-get` or `yum`* 244 245 - Make sure you add a `-y` to the command to prevent it from requiring user 246 input before proceeding. 247 248 *How do I tell what my shell script is doing?* 249 250 - Adding a `-x` flag to the shebang at the top of the script (`#!/bin/sh -x`) 251 will echo the script statements as it is executing. 252 253 *My builds don't always work the same* 254 255 - Some distributions start the SSH daemon before other core services which can 256 create race conditions. Your first provisioner can tell the machine to wait 257 until it completely boots. 258 259 ``` json 260 { 261 "type": "shell", 262 "inline": [ "sleep 10" ] 263 } 264 ```