Create multiple VMs with one Vagrantfile
Posted on 08-Sep-2018
VM_NAME = 'multi.local'
VM_RAM = 3000
VM_CPUS = 2
servers = [
{
'IP': '10.20.7.10',
'NAME': 'server-1',
'PORT': '12710'
},
{
'IP': '10.20.7.20',
'NAME': 'server-2',
'PORT': '12720'
},
]
Vagrant.configure(2) do |config|
servers.each do |srv|
config.vm.define srv[:NAME] do |node|
# ubuntu 16
node.vm.box = "ubuntu/xenial64"
node.vm.box_check_update = false
node.vm.hostname = srv[:NAME]
node.vm.network :private_network, ip: srv[:IP]
node.vm.network :forwarded_port, guest: 22, host: srv[:PORT], id: 'ssh'
node.nfs.map_uid = Process.uid
node.nfs.map_gid = Process.gid
node.vbguest.auto_update = false
node.ssh.insert_key = true
# upload ssh id_rsa file
node.vm.provision :file do |file|
file.source = "~/.ssh/id_rsa"
file.destination = "~/.ssh/id_rsa"
end
# upload ssh id_rsa.pub file
node.vm.provision :file do |file|
file.source = "~/.ssh/id_rsa.pub"
file.destination = "~/.ssh/id_rsa.pub"
end
$script = <<-SCRIPT
# make sure you have python installed if you want to use ansible
apt-get install -y python
cat /home/vagrant/.ssh/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys
SCRIPT
node.vm.provision :shell, inline: $script
node.vm.provider :virtualbox do |vb|
vb.name = srv[:NAME]
vb.customize([
"modifyvm", :id,
"--memory", VM_RAM,
"--cpus", VM_CPUS,
"--vram", 32
])
end
end
end
end