I wrote an article recently looking at how to deploy virtual machines on vSphere using Vagrant. On that post I covered how to deploy a single Photon OS virtual machine using Vagrant. Here, I wanted to do a quick post on how to deploy multiple VMs from a single Vagrant file. One way to do so is to define each VM separately in the Vagrantfile, for example:
Vagrant.configure(2) do |config|
config.vm.define "VM1" do |subconfig|
subconfig.vm.box = "vsphere"
subconfig.vm.box_url = './example_box/dummy.box'
subconfig.vm.network 'private_network', ip: '192.168.5.224'
subconfig.vm.provider 'vsphere' do |vsphere|
# The host we're going to connect to
vsphere.host = 'vc01.testlab.local'
# The host for the new VM
vsphere.compute_resource_name = 'esxi01.testlab.local'
# The resource pool for the new VM
# vsphere.resource_pool_name = 'VagrantVMs'
# The template we're going to clone
vsphere.template_name = 'PhotonTemplate'
# The name of the new machine
vsphere.name = 'PhotonVM1'
# vSphere login
vsphere.user = 'administrator@vsphere.local'
# vSphere password
vsphere.password = 'password'
# If you don't have SSL configured correctly, set this to 'true'
vsphere.insecure = true
vsphere.customization_spec_name = 'centos66'
vsphere.memory_mb = 4096
vsphere.vlan = 'isolated'
end
end
config.vm.define "VM2" do |subconfig1|
subconfig1.vm.box = "vsphere"
subconfig1.vm.box_url = './example_box/dummy.box'
subconfig1.vm.network 'private_network', ip: '192.168.5.225'
subconfig1.vm.provider 'vsphere' do |vsphere|
# The host we're going to connect to
vsphere.host = 'vc01.testlab.local'
# The host for the new VM
vsphere.compute_resource_name = 'esxi01.testlab.local'
# The resource pool for the new VM
# vsphere.resource_pool_name = 'VagrantVMs'
# The template we're going to clone
vsphere.template_name = 'PhotonTemplate'
# The name of the new machine
vsphere.name = 'PhotonVM2'
# vSphere login
vsphere.user = 'administrator@vsphere.local'
# vSphere password
vsphere.password = 'password'
# If you don't have SSL configured correctly, set this to 'true'
vsphere.insecure = true
vsphere.customization_spec_name = 'centos66'
vsphere.vlan = 'isolated'
# config.vm.network 'private_network', ip: '192.168.5.225'
end
end
end
Using this method, you can have Vagrant provision multiple VMs, each with their own configuration. E.g. a small web server VM and a database VM with more compute resources allocated. Alternatively, to provision multiple VMs with the same hardward configuration, something like the following could be used:
[root@localhost vagrant_vm]# cat Vagrantfile
vm1 = { 'name' => "PhotonVM1", 'ip' => "192.168.5.224" }
vm2 = { 'name' => "PhotonVM2", 'ip' => "192.168.5.225" }
vms = [ vm1, vm2]
Vagrant.configure(2) do |config|
vms.each do |node|
vm_name = node['name']
vm_ip = node['ip']
config.vm.define vm_name do |node_config|
node_config.vm.network 'private_network', ip: "#{vm_ip}"
node_config.vm.box = "dummy"
node_config.vm.box_url = "./example_box/dummy.box"
node_config.vm.provider :vsphere do |vsphere|
vsphere.host = 'vc01.testlab.local'
vsphere.compute_resource_name = 'esxi01.testlab.local'
vsphere.name = vm_name
vsphere.customization_spec_name = 'centos66'
vsphere.template_name = 'PhotonTemplate'
vsphere.user = 'administrator@vsphere.local'
vsphere.password = 'password'
vsphere.insecure = true
end
end
end
end
This Vagrantfile has the ‘unique’ configuration of the VMs to be provisioned stated at the start of the file. The above example will create 2 VMs with the stated hostnames and IP addresses, but with the same hardware configuration.This is perfect for provisioning a lot of similar virtual machines quickly.