其他客人启动后,对特定客人执行命令

时间:2017-09-07 14:41:45

标签: vagrant vagrantfile vagrant-provision

我有以下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

现在所有机器都启动并运行后,我想使用master_config.vm.provision "shell", inline: "salt '*' state.apply"在salt master上执行命令。

但问题是,一旦Vagrant完成配置minion2,它就不能再访问master_config机器了。我认为应该有一种方法来执行该命令,而不必执行vagrant ssh master -c stalt '*' state.apply。我不想在主机上使用命令。由于一些网络相关的配置和状态,所有的小兵都需要在高阶段之后应用高阶段。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

...继续发表评论,以此为例。

您可以使用vagrant trigger plugin。如文档中所述:

  

从版本0.5.0开始,触发器也可以作为配置器运行

所以文件末尾的以下内容应该有效:

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