I'm going to create a multiple environments machines with Vagrant. Here is the VagrantFile I'm trying to configure. I would like to instantiate seven machines connected them across a private networks. Every of this one should have two disks. I have found in Vagrant documentation the VBoxManage that expose the createhd
command. I'm not sure where should I place this command. Inside every machine block or inside virtual provider block config?
Vagrant.configure(2) do |config|
config.vm.provision "shell", inline: "echo OpenStack"
config.vm.box = "ubuntu/trusty64"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048", "--cpus", "1"]
end
config.vm.define "machine1" do |machine1|
machine1.vm.hostname = "machine1"
machine1.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine1_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine1_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine1_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine1_disk1.vdi"]
end
machine1.vm.network "private_network", ip: "192.168.10.10"
machine1.vm.network "private_network", ip: "192.168.10.15"
end
config.vm.define "machine2" do |machine2|
machine2.vm.hostname = "machine2"
machine2.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine2_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine2_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine2_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine2_disk1.vdi"]
end
machine2.vm.network "private_network", ip: "192.168.10.20"
machine2.vm.network "private_network", ip: "192.168.10.25"
end
config.vm.define "machine3" do |machine3|
machine3.vm.hostname = "machine3"
machine3.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine3_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine3_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine3_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine3_disk1.vdi"]
end
machine3.vm.network "private_network", ip: "192.168.10.30"
end
config.vm.define "machine4" do |machine4|
machine4.vm.hostname = "machine4"
machine4.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine4_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine4_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine4_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine4_disk1.vdi"]
end
machine4.vm.network "private_network", ip: "192.168.10.40"
end
config.vm.define "machine5" do |machine5|
machine5.vm.hostname = "machine5"
machine5.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine5_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine5_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine5_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine5_disk1.vdi"]
end
machine5.vm.network "private_network", ip: "192.168.10.50"
end
config.vm.define "machine6" do |machine6|
machine6.vm.hostname = "machine6"
machine6.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine6_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine6_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine6_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine6_disk1.vdi"]
end
machine6.vm.network "private_network", ip: "192.168.10.60"
end
config.vm.define "machine7" do |machine7|
machine7.vm.hostname = "machine7"
machine7.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine7_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine7_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine7_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine7_disk1.vdi"]
end
machine7.vm.network "private_network", ip: "192.168.10.70"
end
end
'This works for me.' 'I thought I share this as it might help Vagrant Newbie like me.'
Well, you really want it to create and attach the extra storage just once. I believe the accepted answer above will cause an error on subsequent runs because the .VDI file is already created (and already attached).
The way to do this is to test if the VDI file is on the host's disk with
File.exist?
, meaning it's already been created.For multi-machines, this might be what you want:
First of all customizations, like
createhd
, must be added to provider. If you add it to config providerIt will be defined globally, and these parameters will be used by all machines. (Not sure) In result only one disk will be created, and will be shared among defined machines.
I think you should define provider in each machine. E.g
Please treat this Vagrantfile as a reference.