2

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?

Haije Ploeg
  • 162
  • 2
  • 12
  • you can look at the vagrant trigger plugin (https://github.com/emyl/vagrant-triggers) – Frederic Henri Sep 07 '17 at 14:49
  • @FrédéricHenri Yes, we have looked into that, but we only want this to be executed at the first run of `vagrant up` and not with every change, so only at the bootstrap phase. – Haije Ploeg Sep 08 '17 at 07:06
  • _Starting from version 0.5.0, triggers can also be run as a provisioner_ so it will have provisioning property and will run only once when provisioning instance – Frederic Henri Sep 08 '17 at 07:21
  • @FrédéricHenri so I need to have something like: `config.vm.provision "trigger", ...` But then my questen remains. How to I trigger this after the master, minion1 and minion2 is up and running? Because after they are up, I need to execute a command on the salt master. I can't wrap my head around it. Because how can I target the salt master machine? – Haije Ploeg Sep 08 '17 at 07:31

1 Answers1

0

... 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
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Thanks that solved one problem. But now the part that I need to execute a highstate on the salt master. I want this to be a `config.vm.provision "shell", inline: "salt '*' state.apply"` and I really want to prevent the usage of `vagrant ssh salt ...`. How can I access the saltmaster object after it is done provisioning? – Haije Ploeg Sep 08 '17 at 11:48
  • provisioning are run in order, so if you have the trigger provisioner after all the other, vagrant will execute the command at the end, trigger can run command on the physical server or the remote machine, so it will take care of the `vagrant ssh ...` part – Frederic Henri Sep 13 '17 at 15:23