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.
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.
I suppose you already have Ansible and Multipass installed on your computer. If not, don’t worry, I will guide through the process.
Because I’m on Linux, the commands below are specific to that operating system. Don’t forget to map for your operating system.
sudo snap install multipass
sudo apt update
sudo apt install python3-pip -y
sudo python3 -m pip install ansible
Install the collection
ansible-galaxy collection install theko2fi.multipass
Create a virtual machine
Now let’s create a
prepare.yml playbook file with the content below:
- name: Prepare
- name: Create a Multipass VM
Execute the playbook above with the command:
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.
inventory.yml file with the content below:
foois the name of the VM created previously
theko2fi.multipass.multipassis the name of the connection plugin
/usr/bin/python3is 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
- name: Test the connection
- name: Install apache2
- name: Upload web page
<title>My web page</title>
<p>This is my first web page.</p>
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 !
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
- name: Destroy
- name: Delete and purge a VM
Which can be executed as follows:
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: absentto delete the VM
purge: trueto remove any trace of the VM existence
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, give it a star on the GitHub repository.
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.