具有多个VM和提供程序的Vagrantfile [英] Vagrantfile with multiple vm and providers
问题描述
我正在尝试使用由多个提供程序备份的多台计算机编写一个Vagrantfile.我特别希望能够一次性生成多台其中一台机器.基本上我想运行命令:
vagrant up vb_vm aws_vm
I am trying to write a Vagrantfile with multiple machines backed up by multiple providers. I specifically want to be able to spawn more than one of those machines in one go. Basically I want to run the command:
vagrant up vb_vm aws_vm
我知道--provider标志,但这适用于所有正在生成的所有机器,因此不适用于我的情况.
I am aware of the --provider flag, but this would apply to all machines being spawned, so not applicable in my case.
这是我的(非常删减但仍然有效)Vagrantfile:
This is my (very trimmed down but still valid) Vagrantfile:
Vagrant.configure(2) do |config|
config.vm.define 'vb_vm' do |vb_vm|
vb_vm.vm.box='ubuntu/trusty64' # from hashicorp
vb_vm.vm.provider :virtualbox do |v|
end
end
config.vm.define 'aws_vm' do |aws_vm|
aws_vm.vm.box = "aws/dummy"
aws_vm.vm.box_url = 'https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box'
aws_vm.vm.provider :aws do |a, override|
a.access_key_id = 'something'
a.secret_access_key = 'something'
a.ami='something'
end
end
end
A vagrant box list
显示用于每个定义的框的类型正确:
A vagrant box list
shows that the boxes used for each definitions are of the right type:
aws/dummy (aws, 0)
ubuntu/trusty64 (virtualbox, 20150928.0.0)
但是vagrant status
给了我(请注意,我确实有可用的lxc插件,这已成为默认设置)
But a vagrant status
gives me (note that I do have the lxc plugin available, which became the default)
当前机器状态:
aws_vm not created (aws)
vb_vm not created (lxc)
因此,这表明可以使用多个提供程序生成多台计算机,但是提供程序的选择是错误的.
So this shows that spawning multiple machine with multiple provider is indeed possible, but the choice of provider is wrong.
我知道设置默认提供程序的技巧,但这只会使情况变得更糟(virtualbox随处可见,aws根本不使用...)
I am aware of the tricks to set up the default provider, but this only makes things worse (virtualbox used everywhere, aws not used at all...)
我也知道旧的堆栈溢出问题,但它们与Vagrant的较旧版本有关.
I am aware of old stackoverflow questions as well, but they are related to a much older version of Vagrant.
问题是:如何确保定义的每个框都使用其正确的提供程序?
So the question is: how do I make sure that each box defined uses its proper provider?
推荐答案
诀窍将是使用自己的提供程序创建VM.
The trick will be to create the VM with their own provider.
示例:我为每个提供商定义了一个快速的Vagrantfile(已最小化)并带有框
example: I've defined a quick Vagrantfile (minimized) with boxes for each provider
Vagrant.configure(2) do |config|
config.vm.define "db" do |db|
db.vm.box = "..."
db.vm.hostname = "db"
end
config.vm.define "app", primary: true do |app|
app.vm.box = "..."
app.vm.hostname = "app"
app.ssh.forward_agent = true
app.ssh.forward_x11 = true
app.vm.provider "vmware_fusion" do |vm|
vm.vmx["memsize"] = "4096"
end
end
end
我分别创建每个VM
fhenri@machine:~/project/examples/vagrant/multimachine$ vagrant up db --provider=virtualbox
Bringing machine 'db' up with 'virtualbox' provider...
....
fhenri@machine:~/project/examples/vagrant/multimachine$ vagrant up app
Bringing machine 'app' up with 'vmware_fusion' provider...
....
然后我停止一切,下次再执行vagrant up
then I halt everything and next time I do vagrant up
fhenri@machine:~/project/examples/vagrant/multimachine$ vagrant up
Bringing machine 'db' up with 'virtualbox' provider...
Bringing machine 'app' up with 'vmware_fusion' provider...
状态看起来不错
fhenri@machine:~/project/examples/vagrant/multimachine$ vagrant status
Current machine states:
db running (virtualbox)
app running (vmware_fusion)
This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
这篇关于具有多个VM和提供程序的Vagrantfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!