Using Vagrant in Multi-Machine setup with libvirt provider

I am currently working on an Ansible project, which besides the configuration managemt, should automate the migration of an old system to a new hardware. My test environment is a Fedora system with many Linux KVM based virtual boxes. In order to develop and test the ansible scripts, I need a quick way to mass create a big number of virtual machines, run ansible on them and destroy them afterwards.

After some research, I have found out that Vagrant Multi-Machine configuration with libvirt provider (KVM) is what I am looking for.

Here is excerpt of the Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# name of the vm image
BOX_IMAGE = "sles11sp3"

Vagrant.configure("2") do |config|
config.ssh.insert_key = false
config.ssh.private_key_path = "/home/user/.ssh/id_rsa_vagrant"
config.ssh.username = "vagrant"
config.vm.synced_folder ".", "/vagrant", disabled: true

# libvirt provider configuration
config.vm.provider :libvirt do |lv|
lv.management_network_address = "192.168.5.0/24"
lv.cpus = 2
lv.memory = 2048
end

# virtual machines configuration
config.vm.define :web01 do |node|
node.vm.box = "sles11sp3"
node.vm.hostname = "web01"
node.vm.network "private_network", ip: "192.168.15.2"
node.vm.network "private_network", ip: "192.168.20.2"
end
config.vm.define :web02 do |node|
node.vm.box = "sles11sp3"
node.vm.hostname = "web02"
node.vm.network "private_network", ip: "192.168.15.3"
node.vm.network "private_network", ip: "192.168.20.3"
end
config.vm.define :oracle01 do |node|
node.vm.box = "sles11sp3"
node.vm.hostname = "oracle01"
node.vm.network "private_network", ip: "192.168.15.4"
node.vm.network "private_network", ip: "192.168.20.4"
end
config.vm.define :oracle02 do |node|
node.vm.box = "sles11sp3"
node.vm.hostname = "oracle02"
node.vm.network "private_network", ip: "192.168.15.5"
node.vm.network "private_network", ip: "192.168.20.5"
end
end

In order to start all machines, just run:

~$ vagrant up

In case you would like to start only a subset of machines e.g. all oracle nodes:

~$ vagrant up /oracle/

Vagrant Multi-Machine documentation is available at https://www.vagrantup.com/docs/multi-machine/

Leave a comment

Your email address will not be published. Required fields are marked *