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