Skip to content

Latest commit

 

History

History
148 lines (116 loc) · 7.85 KB

File metadata and controls

148 lines (116 loc) · 7.85 KB

Ansible Role jm1.cloudy.libvirt_pools

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

Available on Ansible Galaxy in Collection jm1.cloudy.

Requirements

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.

Variables

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

Dependencies

None.

Example Playbook

- 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.

License

GNU General Public License v3.0 or later

See LICENSE.md to see the full text.

Author

Jakob Meng @jm1 (github, galaxy, web)

Footnotes

  1. If keys xml, xml_file or xml_var are NOT present in a list item, then the item, i.e. its key-value pairs, is passed to module jm1.libvirt.pool else it is passed to module jm1.libvirt.pool_xml. If key xml is present in a list item, then it will be passed unchanged to jm1.libvirt.pool_xml. If xml is not present but xml_file is present in a list item, then the file path stored in xml_file will be read with Ansible's lookup('template', item.xml_file) plugin and passed as parameter xml to module jm1.libvirt.pool_xml. If both xml and xml_var are not present in a list item but key xml_var is, then the variable named by xml_var will be read with Ansible's lookup('vars', item.xml_var) plugin and passed as parameter xml to module jm1.libvirt.pool_xml. If a list item does not contain key uri then it will be initialized from Ansible variables libvirt_uri. 2 3