An Ansible role to install and configure KVM/QEMU virtualization with libvirt.
- KVM/QEMU/libvirt installation and configuration
- Virtual machine management (create, start, stop, destroy)
- Virtual network configuration (bridge, NAT, Open vSwitch)
- Storage pool management
- Remote access via TCP/TLS
- User management for libvirt access
- Security driver configuration (SELinux, AppArmor)
- Libvirtd performance tuning
| Platform | Versions |
|---|---|
| Ubuntu | 14.04+, 20.04, 22.04 |
| Debian | 8+, 10, 11 |
| EL/CentOS/Rocky | 7, 8, 9 |
Install required Ansible roles:
ansible-galaxy install -r requirements.yml---
- hosts: kvm_hosts
become: true
roles:
- role: mrlesmithjr.kvm---
- hosts: kvm_hosts
become: true
vars:
kvm_config: true
kvm_config_virtual_networks: true
kvm_config_storage_pools: true
kvm_manage_vms: true
roles:
- role: mrlesmithjr.kvm| Variable | Default | Description |
|---|---|---|
kvm_config |
false |
Enable libvirt configuration |
kvm_manage_vms |
false |
Enable VM management |
kvm_images_path |
/var/lib/libvirt/images |
Path for VM disk images |
kvm_images_format_type |
qcow2 |
Default disk image format |
| Variable | Default | Description |
|---|---|---|
kvm_config_virtual_networks |
false |
Enable virtual network configuration |
kvm_enable_tcp |
false |
Enable unencrypted TCP connections |
kvm_enable_tls |
true |
Enable TLS connections |
kvm_listen_addr |
0.0.0.0 |
Address to bind for connections |
| Variable | Default | Description |
|---|---|---|
kvm_security_driver |
none |
Security driver (selinux, apparmor, none) |
kvm_disable_apparmor |
false |
Disable AppArmor for libvirt |
kvm_allow_root_ssh |
false |
Allow root SSH for remote management |
kvm_config_virtual_networks: true
kvm_virtual_networks:
# Bridge network
- name: br0-network
mode: bridge
bridge_name: br0
autostart: true
state: active
# Open vSwitch with VLANs
- name: ovs-network
mode: bridge
bridge_name: ovsbr0
autostart: true
state: active
virtualport_type: openvswitch
vlans:
- name: vlan-101
trunk: false
vlan: 101
- name: vlan-102
trunk: false
vlan: 102kvm_config_storage_pools: true
kvm_storage_pools:
- name: default
path: /var/lib/libvirt/images
autostart: true
state: active
- name: fast-storage
path: /ssd/vms
autostart: true
state: activekvm_manage_vms: true
kvm_vms:
- name: web-server
autostart: true
boot_devices:
- hd
- network
graphics: false
disks:
- disk_driver: virtio
name: web-server.qcow2
size: 51200 # Size in MB
memory: 2048
vcpu: 2
network_interfaces:
- source: default
network_driver: virtio
type: network
state: runningkvm_manage_vms: true
kvm_vms:
- name: new-vm
autostart: false
boot_devices:
- cdrom
- hd
cdrom:
source: /var/lib/libvirt/isos/ubuntu-22.04.iso
graphics: true
disks:
- disk_driver: virtio
name: new-vm.qcow2
size: 36864
memory: 4096
vcpu: 2
network_interfaces:
- source: default
network_driver: virtio
type: network
state: runningkvm_config_users: true
kvm_users:
- deployer
- developerSee defaults/main.yml for all available variables.
None (optional: mrlesmithjr.config-interfaces for bridge setup)
MIT
Larry Smith Jr.
