Vagrant execute command on specific guest after other guests are up

1.4k views Asked by At

I have the following Vagrantfile:

Vagrant.configure(VAGRANT_API_VERSION) do |config|
  config.vm.provider "virtualbox" do |vb|
    vb.memory = 1024
    vb.cpus = 2
  end

  config.vm.define :master do |master_config|
    master_config.vm.box = "centos/7"
    master_config.vm.host_name = 'saltmaster.local'
    master_config.vm.network "private_network", ip: "172.16.10.10"
    master_config.vm.synced_folder ".", "/vagrant", disabled: true
    master_config.vm.synced_folder "states", "/vagrant/states", type: "virtualbox"
    master_config.vm.synced_folder "pillar", "/vagrant/pillar", type: "virtualbox"

    master_config.vm.provision :salt do |salt|
      salt.master_config = "etc/master"
      salt.install_type = "git"
      salt.install_args = "v2016.11.7"
      salt.no_minion = true
      salt.install_master = true
      salt.verbose = true
      salt.colorize = true
      salt.bootstrap_options = "-P -c /tmp"
    end
  end

  config.vm.define :minion1 do |minion_config|
    minion_config.vm.box = "centos/7"
    minion_config.vm.host_name = 'saltminion1.local'
    minion_config.vm.network "private_network", ip: "172.16.10.11"
    minion_config.vm.synced_folder ".", "/vagrant", disabled: true

    minion_config.vm.provision :salt do |salt|
      salt.minion_config = "etc/minion1"
      salt.install_type = "git"
      salt.install_args = "v2016.11.7"
      salt.verbose = true
      salt.colorize = true
      salt.bootstrap_options = "-P -c /tmp"
    end
  end

  config.vm.define :minion2 do |minion_config|
    minion_config.vm.box = "centos/7"
    minion_config.vm.host_name = 'saltminion2.local'
    minion_config.vm.network "private_network", ip: "172.16.10.12"
    minion_config.vm.synced_folder ".", "/vagrant", disabled: true

    minion_config.vm.provision :salt do |salt|
      salt.minion_config = "etc/minion2"
      salt.install_type = "git"
      salt.install_args = "v2016.11.7"
      salt.verbose = true
      salt.colorize = true
      salt.bootstrap_options = "-P -c /tmp"
    end
  end

end

Now after all the machines are up and running, I want to execute a command on the salt master using master_config.vm.provision "shell", inline: "salt '*' state.apply".

But the problem is, once Vagrant finished provisioning minion2, it can't access the master_config machine anymore. I think there should be a way to execute that command, without having to execute vagrant ssh master -c stalt '*' state.apply. I don't want to use a command on the host. And the highstate needs to be applied AFTER all the minions are up, due to some networking related configurations and states.

Can someone help me out?

1

There are 1 answers

2
Frederic Henri On

... continued from comments, making here for the example.

You can use the vagrant trigger plugin. As mentioned in the doc:

Starting from version 0.5.0, triggers can also be run as a provisioner

so the following at the end of the file should work:

  config.vm.provision "trigger", :vm => "minion_config" do |trigger|
    trigger.fire do
      run "script"
    end
  end