我试图从一个vagrant文件中使用循环启动2个盒子。我以前从来没有使用Ruby编程,而且我使用vagrant也不是很高级,所以可能是一些简单而明显的东西,但我不明白是什么。
下面是一个简化的例子。
{
:hostname => "first",
:ip => "192.168.100.10",
:box => "minimal/xenial64",
:ram => 1024,
:cpu => 2,
:port => 9080
},
{
:hostname => "second",
:ip => "192.168.100.11",
:box => "minimal/xenial64",
:ram => 2024,
:cpu => 1,
:port => 9081
}
]
Vagrant.configure(2) do |config|
machines.each do |machine|
config.vm.define machine[:hostname] do |node|
# This is working just fine, each machine gats it's own ip, port, ram, memory
# as they specified in machines array above
node.vm.box = machine[:box]
node.vm.hostname = machine[:hostname]
node.vm.network "private_network", ip: machine[:ip]
node.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", machine[:ram]]
end
# But this is not working the same way for some reason,
# firs machine gets both ports
config.vm.network :forwarded_port, guest: 80, host: machine[:port]
#==> first: Forwarding ports...
# first: 80 (guest) => 9080 (host) (adapter 1)
# first: 80 (guest) => 9081 (host) (adapter 1)
# And than second machine obviously can start because of:
# The forwarded port to 9080 is already in use on the host machine.
end
end
end
由于我怀疑这是一些简单和明显的,我用了
config.vm.network :forwarded_port, guest: 80, host: machine[:port]
反而
node.vm.network :forwarded_port, guest: 80, host: machine[:port]
应使用,因为 node
是指当前的关闭。