2014-09-30 41 views
2

好吧,我很新的Ruby(我来自PHP,Symfony2和AngularJS)和相对较新的,当涉及到正确写Vagrantfiles。我试图在坚持DRY原则的同时创建一个多机器环境。试图循环散列Vagrant框,失败

当我读到Vagrantfiles理解Ruby语法时,我查找了Ruby定义关联数组的方式。这很容易,显然不是。

我Vagrantfile:

# -*- mode: ruby -*- 
# vi: set ft=ruby : 

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! 
VAGRANTFILE_API_VERSION = "2" 

#hash for boxes: 'box_name' => 'last_ip_octet' 
boxes = { 
     'frontend' => '10', 
     'qp' => '11' 
} 


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 

config.vm.box = "chef/ubuntu-14.04" 

#All machines, see the hash defined in top of the Vagrantfile for all the boxes 
    boxes.each do |key, value| 
    config.vm.define "#{key}.qp" do |#{key}_qp| 
     #{key}_qp.vm.network "private_network", ip: "192.168.51.#{value}" 
     #{key}_qp.vm.provision "shell", path: "../provisioning/agentinstall.sh" 
     #{key}_qp.vm.synced_folder "./share/#{key}.qp", "/var/www/html" 
    end 
    end 
end           

我的问题如下:

There is a syntax error in the following Vagrantfile. The syntax error 
message is reproduced below for convenience: 

/Users/Zowie/Documents/vagrant/project/temp/Vagrantfile:30: syntax error,   unexpected keyword_end, expecting '|' 
end  
^  

不幸的是,我无法找到使用哈希或任何东西Vagrantfiles类似的任何信息。 我真的希望你能帮助我,因为写一个有很多重复的超长流浪文件我感觉不好...

在此先感谢!

+0

Jeeeeeeez,我现在注意到,“#”注释掉行的文件中。我会在30分钟内检查它!如果你正在阅读,请稍等一下。谢谢! – ZvL 2014-09-30 09:32:26

+1

你是对的 - 这是'config.vm.define'这行中的散列,这是破坏性的。 – 2014-09-30 09:53:37

+0

修正了它!感谢您的确认。回答自己的问题,以帮助有同样问题的任何人。 – ZvL 2014-09-30 10:43:25

回答

2

Stackoverflow网站为我解答了我的问题! 感谢Stackoverflow的代码块功能,我注意到我的机器特定的配置被注释掉了,因为我使用了'#'。

我用我的环以下语法(这也是更容易阅读)固定它:

boxes.each do |key, value| 
    config.vm.define "#{key}.qp" do |node| 
     node.vm.network "private_network", ip: "192.168.51.#{value}" 
     node.vm.provision "shell", path: "../provisioning/agentinstall.sh" 
     node.vm.synced_folder "./share/#{key}.qp", "/var/www/html" 
    end 
    end