github.com/raghuse92/packer@v1.3.2/website/source/docs/provisioners/shell-local.html.md (about) 1 --- 2 description: | 3 shell-local will run a shell script of your choosing on the machine where Packer 4 is being run - in other words, shell-local will run the shell script on your 5 build server, or your desktop, etc., rather than the remote/guest machine being 6 provisioned by Packer. 7 layout: docs 8 page_title: 'Shell (Local) - Provisioners' 9 sidebar_current: 'docs-provisioners-shell-local' 10 --- 11 12 # Local Shell Provisioner 13 14 Type: `shell-local` 15 16 shell-local will run a shell script of your choosing on the machine where Packer 17 is being run - in other words, shell-local will run the shell script on your 18 build server, or your desktop, etc., rather than the remote/guest machine being 19 provisioned by Packer. 20 21 The [remote shell](/docs/provisioners/shell.html) provisioner executes 22 shell scripts on a remote machine. 23 24 ## Basic Example 25 26 The example below is fully functional. 27 28 ``` json 29 { 30 "type": "shell-local", 31 "command": "echo foo" 32 } 33 ``` 34 35 ## Configuration Reference 36 37 The reference of available configuration options is listed below. The only 38 required element is "command". 39 40 Exactly *one* of the following is required: 41 42 - `command` (string) - This is a single command to execute. It will be written 43 to a temporary file and run using the `execute_command` call below. 44 If you are building a windows vm on AWS, Azure or Google Compute and would 45 like to access the generated password that Packer uses to connect to the 46 instance via WinRM, you can use the template variable `{{.WinRMPassword}}` 47 to set this as an environment variable. 48 49 - `inline` (array of strings) - This is an array of commands to execute. The 50 commands are concatenated by newlines and turned into a single file, so they 51 are all executed within the same context. This allows you to change 52 directories in one command and use something in the directory in the next 53 and so on. Inline scripts are the easiest way to pull off simple tasks 54 within the machine. 55 56 - `script` (string) - The path to a script to execute. This path can be 57 absolute or relative. If it is relative, it is relative to the working 58 directory when Packer is executed. 59 60 - `scripts` (array of strings) - An array of scripts to execute. The scripts 61 will be executed in the order specified. Each script is executed in 62 isolation, so state such as variables from one script won't carry on to the 63 next. 64 65 Optional parameters: 66 67 - `environment_vars` (array of strings) - An array of key/value pairs to 68 inject prior to the `execute_command`. The format should be `key=value`. 69 Packer injects some environmental variables by default into the environment, 70 as well, which are covered in the section below. If you are building a 71 windows vm on AWS, Azure or Google Compute and would like to access the 72 generated password that Packer uses to connect to the instance via WinRM, 73 you can use the template variable `{{.WinRMPassword}}` to set this as an 74 environment variable. For example: 75 `"environment_vars": "WINRMPASS={{.WinRMPassword}}"` 76 77 - `execute_command` (array of strings) - The command used to execute the script. 78 By default this is `["/bin/sh", "-c", "{{.Vars}}", "{{.Script}}"]` 79 on unix and `["cmd", "/c", "{{.Vars}}", "{{.Script}}"]` on windows. 80 This is treated as a [template engine](/docs/templates/engine.html). 81 There are two available variables: `Script`, which is the path to the script 82 to run, and `Vars`, which is the list of `environment_vars`, if configured. 83 84 If you choose to set this option, make sure that the first element in the 85 array is the shell program you want to use (for example, "sh"), and a later 86 element in the array must be `{{.Script}}`. 87 88 This option provides you a great deal of flexibility. You may choose to 89 provide your own shell program, for example "/usr/local/bin/zsh" or even 90 "powershell.exe". However, with great power comes great responsibility - 91 these commands are not officially supported and things like environment 92 variables may not work if you use a different shell than the default. 93 94 For backwards compatibility, you may also use {{.Command}}, but it is 95 decoded the same way as {{.Script}}. We recommend using {{.Script}} for the 96 sake of clarity, as even when you set only a single `command` to run, 97 Packer writes it to a temporary file and then runs it as a script. 98 99 If you are building a windows vm on AWS, Azure or Google Compute and would 100 like to access the generated password that Packer uses to connect to the 101 instance via WinRM, you can use the template variable `{{.WinRMPassword}}` 102 to set this as an environment variable. 103 104 - `inline_shebang` (string) - The 105 [shebang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29) value to use when 106 running commands specified by `inline`. By default, this is `/bin/sh -e`. If 107 you're not using `inline`, then this configuration has no effect. 108 **Important:** If you customize this, be sure to include something like the 109 `-e` flag, otherwise individual steps failing won't fail the provisioner. 110 111 - `only_on` (array of strings) - This is an array of 112 [runtime operating systems](https://golang.org/doc/install/source#environment) 113 where `shell-local` will execute. This allows you to execute `shell-local` 114 *only* on specific operating systems. By default, shell-local will always run 115 if `only_on` is not set." 116 117 - `use_linux_pathing` (bool) - This is only relevant to windows hosts. If you 118 are running Packer in a Windows environment with the Windows Subsystem for 119 Linux feature enabled, and would like to invoke a bash script rather than 120 invoking a Cmd script, you'll need to set this flag to true; it tells Packer 121 to use the linux subsystem path for your script rather than the Windows path. 122 (e.g. /mnt/c/path/to/your/file instead of C:/path/to/your/file). Please see 123 the example below for more guidance on how to use this feature. If you are 124 not on a Windows host, or you do not intend to use the shell-local 125 provisioner to run a bash script, please ignore this option. 126 127 ## Execute Command 128 129 To many new users, the `execute_command` is puzzling. However, it provides an 130 important function: customization of how the command is executed. The most 131 common use case for this is dealing with **sudo password prompts**. You may also 132 need to customize this if you use a non-POSIX shell, such as `tcsh` on FreeBSD. 133 134 ### The Windows Linux Subsystem 135 136 The shell-local provisioner was designed with the idea of allowing you to run 137 commands in your local operating system's native shell. For Windows, we've 138 assumed in our defaults that this is Cmd. However, it is possible to run a 139 bash script as part of the Windows Linux Subsystem from the shell-local 140 provisioner, by modifying the `execute_command` and the `use_linux_pathing` 141 options in the provisioner config. 142 143 The example below is a fully functional test config. 144 145 One limitation of this offering is that "inline" and "command" options are not 146 available to you; please limit yourself to using the "script" or "scripts" 147 options instead. 148 149 Please note that the WSL is a beta feature, and this tool is not guaranteed to 150 work as you expect it to. 151 152 ``` 153 { 154 "builders": [ 155 { 156 "type": "null", 157 "communicator": "none" 158 } 159 ], 160 "provisioners": [ 161 { 162 "type": "shell-local", 163 "environment_vars": ["PROVISIONERTEST=ProvisionerTest1"], 164 "execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"], 165 "use_linux_pathing": true, 166 "scripts": ["C:/Users/me/scripts/example_bash.sh"] 167 }, 168 { 169 "type": "shell-local", 170 "environment_vars": ["PROVISIONERTEST=ProvisionerTest2"], 171 "execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"], 172 "use_linux_pathing": true, 173 "script": "C:/Users/me/scripts/example_bash.sh" 174 } 175 ] 176 } 177 ``` 178 179 ## Default Environmental Variables 180 181 In addition to being able to specify custom environmental variables using the 182 `environment_vars` configuration, the provisioner automatically defines certain 183 commonly useful environmental variables: 184 185 - `PACKER_BUILD_NAME` is set to the name of the build that Packer is running. 186 This is most useful when Packer is making multiple builds and you want to 187 distinguish them slightly from a common provisioning script. 188 189 - `PACKER_BUILDER_TYPE` is the type of the builder that was used to create the 190 machine that the script is running on. This is useful if you want to run 191 only certain parts of the script on systems built with certain builders. 192 193 - `PACKER_HTTP_ADDR` If using a builder that provides an http server for file 194 transfer (such as hyperv, parallels, qemu, virtualbox, and vmware), this 195 will be set to the address. You can use this address in your provisioner to 196 download large files over http. This may be useful if you're experiencing 197 slower speeds using the default file provisioner. A file provisioner using 198 the `winrm` communicator may experience these types of difficulties. 199 200 ## Safely Writing A Script 201 202 Whether you use the `inline` option, or pass it a direct `script` or `scripts`, 203 it is important to understand a few things about how the shell-local 204 provisioner works to run it safely and easily. This understanding will save 205 you much time in the process. 206 207 ### Once Per Builder 208 209 The `shell-local` script(s) you pass are run once per builder. That means that 210 if you have an `amazon-ebs` builder and a `docker` builder, your script will be 211 run twice. If you have 3 builders, it will run 3 times, once for each builder. 212 213 ### Always Exit Intentionally 214 215 If any provisioner fails, the `packer build` stops and all interim artifacts 216 are cleaned up. 217 218 For a shell script, that means the script **must** exit with a zero code. You 219 *must* be extra careful to `exit 0` when necessary. 220 221 222 ## Usage Examples: 223 224 Example of running a .cmd file on windows: 225 226 ``` 227 { 228 "type": "shell-local", 229 "environment_vars": ["SHELLLOCALTEST=ShellTest1"], 230 "scripts": ["./scripts/test_cmd.cmd"] 231 }, 232 ``` 233 234 Contents of "test_cmd.cmd": 235 236 ``` 237 echo %SHELLLOCALTEST% 238 ``` 239 240 Example of running an inline command on windows: 241 Required customization: tempfile_extension 242 243 ``` 244 { 245 "type": "shell-local", 246 "environment_vars": ["SHELLLOCALTEST=ShellTest2"], 247 "tempfile_extension": ".cmd", 248 "inline": ["echo %SHELLLOCALTEST%"] 249 }, 250 ``` 251 252 Example of running a bash command on windows using WSL: 253 Required customizations: use_linux_pathing and execute_command 254 255 ``` 256 { 257 "type": "shell-local", 258 "environment_vars": ["SHELLLOCALTEST=ShellTest3"], 259 "execute_command": ["bash", "-c", "{{.Vars}} {{.Script}}"], 260 "use_linux_pathing": true, 261 "script": "./scripts/example_bash.sh" 262 } 263 ``` 264 265 Contents of "example_bash.sh": 266 267 ``` 268 #!/bin/bash 269 echo $SHELLLOCALTEST 270 ``` 271 272 Example of running a powershell script on windows: 273 Required customizations: env_var_format and execute_command 274 275 ``` 276 277 { 278 "type": "shell-local", 279 "environment_vars": ["SHELLLOCALTEST=ShellTest4"], 280 "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"], 281 "env_var_format": "$env:%s=\"%s\"; ", 282 } 283 ``` 284 285 Example of running a powershell script on windows as "inline": 286 Required customizations: env_var_format, tempfile_extension, and execute_command 287 288 ``` 289 { 290 "type": "shell-local", 291 "tempfile_extension": ".ps1", 292 "environment_vars": ["SHELLLOCALTEST=ShellTest5"], 293 "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"], 294 "env_var_format": "$env:%s=\"%s\"; ", 295 "inline": ["write-output $env:SHELLLOCALTEST"] 296 } 297 ``` 298 299 300 Example of running a bash script on linux: 301 302 ``` 303 { 304 "type": "shell-local", 305 "environment_vars": ["PROVISIONERTEST=ProvisionerTest1"], 306 "scripts": ["./scripts/dummy_bash.sh"] 307 } 308 ``` 309 310 Example of running a bash "inline" on linux: 311 312 ``` 313 { 314 "type": "shell-local", 315 "environment_vars": ["PROVISIONERTEST=ProvisionerTest2"], 316 "inline": ["echo hello", 317 "echo $PROVISIONERTEST"] 318 } 319 ``` 320