t2d.ansible_role_libvirt_vm
Libvirt VM
This role sets up and manages virtual machines (VMs) on a KVM hypervisor, including creating and deleting VMs.
Requirements
The host system must have Virtualization Technology (VT) enabled and should be configured with libvirt/KVM beforehand.
Role Variables
libvirt_vm_default_console_log_dir
: Default folder for VM console logs if a specific log path is not provided. Default is "/var/log/libvirt/qemu/".libvirt_vm_image_cache_path
: Folder to store cached downloaded images. Default is "/tmp/".libvirt_volume_default_images_path
: Folder for storing instance images. Default is '/var/lib/libvirt/images'.libvirt_volume_default_type
: Type of volume backing the instance. Default isvolume
.libvirt_volume_default_format
: Format for volumes created, default isqcow2
.libvirt_volume_default_device
: Specifies how the device appears in the guest OS. Defaults todisk
.libvirt_vm_engine
: Virtualization engine. If not specified, the role will try to choose the best one automatically.libvirt_vm_emulator
: Path to the emulator binary. If not specified, the role will try to detect it automatically.libvirt_cpu_mode_default
: The default CPU mode iflibvirt_cpu_mode
orvm.cpu_mode
is not defined.libvirt_vm_arch
: CPU architecture, default isx86_64
.libvirt_vm_uri
: Override the libvirt connection URI. Refer to the libvirt documentation for details.libvirt_vm_virsh_default_env
: Variables in this dictionary will be included in the environment when running virsh commands.libvirt_vm_clock_offset
: If provided, sets the instance's clock offset. If not defined, sync is set tolocaltime
.libvirt_vms
: List of VMs to be created or deleted. Each VM can have the following attributes:state
: Set topresent
to create orabsent
to delete the VM. Default ispresent
.name
: Name to assign to the VM.memory_mb
: Amount of memory for the VM in megabytes.vcpus
: Number of virtual CPU cores for the VM.machine
: Type of virtual machine. Default isNone
forlibvirt_vm_engine
askvm
, otherwisepc-1.0
.cpu_mode
: Virtual machine CPU mode. Default ishost-passthrough
forkvm
, otherwisehost-model
. Set to none to avoid configuring a CPU mode.clock_offset
: Overrides the default inlibvirt_vm_clock_offset
.enable_vnc
: If true, enables VNC listening on localhost for tools like VirtManager.volumes
: List of volumes to attach to the VM, defined with:pool
: Name or UUID of the storage pool for the volume.name
: Name for the volume being created; include an extension forfile
type volumes.file_path
: Location forfile
type volume images; defaults tolibvirt_volume_default_images_path
.device
: Eitherdisk
orcdrom
.capacity
: Size of the volume (can use M,G,T or MB,GB,TB, etc.) (required fordisk
type).format
: Options includeraw
,qcow2
,vmdk
. Default isqcow2
.image
: (optional) URL for initializing the volume image.backing_image
: (optional) Name of the backing volume; must be in the same pool (copy-on-write).image
andbacking_image
cannot be used together.target
: (optional) Manually specify volume type and order.
interfaces
: List of network interfaces for the VM, defined with:type
: Type of the interface. Options:network
: Connects to a named Libvirt virtual network (default).direct
: Connects directly to a host's physical interface usingmacvtap
.
network
: Name of the network for the interface; required if type isnetwork
.mac
: "Hardware" address for the virtual instance; if not provided, one is created.source
: A dict defining the host interface for this VM interface; required if type isdirect
:dev
: Name of the host interface for this VM interface.mode
: Options arevepa
,bridge
,private
, orpassthrough
. Default isvepa
.
console_log_enabled
: Iftrue
, logs console output to a file at the specifiedconsole_log_path
; otherwise, directs output to a PTY. Default isfalse
.console_log_path
: Path for the console log file. Default is{{ libvirt_vm_default_console_log_dir }}/{{ name }}-console.log
.start
: If true, starts the VM right after defining it. Default istrue
.autostart
: If true, starts the VM when the host starts. Default istrue
.xml_file
: Optionally provide a modified XML template for customization, based on the defaultvm.xml.j2
template.
Note: The following variables are outdated: libvirt_vm_state
, libvirt_vm_name
, libvirt_vm_memory_mb
, libvirt_vm_vcpus
, libvirt_vm_engine
, libvirt_vm_machine
, libvirt_vm_cpu_mode
, libvirt_vm_volumes
, libvirt_vm_interfaces
, and libvirt_vm_console_log_path
. If libvirt_vms
is not set, it defaults to a list containing a VM specification using these outdated variables.
Dependencies
If using qcow2 formatted drives, qemu-img
(included in qemu-utils package) is needed.
Example Playbook
---
- name: Create VMs
hosts: hypervisor
roles:
- role: stackhpc.libvirt-vm
libvirt_vms:
- state: present
name: 'vm1'
memory_mb: 512
vcpus: 2
volumes:
- name: 'data1'
device: 'disk'
format: 'qcow2'
capacity: '400GB'
pool: 'my-pool'
- name: 'debian-10.2.0-amd64-netinst.iso'
type: 'file'
device: 'cdrom'
format: 'raw'
target: 'hda' # First device on IDE bus
interfaces:
- network: 'br-datacentre'
- state: present
name: 'vm2'
memory_mb: 1024
vcpus: 1
volumes:
- name: 'data2'
device: 'disk'
format: 'qcow2'
capacity: '200GB'
pool: 'my-pool'
- name: 'filestore'
type: 'file'
file_path: '/srv/cloud/images'
capacity: '900GB'
interfaces:
- type: 'direct'
source:
dev: 'eth123'
mode: 'private'
- type: 'bridge'
source:
dev: 'br-datacentre'
Author Information
- Mark Goddard (mark@stackhpc.com)