Run script on host machine during vagrant up
At least two plugins which should help:
- vagrant-host-shell
- vagrant-triggers
If you don't care that the script is run on (almost) all vagrant
commands, you can also just shell out (or use what ever ruby magic) in Vagrantfile:
system('./myscript.sh')
Vagrant.configure('2') do |config|
# ...
end
Simple (and complete) Solution
(I say complete because the accepted answer does not check if the user is using vagrant up. Therefore, the script is executed on each command, which is not what the OP wants.)
There is however a simple solution to this.
ARGV[0]
is the first argument of the command entered and can be up
, down
, status
, etc.. Simply check the value of ARGV[0]
in your Vagrantfile.
Something like this will do:
system("
if [ #{ARGV[0]} = 'up' ]; then
echo 'You are doing vagrant up and can execute your script'
./myscript.sh
fi
")
Vagrant.configure('2') do |config|
# ...
end
Put this near the top of your Vagrantfile:
module LocalCommand
class Config < Vagrant.plugin("2", :config)
attr_accessor :command
end
class Plugin < Vagrant.plugin("2")
name "local_shell"
config(:local_shell, :provisioner) do
Config
end
provisioner(:local_shell) do
Provisioner
end
end
class Provisioner < Vagrant.plugin("2", :provisioner)
def provision
result = system "#{config.command}"
end
end
end
Then simply invoke in your Vagrantfile like this:
config.vm.provision "list-files", type: "local_shell", command: "ls"
And via the command line like this:
vagrant provision --provision-with list-files
This is kind of a hack as it looks like plug-in, but really isn't (it won't show up when you do vagrant plugin list
). I don't recommend doing it this way except that it has the advantage of not needing to install a plugin, so your Vagrantfile will work on any machine that supports the latest configuration version (version 2 as of writing this). Though that sounds promisingly portable, there's also the whole cross-platform issue of the actual command you're issuing. You'll need to take into consideration if you want your Vagrantfile to be portable, but this should get you started.