Ansible Role kvm_vm
This role creates and manages virtual machines (VMs) on a KVM host.
By default, this role requires an OS image that has been modified using cloud-init and sysprep (to provide ssh keys or set the root password). Note: cloud-init and sysprep are only run when the boot disk is re-created.
If you want to create a VM with an existing disk, see the kvm_vm__existing_boot_disk
variable.
The role does not currently support resizing the VM.
Mandatory Requirements
Install Python 3, and the python3-libvirt and python3-lxml modules on the KVM host. This can be done using the linuxfabrik.lfops.python role. If you use the kvm_host Playbook to setup the KVM host, this is automatically done for you.
Place the base image in the
kvm_vm__pool
on the KVM host. Ifkvm_vm__pool
isdefault
, you get the storage path by runningvirsh pool-dumpxml default | grep -i path
on the KVM host.
Mandatory Role Variables
Variable |
Description |
---|---|
|
The base image file which will be used for the VM. Has to be placed in the |
|
The size to which the boot disk will be resized. This is required since we are using a base image. Should either be in bytes, or given using an optional suffix: k or K (kilobyte, 1024), M (megabyte, 1024k) and G (gigabyte, 1024M) and T (terabyte, 1024G) are supported. b is ignored. |
|
The KVM host. Will be used in |
|
Memory to allocate for the VM, in MiB. |
|
Number of virtual cpus to configure for the VM. |
Example:
# mandatory
kvm_vm__base_image: 'rocky8-base-image.qcow2'
kvm_vm__boot_disk_size: '50G'
kvm_vm__host: 'kvm-host.example.com'
kvm_vm__memory: 2048
kvm_vm__vcpus: 2
Optional Role Variables
Variable |
Description |
Default Value |
---|---|---|
|
A list of additional disks. They will be created in the
|
|
|
Whether the VM should be started on host boot up or not. |
|
|
String. See |
|
|
URL for connecting to the hypervisor on the |
|
|
A list of existing additional disks. They will not be modified, only added to the VM during creation. The disk have to be placed in the |
|
|
This allows to provide an already existing boot image, skipping the usage of a base image, and any modification to the disk. The disk has to be placed in the |
unset |
|
The run time maximum memory allocation of the VM. This is the maximum amount of memory that can be hot-plugged. |
|
|
The domain name of the VM. |
|
|
List of dictionaries of network connections to configure. Currently only supports ethernet devices (no bond/bridges/vlans). Subkeys: |
|
|
Set the operating system of the VM, will be used to optimise the guest configuration. Have a look at |
|
|
A list of packages which will be injected into the image using |
|
|
The KVM storage pool for the base image and disks. |
|
|
The root password of the VM. |
unset |
|
A list of keys which will be authorized to connect to the VM via SSH. |
|
|
The state of the VM. Possible options: |
|
Example:
# optional
kvm_vm__additional_disks:
- name: 'disk1'
size: '10G'
kvm_vm__autostart: true
# a more complex `--boot` parameter: boot using UEFI, but without Secure Boot (paths valid for RHEL)
kvm_vm__boot: 'loader=/usr/share/OVMF/OVMF_CODE.secboot.fd,loader.readonly=yes,loader.type=pflash,nvram.template=/usr/share/OVMF/OVMF_VARS.fd,loader_secure=no'
kvm_vm__existing_additional_disks:
- 'vm1-existing-disk1.qcow2'
- 'vm1-existing-disk2.qcow2'
kvm_vm__existing_boot_disk: 'vm1-existing-boot.qcow2'
kvm_vm__connect_url: 'qemu:///system'
kvm_vm__max_memory: '{{ kvm_vm__memory }}'
kvm_vm__name: '{{ inventory_hostname }}'
kvm_vm__network_connections:
- name: 'eth0'
addresses:
- '192.0.2.2'
gateway4: '192.0.2.1'
kvm_vm__osinfo: 'detect=on'
kvm_vm__packages:
- 'cloud-init'
- 'qemu-guest-agent'
kvm_vm__pool: 'default'
kvm_vm__root_password: 'linuxfabrik'
kvm_vm__ssh_authorized_keys: []
kvm_vm__state: 'running'