Ansible Playbook: Deploying Canonical Ubuntu vms
Recently I’ve created a playbook to help me with my daily routine to create labs and configurations for Ubuntu based environment.
First step:
Remember: You must install the libvirt, ansible and git packages for the instalation
Version of packages:
libvirt 8
ansible 2.10
- Create the template Download the ubuntu 20 or 22 for template configuration
inside of the util directory there are some files to help with:
- netlab1.xml: to configure the invernal libvirt network, virsh net-import netlab1.xml.
- virt_install.sh: the script to run and prepare the template vm, this step is interactive with the terminal to configure everything. e.g: name, ip, hostname…
Setting the variables
Inside of inventory/host_vars/localhost.yml you must to setup some variables:
img_template: the template image name without extension template_address: the ip address set to the interface
for vm section you must inform ram, vcpu and net_type for networking selection(bridge or virtual network), and net_connector is the network name for virtual network or the bridge name for brige.
img_template: "ubuntu20.04"
template_address: "192.168.200.10"
osvariant: ubuntu20.04
vm:
- name: ubuntu1
cpu: 1
mem: 2048
net_type: "network"
net_connector: "netlab1"
net:
ip: 192.168.200.11
mask: 255.255.255.0
gateway: 192.168.200.1
dns: 192.168.200.1
- name: ubuntu2
cpu: 1
mem: 2048
net_type: "network"
net_connector: "netlab1"
net:
ip: 192.168.200.12
mask: 255.255.255.0
gateway: 192.168.200.1
dns: 192.168.200.1
Deploying the vms
clone the repository and enter into the ubuntu directory
git clone https://github.com/fabioabreureis/ansible_libvirt
cd ubuntu
ansible-playbook -i inventory/hosts ubuntu_vms.yml
run the playbook
ansible-playbook -i inventory/hosts ubuntu_vms.yml
you can destroy the vms with :
ansible-playbook -i inventory/hosts destroy.yml
Examples
Creating the vms
Destroying the vms