我有一个流浪文件,我想要一个变量"服务器"待用......
# -*- mode: ruby -*-
# vi: set ft=ruby :
NUMM = 3
IP_OFFSET = 10
setup_master = File.read("master.sh")
setup_slave = File.read("slave.sh")
def ip_from_num(i)
"172.31.16.#{100+i+IP_OFFSET}"
end
# Map of servers -> parameters.
servers = {
0 => ["mybox","master.rhbd","ami-759dcb74","ap-northeast-1","subnet-4aa28b22","MASTER",ip_from_num(0)],
1 => ["mybox","slave1.rhbd","ami-759dcb74","ap-northeast-1","subnet-4aa28b22","SLAVE",ip_from_num(1)],
}
def getBox()
## this variable isnt available to vagrant...
servers[0]
end
Vagrant.configure("2") do |config|
(0..NUMM).each do |i|
config.vm.define "aws#{i}" do |n|
n.vm.box = getBox()
...
但是,当调用此Vagrantfile时,vagrant会抱怨"服务器"变量不存在。这是有道理的:如果vagrant从另一个类调用,并从该位置读取配置,则可能无法在该范围内访问Vagrantfile中定义的类变量。
所以我的问题是:如何在我的Vagrantfile中创建外部供应商可访问的变量?它似乎与函数调用一样正常(或者因为它们在创建期间实现,或者因为vagrant可以很容易地调用函数,因为默认的作用域)。
答案 0 :(得分:0)
另一种选择是将变量转换为函数-不好,但是如果对这种情况足够好的话:
# Map of servers -> parameters.
def servers()
{
0 => ["mybox","master.rhbd","ami-759dcb74","ap-northeast-1","subnet-4aa28b22","MASTER",ip_from_num(0)],
1 => ["mybox","slave1.rhbd","ami-759dcb74","ap-northeast-1","subnet-4aa28b22","SLAVE",ip_from_num(1)],
}
end
def getBox()
## this variable isnt available to vagrant...
servers()[0]
end
(假设在其他地方也需要servers
,否则它可能只是getBox
中的局部变量。)