Automating Linux VM Deployments on KVM Hosts
Using Ansible Deploying Linux VM on KVM host
Install KVM on target nodes (Managed Linux Hosts)
:::warning Ensure Target Node Supports Virtualization :::
On RHEL based distribution
sudo dnf update
sudo dnf install @virt
sudo systemctl enable --now libvirtd
sudo usermod -aG libvirt $USER
On Debian based distributions
sudo apt update
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
sudo systemctl enable --now libvirtd
sudo usermod -aG libvirt $USER
Setting up Ansible on controller node
Installing Ansible on RHEL based distribution
Installing Ansible on Debian based distributions
Configuring Ansible Controller node
Creating Inventory file
Add the content
Test connectivity to target nodes from ansible controller
:::info Ensure SSH connectivity is enabled from ansible controller to KVM node :::
Importing Ansible Module
Creating playbook for VM deployment
Paste the content
- name: Deploy VM
hosts: localhost
become: yes
vars_files:
- vm_definition.yml
tasks:
- name: Deploy VM Using Ansible Collection Role
include_role:
name: vm_deploy
loop: "{{ vm }}"
Creating VM definition file
Paste the contents
# vm_vars.yml
vm:
- name: vm02 # Required
hostname: linux # Optional, Default: linux
domain: domain.local # Optional, Default domain: .local
os: centos9 # Required, Refer OS Section
memory: 2048 # Optional, Default Size is 2048 GiB
vcpu: 2 # Optional, Default number of CPU is 2
disk_size: 20 # Optional, Default Size is 10 GiB
users:
- name: user
password: "password" # Optional, Default is test123
groups: sudo, admin # Optional
ssh_key: # Optional
- "SSH_key" # Multiple SSH-Keys Can be added
# Multiple users blocks can be added here
networks: # Whole Network Block is Optional
- name: default # Network Name is the one that is Defined on KVM host
ip:
- 192.168.122.11/24 # Optional, Default DHCP (Multiple IP for single NIC can be added)
gateway: 192.168.122.1 # Optional, No Default, If not specified no internet access
dns1: 1.1.1.1 # Optional
dns2: 8.8.8.8 # Optional, If no DNS Specified, Default: 1.1.1.1
## Multiple Network Blocks Can be added
packages:
- vim
- curl
## List of Packages to be installed can be added
# Multiple VM Blocks can be added here
Running Playbook
Variables Selection in VM definition
OS Selection
Below mentioned are the available OS for deployment, use the corresponding key as a value for os in VM definition file
| OS | Key |
|---|---|
| Ubuntu 24.04 LTS | ubuntu24.04 |
| Ubuntu 22.04 LTS | ubuntu22.04 |
| Ubuntu 20.04 | ubuntu20.04 |
| CentOS 10 Stream | centos10 |
| CentOS 9 Stream | centos9 |
| CentOS 8 Stream | centos 8 |
| Centos 7 | centos 7 |
| Debian 13 Trixie | debian13 |
| Debian 12 | debian12 |
| Debian 11 | debian11 |
| Alma Linux 10 | almalinux10 |
| Alma Linux 9 | almalinux9 |
| Alma Linux 8 | almalinux8 |
| Rocky Linux 10 | rockylinux10 |
| Rocky Linux 9 | rockylinux9 |
| Rocky Linux 8 | rockylinux8 |
Other Features
- No downloading of images is required
- Adding Multiple VM definitions in same file for multiple VM deployment in Single play
- Adding Multiple Users Per VM
- Adding Multiple Network Interfaces with Different Configuration
- Adding List of packages to be installed on the deploying VM
:::danger At the moment, Debian 11 image gets deployed as VM, but configurations may not take effect :::