2016-09-07 105 views
0

如何根據condition告訴vagrantprovisionVagrant Multi VM:根據條件設置

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 

但每次它執行上的所有虛擬機

provision塊,我一環

Vagrantfile下紡絲了多虛擬機

回答

0

這應該是srv.vm.provision,即你只在該服務器上定義它而不是根Vagrant配置。

+0

感謝@coderanger,我計算過,後來:) – devnull

0

我想我找到了解決方案。這是我的錯

代替:

config.vm.provision :chef_solo do |chef| 

我應該用

srv.vm.provision :chef_solo do |chef| 

srv循環