Create simple reproducable VMs using libvirt virsh and community.libvirt. Created for testing my own Ansible recipes in virtual machine. This replaces Hashicorp Vagrant. The community has rightfully stopped supporting it due to licensing issues.
Predefined distros:
trixienoblealma8alma9alma10
2.0.1--- fix linting2.0.0--- rework after community.libvirt had a breaking change on minor1.2.0--- avoid race condition with temp files1.1.0--- added ability to use custom temp path1.0.0--- initial version
Required Python packages on all machines the role runs on:
libvirt-pythonlxml
Install on Debian:
sudo apt install python3-libvirt python3-lxmlOnly tested on Debian Trixie, but no limitations on which OS you can run role on.
Tested with ansible-core = "~=2.20.0".
- It is difficult to install
libvirt-pythonandlxmlin an virtual environment. SetPIPENV_SITE_PACKAGES=1to allow for using system packages in addition to your virtual environment. Can be set in.envwhen usingpipenv. - Temporary cloud images is not deleted on
state=absentto reduce downloads. - All images used must be
qcow2images that supportcloud-init.
libvirt_provision_connection--- A valid connection URL, defaultqemu:///system.libvirt_provision_distros--- Dictionary of cloud images. Each list element as below:arbitrary-name-- Name of distro profile.url--- Url to cloud image.os_variant--- Validvirt-install --os-variant listOS short-name.boot--- Boot mode, most commonuefi.sha--- Optional checksum of image.
libvirt_provision_storage_pool--- Libvirt storage pool for VM disks, defaultdefault.libvirt_provision_root_ssh_key--- SSH key to inject for root, default not set.libvirt_provision_root_password--- Root user password for console usage, default not set.libvirt_provision_temp_path--- Temp folder to store images and temporary files, defaults/tmp.libvirt_provision_virtual_machine_config--- Default VM configuration, see main.yml.libvirt_provision_virtual_machine_net_config--- Default cloud image network, see main.yml.libvirt_provision_virtual_machines--- List of virtual machines to create. Defaults, see below. For more advanced setup, see test.yml.- name: my-trixie-vm distro: trixie networks:
state--- Create or destroy VMs and disks withpresentandabsent, defaultpresent.
Requires installation of community.libvirt collection.
---
collections:
- name: community.libvirt
version: ">=2.0.0,<=2.1.0"---
- hosts: all
pre_tasks:
- name: Set variables that need to be set on localhost
ansible.builtin.set_fact:
libvirt_provision_root_ssh_key: "{{ lookup('file', lookup('env', 'HOME') + '/.ssh/id_ed25519.pub') }}"
delegate_to: localhost
roles:
- role: aheimsbakk.libvirt_provision
vars:
libvirt_provision_root_password: root
libvirt_provision_temp_path: '{{ lookup("env", "HOME") + "/tmp" }}'
libvirt_provision_virtual_machines:
- name: trixie01
distro: trixie
memory_mb: 2000
disk_gb: 20
vcpus: 2
net_config:
version: 2
ethernets:
enp1s0:
dhcp4: false
addresses:
- 192.168.122.11/24
routes:
- to: 0.0.0.0/0
via: 192.168.122.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
- name: alma01
distro: alma10
memory_mb: 3000
vcpus: 2
net_config:
version: 2
ethernets:
eth0:
addresses:
- 192.168.122.12/24
routes:
- to: 0.0.0.0/0
via: 192.168.122.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
- name: ubuntu01
distro: nobleEnter the role and install requirements.
mkdir .venv
pipenv install
pipenv shell
ansible-galaxy install -r tests/collections.yml
cd testsRunning the test.yml playbook expects you have .ssh/ed25519.pub SSH key available. Change the playbook if you use an RSA key.
Download cloud images and start VMs. state=present is implicit.
ansible-playbook -i inventory test.ymlRemoves VMs and disks.
ansible-playbook -i inventory test.yml -e state=absentGPL-2.0-or-later