好的,我是Ruby的新手(我来自PHP,Symfony2和AngularJS),而且在正确编写Vagrantfiles方面相对较新。我试图在坚持DRY原则的同时创建一个多机器环境。
当我读到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中使用Hashes或类似内容的任何信息。 我真的希望你可以帮助我,因为我在写一个有很多重复的超长Vagrant文件时感觉不舒服...
提前致谢!
答案 0 :(得分: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