Commanding Vagrant
To run a command in a vagrant VM you have to SSH, change to the folder, and run the command, far too many steps.
The exec plugin lets you execute commands inside the VM from the outside.
$ vagrant plugin install vagrant-exec
Install the plugin via the vagrant command line. You can now run commands inside the VM. To list files use vagrant exec ls.
Directory
Vagrant.configure("2") do |config|
config.exec.commands "*", directory: '/home/vagrant/project'
config.exec.commands "npm", directory: '/home/vagrant/other'
end
You can set the directory a command will run in, the special *
command apply
to all commands.
Running vagrant exec npm install will run npm install
in the
directory /home/vagrant/other
. Any other commands will run in
/home/vagrant/project
.
Prepends
Vagrant.configure("2") do |config|
config.exec.commands "rake" prepend: 'bundle exec'
end
You can prepend text to the start of a command.
Running vagrant exec rake foo will now runbundle exec rake foo
instead.
Environment Variables
Vagrant.configure("2") do |config|
config.exec.commands "*", env: {
"PATH" => "/home/vagrant/env/bin:$PATH"
}
end
You can alter the environment variables for a command too. The above will change
PATH
to use commands in a python virtualenv.
Complex Rules
Vagrant.configure("2") do |config|
config.exec.commands "*", directory: '/home/vagrant/project'
config.exec.commands "*", env: {
"PATH" => "/home/vagrant/env/bin:$PATH"
}
end
Each rule that matches a command will apply in order, and you can add more than one rule for each command.