Installing Hyper-V Linux Integration Services (LIS) with Ansible

Introduction

For the introduction to Ansible have a look at my previous blog post about updating you homelab with Ansible. There i go indepth in on the installation on Ubuntu 20.04 LTS and how to setup and configure inventory files and playbooks. This post can be found here Updating your Homelab with Ansible.

In this post i will explain my playbook for installing the Linux Integration Services on Ubuntu/Debian systems.

Hyper-V Linux Integration Services (LIS)

The Hyper-V Linux Integration Services also called LIS are a set of drivers that improve the integration between Hyper-V and Linux. Maybe some of you are already familiar with this concept from VMWare Tools on VMWare ESXi or QEMU guest agent on Proxmox. The LIS are basicly the same and will for example allow for better power management, IP address status in Hyper-V and better performance.

Installing Hyper-V Linux Integration Services (LIS) manually

First i will show the steps how to install the services then after i will show the Ansible playbook i made to simplify this setup.

Installing the Hyper-V Linux Integration Services is very straight forward.

First we edit the modules file.

sudo nano /etc/initramfs-tools/modules

In this file we add the following lines:
hv_vmbus
hv_storvsc
hv_blkvsc
hv_netvsc

After that we install the packages from apt.

sudo apt update
sudo apt install linux-virtual linux-cloud-tools-virtual linux-tools-virtual

Then we need to re-initialise the modules.

sudo update-initramfs -u

Finally we need to reboot the virtual machine.

sudo reboot

After rebooting we can check the LIS drivers are loaded with:

lsmod

Now look for the following modules:

hid_hyperv
hv_netvsc
hv_utils
hv_storvc
hv_vmbus

Installing Hyper-V Linux Integration Services (LIS) via Ansible playbook

Now we know how to install the Hyper-V Linux Integration Services (LIS). This proces can be streamlined with an Ansible. For this i created a playbook.

---
 - hosts: ubuntu
   gather_facts: true
   tasks:
   - name: Install Hyper-V LIS
     block:
     - name: Add modules to load
       lineinfile:
          state: present
          path: /etc/initramfs-tools/modules
          line: '{{ item }}'
       with_items:
         - 'hv_vmbus'
         - 'hv_storvsc'
         - 'hv_blkvsc'
         - 'hv_netvsc'
     - name: Install packages
       apt:
         name: ['linux-virtual', 'linux-cloud-tools-virtual', 'linux-tools-virtual']
         state: present
     - name: Update initramfs
       command: update-initramfs -u

This playbook basically follows all the manual steps. For this it uses the Ansible lineinfile, apt and command modules. The lineinfile makes sure the /etc/initramfs-tools/modules file is loaded with the correect items. Apt module makes sure the specified packages are installed and the command module executes a command on the shell.

Updated method for Ubuntu 16.04, 18.04 and 20.04 LTS

The Reddit user Doso777 pointed me to an updated method for the integration services for Ubuntu 16.04, 18.04 and 20.04 LTS. These can by easily installed via apt.

apt update
apt install linux-azure

For this we can also create a simple playbook.

---
 - hosts: ubuntu
   gather_facts: true
   tasks:
   - name: Install Hyper-V Linux Azure Kernel
     block:
     - name: Install packages
       apt:
         name: ['linux-azure']
         state: present

Conclusion

Using Ansible to install the LIS drivers saves alot of time especially if you need to install it on multiple systems. Leave comment below if you have any feedback.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *