I'm trying to run a script that'll connect to all machines defined in my vagrantfile and do some configuration that requires all of them to be up. However, I'd like to run it with the vagrant up command rather than manually after.
Is there a way to achieve this?
This will only run on initial vagrant up (creation of the VM). If you mess up, you will have to vagrant destroy to run the provisioning again.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(“2”) do |config|
config.vm.box = “bento/centos-7.6”
config.vm.hostname = “yourhostname”
config.vm.network “forwarded_port”, guest: 80, host: 8080
config.vm.network “private_network”, ip: “192.168.33.10”
config.vm.synced_folder “.”, “/var/www”, :nfs => { :mount_options => [“dmode=777″,”fmode=666”] }
config.ssh.insert_key = false
config.vm.provision “shell”, path: “install.sh”, privileged: false
end
[install.sh](https://install.sh)
#!/bin/bash
#whatever your heart desires
sudo yum update
If you want to run a script on the host then you could use vagrant triggers, they were introduced in vagrant ver-2.1.0
You could do something like this;
“`
Vagrant.configure(“2”) do |config|
config.trigger.after :up do |trigger|
trigger.run = {path: path/to/script.sh}
end
…
Machine definitions starts here
“`
This would run your script after all the machines are up