github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/website/source/docs/provisioners/shell.html.markdown (about) 1 --- 2 layout: "docs" 3 --- 4 5 # Shell Provisioner 6 7 Type: `shell` 8 9 The shell provisioner provisions machines built by Packer using shell scripts. 10 Shell provisioning is the easiest way to get software installed and configured 11 on a machine. 12 13 ## Basic Example 14 15 The example below is fully functional. 16 17 <pre class="prettyprint"> 18 { 19 "type": "shell", 20 "inline": ["echo foo"] 21 } 22 </pre> 23 24 ## Configuration Reference 25 26 The reference of available configuration options is listed below. The only 27 required element is either "inline" or "script". Every other option is optional. 28 29 Exactly _one_ of the following is required: 30 31 * `inline` (array of strings) - This is an array of commands to execute. 32 The commands are concatenated by newlines and turned into a single file, 33 so they are all executed within the same context. This allows you to 34 change directories in one command and use something in the directory in 35 the next and so on. Inline scripts are the easiest way to pull of simple 36 tasks within the machine. 37 38 * `script` (string) - The path to a script to upload and execute in the machine. 39 This path can be absolute or relative. If it is relative, it is relative 40 to the working directory when Packer is executed. 41 42 * `scripts` (array of strings) - An array of scripts to execute. The scripts 43 will be uploaded and executed in the order specified. Each script is executed 44 in isolation, so state such as variables from one script won't carry on to 45 the next. 46 47 Optional parameters: 48 49 * `environment_vars` (array of strings) - An array of key/value pairs 50 to inject prior to the execute_command. The format should be 51 `key=value`. Packer injects some environmental variables by default 52 into the environment, as well, which are covered in the section below. 53 54 * `execute_command` (string) - The command to use to execute the script. 55 By default this is `chmod +x {{ .Path }}; {{ .Vars }} {{ .Path }}`. The value of this is 56 treated as [configuration template](/docs/templates/configuration-templates.html). There are two available variables: `Path`, which is 57 the path to the script to run, and `Vars`, which is the list of 58 `environment_vars`, if configured. 59 60 * `inline_shebang` (string) - The 61 [shebang](http://en.wikipedia.org/wiki/Shebang_(Unix)) value to use when 62 running commands specified by `inline`. By default, this is `/bin/sh`. 63 If you're not using `inline`, then this configuration has no effect. 64 65 * `remote_path` (string) - The path where the script will be uploaded to 66 in the machine. This defaults to "/tmp/script.sh". This value must be 67 a writable location and any parent directories must already exist. 68 69 * `start_retry_timeout` (string) - The amount of time to attempt to 70 _start_ the remote process. By default this is "5m" or 5 minutes. This 71 setting exists in order to deal with times when SSH may restart, such as 72 a system reboot. Set this to a higher value if reboots take a longer 73 amount of time. 74 75 ## Execute Command Example 76 77 To many new users, the `execute_command` is puzzling. However, it provides 78 an important function: customization of how the command is executed. The 79 most common use case for this is dealing with **sudo password prompts**. 80 81 For example, if the default user of an installed operating system is "packer" 82 and has the password "packer" for sudo usage, then you'll likely want to 83 change `execute_command` to be: 84 85 ``` 86 "echo 'packer' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'" 87 ``` 88 89 The `-S` flag tells `sudo` to read the password from stdin, which in this 90 case is being piped in with the value of "packer". The `-E` flag tells `sudo` 91 to preserve the environment, allowing our environmental variables to work 92 within the script. 93 94 By setting the `execute_command` to this, your script(s) can run with 95 root privileges without worrying about password prompts. 96 97 ## Default Environmental Variables 98 99 In addition to being able to specify custom environmental variables using 100 the `environmental_vars` configuration, the provisioner automatically 101 defines certain commonly useful environmental variables: 102 103 * `PACKER_BUILD_NAME` is set to the name of the build that Packer is running. 104 This is most useful when Packer is making multiple builds and you want to 105 distinguish them slightly from a common provisioning script. 106 107 * `PACKER_BUILDER_TYPE` is the type of the builder that was used to create 108 the machine that the script is running on. This is useful if you want to 109 run only certain parts of the script on systems built with certain builders. 110 111 ## Handling Reboots 112 113 Provisioning sometimes involves restarts, usually when updating the operating 114 system. Packer is able to tolerate restarts via the shell provisioner. 115 116 Packer handles this by retrying to start scripts for a period of time 117 before failing. This allows time for the machine to start up and be ready 118 to run scripts. The amount of time the provisioner will wait is configured 119 using `start_retry_timeout`, which defaults to a few minutes. 120 121 Sometimes, when executing a command like `reboot`, the shell script will 122 return and Packer will start executing the next one before SSH actually 123 quits and the machine restarts. For this, put a long `sleep` after the 124 reboot so that SSH will eventually be killed automatically: 125 126 ``` 127 reboot 128 sleep 60 129 ```