How to Install KVM on CentOS 7/8 and Ubuntu 18/20

Getting Started

I really like testing new things in my home lab but I also often break things. I am really bored with installing my home lab over and over again so I decided to create a series of posts to show how I automated my home lab provisioning. There are tons of writings on how to install KVM on various operating systems and I ensure you what I implemented here is no magic or different than what others already documented. I just wanted to keep it in an Ansible module under my own GitHub repository and refer to it if I need some time in the future. I will also mention here how I like to structure my Ansible roles and my habits while I am automating things.

Special thanks to Jeff Geerling. I learned a lot from his work while I am writing my own Ansible roles

You can find my Ansible role in Ansible Galaxy here.

I usually test things under CentOS but after Red Hat's recent announcement of converting CentOS into a beta version of RHEL, I decided to give it a try with Ubuntu and added Ubuntu support to this role recently. The role covers operating systems below but I can add more in the future:

  • CentOS 7

  • CentOS 8

  • Ubuntu 18.x

  • Ubuntu 20.x

Prerequisites

I usually prefer to have a separate prerequisite play and include it in my main.yml. I start checking if the role is being run for the operating systems that I tested. Here I added another check and verified if the CPU of the server supports virtualization and it is indeed 64-bit.

tasks/prereq.yaml
---
- name: Setting OS version fact
  set_fact:
    osversion: "{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}"

- name: Check OS version
  fail:
    msg: "OS Version( {{ ansible_distribution }}{{ ansible_distribution_major_version }} ) is not certified for the role"
  when:
    - osversion != "ubuntu20"
    - osversion != "ubuntu18"
    - osversion != "centos7"
    - osversion != "centos8"

- name: Check CPU Virtualization Support
  shell:
    cmd: "egrep -c '(vmx|svm)' /proc/cpuinfo"
  args:
    warn: no
  changed_when: no
  register: cpuinfo_output
  tags: ['skip_ansible_lint']

- name: Check CPU Virtualization Support
  fail:
    msg: "CPU does not support virtualization"
  when:
    - cpuinfo_output.stdout == "0"

- name: Check if your processor is 64-bit
  shell:
    cmd: "egrep -c ' lm ' /proc/cpuinfo"
  args:
    warn: no
  changed_when: no
  register: bitinfo_output
  tags: ['skip_ansible_lint']

- name: Check if your processor is 64-bit
  fail:
    msg: "CPU is not 64-bit"
  when:
    - bitinfo_output.stdout == "0"

So now we can start actual work. Let me first introduce you the vars/main.yml file so you can understand how I structure the global variables I will use in the tasks.

First of all, you will need to decide whether you will use an existing user for virtualization administration. You can for sure go with the root user, but I prefer to keep my own user. Here I define a user with admin user name and set its password to passw0rd so you can feel free to change it. The remaining variables are actually the packages required by each operating system. I could add them in separate variable files but since the number of variables is not that much, I decided to keep them all in a single variable file.

vars/main.yml
---
use_libvirt_user: yes
libvirt_user: "admin"
libvirt_user_passwd: "passw0rd"

libvirt_packages:
  centos7:
    - qemu-kvm
    - libvirt
    - libvirt-python
    - libguestfs-tools
    - virt-install
    - virt-top
    - virt-manager
  centos8:
    - '@virt'
    - libguestfs-tools
    - virt-top
    - virt-manager
  ubuntu18:
    - qemu-kvm
    - libvirt-daemon-system
    - libvirt-clients
    - bridge-utils
    - virt-manager
  ubuntu20:
    - qemu-kvm
    - libvirt-daemon-system
    - libvirt-clients
    - bridge-utils
    - virt-manager

Installation on CentOS 7/8

The remaining of this section is no magic. We will just install the required packages and add our non-root user to the required libvirt groups if we opted to use a non-root user in the variable file.

tasks/centos.yaml
- name: Setting OS version fact
  set_fact:
    osversion: "{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}"

- name: Install yum-utils
  yum:
    name: yum-utils
    state: present

- name: Install Libvirt Packages
  yum:
    name: "{{ libvirt_packages[osversion] }}"
    state: present

- name: Add libvirt non-root user
  user:
    name: "{{ libvirt_user }}"
    shell: /bin/bash
    groups: libvirt
    append: yes
    password: "{{ libvirt_user_passwd | password_hash('sha512') }}"
    update_password: "on_create"
  when: use_libvirt_user | bool

- name: Enable and start libvirt service
  service:
    name: libvirtd
    state: started
    enabled: yes

Installation on Ubuntu 18.x / 20.x

The remaining of this section is no magic. We will just install the required packages and add our non-root user to the required libvirt groups if we opted to use a non-root user in the variable file.

tasks/ubuntu.yaml
- name: Setting OS version fact
  set_fact:
    osversion: "{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}"

- name: Install yum-utils
  yum:
    name: yum-utils
    state: present

- name: Install Libvirt Packages
  yum:
    name: "{{ libvirt_packages[osversion] }}"
    state: present

- name: Add libvirt non-root user
  user:
    name: "{{ libvirt_user }}"
    shell: /bin/bash
    groups: libvirt
    append: yes
    password: "{{ libvirt_user_passwd | password_hash('sha512') }}"
    update_password: "on_create"
  when: use_libvirt_user | bool

- name: Enable and start libvirt service
  service:
    name: libvirtd
    state: started
    enabled: yes

Putting things together

Let me also show how all above put together in the main.yml. I tend to keep main.yml as simple as possible and perform tasks under their dedicated plays.

tasks/main.yml
---
- name: CPU support checks
  include_tasks: prereq.yaml

- name: Install and Configure for CentOS
  include_tasks: centos.yaml
  when: ansible_distribution == "CentOS"

- name: Install and Configure for Ubuntu
  include_tasks: ubuntu.yaml
  when: ansible_distribution == "Ubuntu"

So we are ready to call this role if we want to install KVM on top of CentOS 7/8 or Ubuntu 18.x/20.x

It is important to run this module as root. Otherwise, it will not be able to install packages and will eventually fail.

Last updated