Libvirt Vagrant Template
is python script to generate Vagrantfile
that using libvirt
provider to provision virtual machines using defined specs in vagrant-specs.yaml
file
- Operating system:
Ubuntu 20.04.6 LTS
- Qemu-kvm version:
4.2-3ubuntu6.27
- Libvirtd version:
6.0.0
- Vagrant version:
2.4.0-1
- Libvirt plugins version:
0.12.2
Basically purpose of the script is to generate ready to use Vagrantfile
to provision single or multiple virtual machines with defined specs, like:
- Define hostname, username and password
- Define network name and ip address
- Define cpu and memory
- Define multiple disk with different size
- Define os image (vagrant box)
-
Install qemu, kvm, and libvirt. You can follow any tutorial in google
-
Install Vagrant, follow official install vagrant steps
-
Install vagrant libvirt plugin
vagrant plugin install vagrant-libvirt
- Install genisoimage for generate cloud-init.iso
sudo apt install genisoimage
Before using vagrant, we need to prepare ubuntu box
first. Usually, you can find the cloud images in official repo. Download whatever version you want to use, then use create_box.sh
script to create and add box
to vagrant.
- Create compressed box file
chmod +x create_box.sh
./create_box.sh <image file> <output filename>
- Add box file to vagrant
vagrant box add <output filename> --name <box name>
- Check vagrant box
vagrant box list
- Fill the
vagrant-specs.yaml
file with considered virtual machines specs you want to provision.
The file structure basically like this:
vagrant_specs:
vm_specs: -> vm lists
- hostname: -> vm hostname
network: -> vm network
box: -> vm vagrant box name (image/os)
username: -> vm username
password: -> vm password
vcpu: -> vm cpu core
memory: -> vm memory in MB
disks: -> vm list disks
- {<device>: <size>} -> vm disk
ip: -> vm private network ip address
pub_key: -> (optional) vm pub key for ssh, default using ~/.ssh/id_rsa.pub
- Generate
Vagrantfile
andcloud-init
iso file
python3 generate.py
- Provision the virtual machines
vagrant up
- Check vagrant virtual machines status
vagrant status
- Ssh to virtual machines
vagrant ssh <virtual machine name>
- If wanna make changes, first destroy current virtual machines. Make changes in
vagrant-specs.yaml
file, re-generateVagrantfile
then, provision the new virtual-machines
vagrant destroy
edit vagrant-specs.yaml
python3 generate.py
vagrant up