This role helps with managing libvirt storage pools from Ansible variables. For example, it allows to create
and delete storage pools with variable libvirt_images
. This variable is defined as a list where each list item is a
dictionary of parameters that will be passed to module jm1.libvirt.pool
or
jm1.libvirt.pool_xml
from collection jm1.libvirt
1. For example, to create libvirt storage pool default
with the local libvirt daemon (run by
current Ansible user on the Ansible controller), define variable libvirt_pools
in group_vars
or host_vars
as such:
# connect from Ansible controller to local libvirt daemon
ansible_connection: local
libvirt_pools:
- autostart: true
hardware:
- type: dir
- target: '{{ ansible_env.HOME }}/.local/share/libvirt/images'
name: 'default'
# libvirt connection uri
# Ref.: https://libvirt.org/uri.html
libvirt_uri: 'qemu:///session'
When this role is executed, it will pass each item of the libvirt_pools
list one after another as parameters to module
jm1.libvirt.pool
from collection jm1.libvirt
1.
If a libvirt storage pool with the same name already exists, it will be updated if necessary. If a list item does not
contain key autostart
or if its set to true
then module community.libvirt.virt_pool
from collection community.libvirt
will be used to mark that storage pool to be started
automatically when the libvirt daemon starts. At the end the same module will be used to stop and restart storage pools
automatically to apply pending changes at runtime.
If any list item in libvirt_pools
has its key state
set to absent
then module community.libvirt.virt_pool
will be used to stop (destroy) and delete (undefine) this storage pool.
Tested OS images
- Cloud image (
amd64
) of Debian 10 (Buster) - Cloud image (
amd64
) of Debian 11 (Bullseye) - Cloud image (
amd64
) of Debian 12 (Bookworm) - Cloud image (
amd64
) of Debian 13 (Trixie) - Cloud image (
amd64
) of CentOS 7 (Core) - Cloud image (
amd64
) of CentOS 8 (Stream) - Cloud image (
amd64
) of CentOS 9 (Stream) - Cloud image (
amd64
) of Fedora Cloud Base 40 - Cloud image (
amd64
) of Ubuntu 18.04 LTS (Bionic Beaver) - Cloud image (
amd64
) of Ubuntu 20.04 LTS (Focal Fossa) - Cloud image (
amd64
) of Ubuntu 22.04 LTS (Jammy Jellyfish) - Cloud image (
amd64
) of Ubuntu 24.04 LTS (Noble Numbat)
Available on Ansible Galaxy in Collection jm1.cloudy.
This role uses module(s) from collections community.libvirt
and jm1.libvirt
. To install these collections you may follow the steps described in README.md
using the provided requirements.yml
.
Name | Default value | Required | Description |
---|---|---|---|
libvirt_pools |
[] |
false | List of parameter dictionaries for module jm1.libvirt.pool or jm1.libvirt.pool_xml from collection jm1.libvirt 1 |
libvirt_uri |
qemu:///system |
false | libvirt connection uri |
None.
- hosts: all
vars:
# Variables are listed here for convenience and illustration.
# In a production setup, variables would be defined e.g. in
# group_vars and/or host_vars of an Ansible inventory.
# Ref.:
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
# https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
# connect from Ansible controller to local libvirt daemon
ansible_connection: local
libvirt_pools:
- autostart: true
hardware:
- type: dir
- target: '{{ ansible_env.HOME }}/.local/share/libvirt/images'
name: 'default'
# libvirt connection uri
# Ref.: https://libvirt.org/uri.html
libvirt_uri: 'qemu:///session'
roles:
- name: Setup libvirt storage pools
role: jm1.cloudy.libvirt_pools
tags: ["jm1.cloudy.libvirt_pools"]
For more examples on how to use this role, refer to hosts lvrt-lcl-session
, lvrt-lcl-system
and variable
libvirt_pools
as defined in group_vars/svc_libvirt.yml
from the provided examples inventory.
The top-level README.md
describes how these hosts can be provisioned with playbook
playbooks/site.yml
.
For instructions on how to run Ansible playbooks have look at Ansible's Getting Started Guide.
GNU General Public License v3.0 or later
See LICENSE.md to see the full text.
Jakob Meng @jm1 (github, galaxy, web)
Footnotes
-
If keys
xml
,xml_file
orxml_var
are NOT present in a list item, then the item, i.e. its key-value pairs, is passed to modulejm1.libvirt.pool
else it is passed to modulejm1.libvirt.pool_xml
. If keyxml
is present in a list item, then it will be passed unchanged tojm1.libvirt.pool_xml
. Ifxml
is not present butxml_file
is present in a list item, then the file path stored inxml_file
will be read with Ansible'slookup('template', item.xml_file)
plugin and passed as parameterxml
to modulejm1.libvirt.pool_xml
. If bothxml
andxml_var
are not present in a list item but keyxml_var
is, then the variable named byxml_var
will be read with Ansible'slookup('vars', item.xml_var)
plugin and passed as parameterxml
to modulejm1.libvirt.pool_xml
. If a list item does not contain keyuri
then it will be initialized from Ansible variableslibvirt_uri
. ↩ ↩2 ↩3