Vagrant Multi VM:根据条件配置每个

Vagrant Multi VM: provision each based on condition

如何根据condition告诉vagrantprovision?我在一个循环中启动多个 VM

Vagrantfile:

   Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
     # Iterate through entries in YAML file
     servers.each do |servers|
       config.vm.define servers["name"] do |srv|
         srv.vm.hostname = servers["name"] + DOMAIN
         srv.vm.box = servers["box"]
         srv.vm.network "private_network", ip: servers["ip"]
         srv.vm.synced_folder ".", "/data", id: "mapping", disabled: servers["share"]
         srv.vm.provider :virtualbox do |vb|
           vb.memory = servers["ram"]
           vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
         end
       end
    end
  end

我想添加这样的内容:

    servers.each do |servers|
       config.vm.define servers["name"] do |srv|
         srv.vm.hostname = servers["name"] + DOMAIN
         srv.vm.box = servers["box"]
         srv.vm.network "private_network", ip: servers["ip"]
         srv.vm.synced_folder ".", "/data", id: "mapping", disabled: servers["share"]
         srv.vm.provider :virtualbox do |vb|
           vb.memory = servers["ram"]
           vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
         end
         if servers["name"] == "server1"
           config.vm.provision :chef_solo do |chef|
             chef.cookbooks_path = "#{cookbooks}"
             chef.add_recipe "chef_blah::server"
           end
         else
           puts("Skipping it...")
         end
      end
    end

但每次它在所有 VM 上执行 provision 块时

应该是 srv.vm.provision,也就是说,你只在那个服务器上定义它,而不是根 Vagrant 配置。

我想我找到了解决办法。这是我的错误

而不是:

config.vm.provision :chef_solo do |chef|

我应该用

srv.vm.provision :chef_solo do |chef|

srv循环中