Manage your Multipass VMs with Ansible

Kenneth KOFFI
4 min readJul 29, 2023

--

I use Multipass to create virtual machines (VM) in my homelab. And, in most cases, it is followed by the execution of Ansible playbooks against these VMs in order to automate specific configurations. To be able to run Ansible playbooks against such VMs, I must always do the following pre-tasks:

  • create the VM by hands from the CLI (multipass launch ...)
  • copy my ssh key into the new VM
  • Create an Ansible inventory containing the IP address (which is dynamic) of my new VM
  • Set the correct ssh configs

I feel that it’s a bit hacky and I could get rid of these pre-tasks to simplify my life.

How?

I created an Ansible collection to control the whole lifecycle of Multipass instances (create, start, stop, delete…). It also makes connecting to Multipass VMs easier, without the hassle of ssh configurations. I made the collection available on Ansible Galaxy so that the community might use and benefit from my labor. In this article, I’ll show you how to use it to make your life easier.

Requirements

I suppose you already have Ansible and Multipass installed on your computer. If not, don’t worry, I will guide through the process.

Installation

Because I’m on Linux, the commands below are specific to that operating system. Don’t forget to map for your operating system.

Install Multipass

sudo snap install multipass

Install Ansible

sudo apt update
sudo apt install python3-pip -y
sudo python3 -m pip install ansible

Install the collection

ansible-galaxy collection install theko2fi.multipass

Usage

Create a virtual machine

Now let’s create a prepare.yml playbook file with the content below:

---
- name: Prepare
hosts: localhost
connection: local
tasks:
- name: Create a Multipass VM
theko2fi.multipass.multipass_vm:
name: foo
cpus: 2
memory: 2G
disk: 8G
state: started

Execute the playbook above with the command:

ansible-playbook prepare.yml

This will create and start a Multipass virtual machine (VM) named foo on your laptop with the specifications (CPU number, memory, disk…) present in the prepare.yml playbook file.

Run tasks in a virtual machine

The collection contains a connection plugin to run tasks on VMs.

Create an inventory.yml file with the content below:

all:
hosts:
foo:
ansible_host: foo
ansible_connection: theko2fi.multipass.multipass
ansible_python_interpreter: /usr/bin/python3

where:

  • foo is the name of the VM created previously
  • theko2fi.multipass.multipass is the name of the connection plugin
  • /usr/bin/python3 is the python path present inside the VM. This need to be specified if you’re on Windows.

Let’s create a simple playbook to test our connection. In my case, I will use the playbook.yml with the content below:

- name: Run a play in the multipass VM
hosts: foo
become: true
tasks:
- name: Test the connection
ansible.builtin.ping:
- name: Install apache2
ansible.builtin.apt:
name: apache2
update_cache: true
state: present
- name: Upload web page
ansible.builtin.copy:
content: |
<!DOCTYPE html>
<html>
<head>
<title>My web page</title>
</head>
<body>
<h1>Hello, world!</h1>
<p>This is my first web page.</p>
</body>
</html>
dest: /var/www/html/index.html

It’s a simple playbook which will install Apache on the VM and upload a custom welcome page. It can be run with the next command:

ansible-playbook -i inventory.yml playbook.yml

And that’s it, you’re now managing your Multipass instances natively from Ansible! You no longer need to deal with the hassle of SSH.

Destroy a virtual machine

Let’s say we’re done with a virtual machine, and we want to get rid of it. Then purge any associated data. We can use the playbook destroy.yml below:

- name: Destroy
hosts: localhost
connection: local
tasks:
- name: Delete and purge a VM
theko2fi.multipass.multipass_vm:
name: foo
state: absent
purge: true

Which can be executed as follows:

ansible-playbook destroy.yml

In that playbook, we reuse the theko2fi.multipass.multipass_vm module used in the previous section to create the VM. But now with different argument values:

  • name: foo(required) the name of the VM to delete
  • state: absent to delete the VM
  • purge: true to remove any trace of the VM existence

We’re done!

To deep dive into all the plugins and modules included in this collection, please check out the rich online documentation. You will find there more examples. And if you like the project, consider giving it a star on the GitHub repository to stay updated on upcoming releases.

Thank you for reading this article all the way to the end! I hope you found the information and insights shared here to be valuable and interesting. Get in touch with me on LinkedIn.

I appreciate your support and look forward to sharing more content with you in the future. Until next time!

This post also appears on my personal Blog.

Ansible manage Multipass virtual machines

--

--

Kenneth KOFFI

Administrateur systèmes avec une appétence pour le Cloud et le DevOps