2013-09-03 18:08:28 +00:00
|
|
|
---
|
2013-09-06 16:50:43 +00:00
|
|
|
page_title: "Shell Scripts - Provisioning"
|
2013-09-03 18:08:28 +00:00
|
|
|
sidebar_current: "provisioning-shell"
|
|
|
|
---
|
|
|
|
|
|
|
|
# Shell Provisioner
|
|
|
|
|
2013-09-06 16:50:43 +00:00
|
|
|
**Provisioner name: `"shell"`**
|
2013-09-03 18:08:28 +00:00
|
|
|
|
2014-04-12 15:51:29 +00:00
|
|
|
The shell provisioner allows you to upload and execute a script within
|
|
|
|
the guest machine.
|
2013-09-03 18:08:28 +00:00
|
|
|
|
|
|
|
Shell provisioning is ideal for users new to Vagrant who want to get up
|
|
|
|
and running quickly and provides a strong alternative for users who aren't
|
|
|
|
comfortable with a full configuration management system such as Chef or
|
|
|
|
Puppet.
|
|
|
|
|
2014-04-12 15:51:29 +00:00
|
|
|
For POSIX-like machines, the shell provisioner executes scripts with
|
|
|
|
SSH. For Windows guest machines that are configured to use WinRM, the
|
|
|
|
shell provisioner executes PowerShell and Batch scripts over WinRM.
|
|
|
|
|
2013-09-21 00:31:52 +00:00
|
|
|
## Options
|
|
|
|
|
|
|
|
The shell provisioner takes various options. One of `inline` or `path`
|
|
|
|
is required:
|
|
|
|
|
|
|
|
* `inline` (string) - Specifies a shell command inline to execute on the
|
|
|
|
remote machine. See the [inline scripts](#inline-scripts) section below
|
|
|
|
for more information.
|
|
|
|
|
2013-10-01 21:04:28 +00:00
|
|
|
* `path` (string) - Path to a shell script to upload and execute. It can be a
|
|
|
|
script relative to the project Vagrantfile or a remote script (like a [gist](http://gist.github.com)).
|
2013-09-21 00:31:52 +00:00
|
|
|
|
|
|
|
The remainder of the available options are optional:
|
|
|
|
|
2013-11-25 21:36:51 +00:00
|
|
|
* `args` (string or array) - Arguments to pass to the shell script when executing it
|
2013-09-21 00:31:52 +00:00
|
|
|
as a single string. These arguments must be written as if they were typed
|
|
|
|
directly on the command line, so be sure to escape characters, quote,
|
2013-11-25 21:36:51 +00:00
|
|
|
etc. as needed. You may also pass the arguments in using an array. In this
|
|
|
|
case, Vagrant will handle quoting for you.
|
2013-09-21 00:31:52 +00:00
|
|
|
|
2015-11-24 00:24:06 +00:00
|
|
|
* `env` (hash) - List of key-value pairs to pass in as environment variables to
|
|
|
|
the script. Vagrant will handle quoting for environment variable values, but
|
|
|
|
the keys remain untouched.
|
|
|
|
|
2013-09-21 00:31:52 +00:00
|
|
|
* `binary` (boolean) - Vagrant automatically replaces Windows line endings with
|
|
|
|
Unix line endings. If this is true, then Vagrant will not do this. By default
|
2014-04-12 15:51:29 +00:00
|
|
|
this is "false". If the shell provisioner is communicating over WinRM, this
|
|
|
|
defaults to "true".
|
2013-09-21 00:31:52 +00:00
|
|
|
|
|
|
|
* `privileged` (boolean) - Specifies whether to execute the shell script
|
2015-08-21 20:37:10 +00:00
|
|
|
as a privileged user or not (`sudo`). By default this is "true". Windows
|
|
|
|
guests use a scheduled task to run as a true administrator without the
|
|
|
|
WinRM limitations.
|
2013-09-21 00:31:52 +00:00
|
|
|
|
|
|
|
* `upload_path` (string) - Is the remote path where the shell script will
|
|
|
|
be uploaded to. The script is uploaded as the SSH user over SCP, so this
|
2014-04-12 16:05:08 +00:00
|
|
|
location must be writable to that user. By default this is
|
|
|
|
"/tmp/vagrant-shell". On Windows, this will default to
|
|
|
|
"C:\tmp\vagrant-shell".
|
2013-09-21 00:31:52 +00:00
|
|
|
|
2013-11-24 00:05:44 +00:00
|
|
|
* `keep_color` (boolean) - Vagrant automatically colors output in green and
|
|
|
|
red depending on whether the output is from stdout or stderr. If this is
|
|
|
|
true, Vagrant will not do this, allowing the native colors from the script
|
|
|
|
to be outputted.
|
2013-11-16 12:30:58 +00:00
|
|
|
|
2015-04-18 04:35:21 +00:00
|
|
|
* `name` (string) - This value will be displayed in the output so that
|
|
|
|
identification by the user is easier when many shell provisioners are present.
|
|
|
|
|
2014-10-23 16:53:55 +00:00
|
|
|
* `powershell_args` (string) - Extra arguments to pass to `PowerShell`
|
|
|
|
if you're provisioning with PowerShell on Windows.
|
|
|
|
|
2015-11-18 20:51:18 +00:00
|
|
|
* `powershell_elevated_interactive` (boolean) - Run an elevated script in interactive mode
|
2015-08-21 20:37:10 +00:00
|
|
|
on Windows. By default this is "false". Must also be `privileged`. Be sure to
|
|
|
|
enable auto-login for Windows as the user must be logged in for interactive
|
|
|
|
mode to work.
|
|
|
|
|
2013-09-21 00:31:52 +00:00
|
|
|
<a name="inline-scripts"></a>
|
2013-09-03 18:08:28 +00:00
|
|
|
## Inline Scripts
|
|
|
|
|
|
|
|
Perhaps the easiest way to get started is with an inline script. An
|
|
|
|
inline script is a script that is given to Vagrant directly within
|
|
|
|
the Vagrantfile. An example is best:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
2013-09-06 16:50:43 +00:00
|
|
|
config.vm.provision "shell",
|
|
|
|
inline: "echo Hello, World"
|
2013-09-03 18:08:28 +00:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
This causes `echo Hello, World` to be run within the guest machine when
|
|
|
|
provisioners are run.
|
|
|
|
|
|
|
|
Combined with a little bit more Ruby, this makes it very easy to embed
|
|
|
|
your shell scripts directly within your Vagrantfile. Another example below:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
$script = <<SCRIPT
|
|
|
|
echo I am provisioning...
|
|
|
|
date > /etc/vagrant_provisioned_at
|
|
|
|
SCRIPT
|
|
|
|
|
|
|
|
Vagrant.configure("2") do |config|
|
2013-09-06 16:50:43 +00:00
|
|
|
config.vm.provision "shell", inline: $script
|
2013-09-03 18:08:28 +00:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2014-04-12 15:51:29 +00:00
|
|
|
I understand that if you're not familiar with Ruby, the above may seem very
|
2013-09-03 18:08:28 +00:00
|
|
|
advanced or foreign. But don't fear, what it is doing is quite simple:
|
|
|
|
the script is assigned to a global variable `$script`. This global variable
|
|
|
|
contains a string which is then passed in as the inline script to the
|
|
|
|
Vagrant configuration.
|
|
|
|
|
|
|
|
Of course, if any Ruby in your Vagrantfile outside of basic variable assignment
|
|
|
|
makes you uncomfortable, you can use an actual script file, documented in
|
|
|
|
the next section.
|
|
|
|
|
2014-04-12 15:51:29 +00:00
|
|
|
For Windows guest machines, the inline script _must_ be PowerShell. Batch
|
|
|
|
scripts are not allowed as inline scripts.
|
|
|
|
|
2013-09-03 18:08:28 +00:00
|
|
|
## External Script
|
|
|
|
|
|
|
|
The shell provisioner can also take an option specifying a path to
|
|
|
|
a shell script on the host machine. Vagrant will then upload this script
|
|
|
|
into the guest and execute it. An example:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
2013-09-06 16:50:43 +00:00
|
|
|
config.vm.provision "shell", path: "script.sh"
|
2013-09-03 18:08:28 +00:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
Relative paths, such as above, are expanded relative to the location
|
|
|
|
of the root Vagrantfile for your project. Absolute paths can also be used,
|
|
|
|
as well as shortcuts such as `~` (home directory) and `..` (parent directory).
|
|
|
|
|
2013-10-01 21:04:28 +00:00
|
|
|
If you use a remote script as part of your provisioning process, you can pass in
|
|
|
|
its URL as the `path` argument as well:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
config.vm.provision "shell", path: "https://example.com/provisioner.sh"
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2014-04-12 15:51:29 +00:00
|
|
|
If you're running a Batch of PowerShell script for Windows, make sure
|
|
|
|
that the external path has the proper extension (".bat" or ".ps1"), because
|
2015-10-11 15:11:33 +00:00
|
|
|
Windows uses this to determine what kind of file it is to execute. If you
|
2014-04-12 15:51:29 +00:00
|
|
|
exclude this extension, it likely won't work.
|
|
|
|
|
2015-01-21 15:26:27 +00:00
|
|
|
To run a script already available on the guest you can use an inline script to
|
|
|
|
invoke the remote script on the guest.
|
2015-01-21 15:22:10 +00:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
config.vm.provision "shell",
|
2015-01-21 15:26:27 +00:00
|
|
|
inline: "/bin/sh /path/to/the/script/already/on/the/guest.sh"
|
2015-01-21 15:22:10 +00:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2013-09-03 18:08:28 +00:00
|
|
|
## Script Arguments
|
|
|
|
|
|
|
|
You can parameterize your scripts as well like any normal shell script.
|
|
|
|
These arguments can be specified to the shell provisioner. They should
|
|
|
|
be specified as a string as they'd be typed on the command line, so
|
|
|
|
be sure to properly escape anything:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
2013-09-06 16:50:43 +00:00
|
|
|
config.vm.provision "shell" do |s|
|
2013-09-03 18:08:28 +00:00
|
|
|
s.inline = "echo $1"
|
|
|
|
s.args = "'hello, world!'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
2013-11-25 21:36:51 +00:00
|
|
|
|
2015-12-30 21:17:45 +00:00
|
|
|
You can also specify arguments as an array if you don't want to worry about
|
2013-11-25 21:36:51 +00:00
|
|
|
quoting:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
config.vm.provision "shell" do |s|
|
|
|
|
s.inline = "echo $1"
|
|
|
|
s.args = ["hello, world!"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|