Skip to content

Commit

Permalink
Merge pull request #51 from stackhpc/libvirt-config
Browse files Browse the repository at this point in the history
TCP/TLS, libvirtd.conf, qemu.conf, additional packages
  • Loading branch information
markgoddard committed Mar 3, 2022
2 parents 211b6e6 + 5211ff9 commit a4f0dcc
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 57 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,49 @@ daemon. Default is `true`.
`libvirt_host_install_client`: Whether to install and enable the libvirt
client. Default is `true`.

`libvirt_host_extra_daemon_packages`: List of additional packages to install on
libvirt daemon hosts.

`libvirt_host_extra_client_packages`: List of additional packages to install on
libvirt client hosts.

`libvirt_host_libvirtd_conf_enabled`: Whether to configure `libvirtd.conf`.
Default is `true`.

`libvirt_host_libvirtd_conf`: Configuration for `libvirtd.conf`. Dict mapping
option names to values. Default is an empty dict.

`libvirt_host_qemu_conf_enabled`: Whether to configure `qemu.conf`. Default is
`true`.

`libvirt_host_qemu_conf`: Configuration for `qemu.conf`. Dict mapping option
names to values. Default is an empty dict.

`libvirt_host_tcp_listen`: Whether to enable the systemd TCP socket unit.
Default is `false`.

`libvirt_host_tcp_listen_address`: Systemd TCP socket ListenStream. See man
systemd.socket for format. Default is unset.

`libvirt_host_tls_listen`: Whether to enable the systemd TLS socket unit.
Default is `false`.

`libvirt_host_tls_listen_address`: Systemd TLS socket ListenStream. See man
systemd.socket for format. Default is unset.

`libvirt_host_tls_server_cert`: TLS server certificate. Default is unset.

`libvirt_host_tls_server_key`: TLS server key. Default is unset.

`libvirt_host_tls_client_cert`: TLS client certificate. Default is unset.

`libvirt_host_tls_client_key`: TLS client key. Default is unset.

`libvirt_host_tls_cacert`: TLS CA certificate. Default is unset.

`libvirt_host_configure_apparmor`: Whether to configure AppArmor for directory
storage pools.

Dependencies
------------

Expand Down
35 changes: 35 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,38 @@ libvirt_host_install_daemon: true

# Whether to install and enable the libvirt client.
libvirt_host_install_client: true

# List of additional packages to install on libvirt daemon hosts.
libvirt_host_extra_daemon_packages: []

# List of additional packages to install on libvirt client hosts.
libvirt_host_extra_client_packages: []

# Whether to configure libvirtd.conf.
libvirt_host_libvirtd_conf_enabled: true
# Configuration for libvirtd.conf. Dict mapping option names to values.
libvirt_host_libvirtd_conf: {}

# Whether to configure qemu.conf.
libvirt_host_qemu_conf_enabled: true
# Configuration for qemu.conf. Dict mapping option names to values.
libvirt_host_qemu_conf: {}

# Whether to enable the systemd TCP socket unit.
libvirt_host_tcp_listen: false
# Systemd TCP socket ListenStream. See man systemd.socket for format.
libvirt_host_tcp_listen_address:

# Whether to enable the systemd TLS socket unit.
libvirt_host_tls_listen: false
# Systemd TLS socket ListenStream. See man systemd.socket for format.
libvirt_host_tls_listen_address:
# TLS server and client certificates.
libvirt_host_tls_server_cert:
libvirt_host_tls_server_key:
libvirt_host_tls_client_cert:
libvirt_host_tls_client_key:
libvirt_host_tls_cacert:

# Whether to configure AppArmor for directory storage pools.
libvirt_host_configure_apparmor: "{{ libvirt_host_install_daemon | bool }}"
29 changes: 27 additions & 2 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
---

- name: restart libvirt
- name: reload systemd
systemd:
daemon_reload: true
become: true

# The socket units cannot be stopped or started if libvirt is running.
- name: stop libvirt
service:
name: libvirtd
state: stopped
become: true
listen:
- restart libvirt

- name: start libvirtd sockets
service:
name: "{{ item.service }}"
state: "{{ item.enabled | bool | ternary('started', 'stopped') }}"
become: true
loop: "{{ _libvirt_socket_services }}"
loop_control:
label: "{{ item.service }}"
listen:
- restart libvirt

- name: start libvirt
service:
name: libvirtd
state: restarted
state: started
become: true

- name: reload libvirt qemu apparmor profile template
Expand Down
121 changes: 108 additions & 13 deletions tasks/config.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
---
# Configure services - runs after the install stage

- name: Set socket directory in libvirtd.conf
lineinfile:
path: /etc/libvirt/libvirtd.conf
insertafter: '^#unix_sock_dir ='
regexp: '^unix_sock_dir ='
line: unix_sock_dir = "{{ libvirt_host_socket_dir }}"
become: true
when: libvirt_host_socket_dir | length > 0
notify: restart libvirt

- name: Create directory for libvirt socket
file:
state: directory
Expand All @@ -31,12 +21,117 @@
notify:
- restart libvirt

- name: Ensure configuration files exist
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: root
group: root
mode: 0644
become: true
loop: "{{ _libvirt_config_files | selectattr('enabled') }}"
loop_control:
label: "{{ item.dest | basename }}"
vars:
_libvirt_config_files:
- src: libvirtd.conf.j2
dest: /etc/libvirt/libvirtd.conf
enabled: "{{ libvirt_host_libvirtd_conf_enabled | bool }}"
- src: qemu.conf.j2
dest: /etc/libvirt/qemu.conf
enabled: "{{ libvirt_host_qemu_conf_enabled | bool }}"
notify:
- restart libvirt

- name: Create systemd drop-in directory for socket listen address
file:
path: "/etc/systemd/system/{{ item.service }}.d"
state: directory
owner: root
group: root
mode: 0755
become: true
loop: "{{ _libvirt_socket_services | selectattr('enabled') }}"
when:
- item.listen_address is not none
- item.listen_address | length > 0
loop_control:
label: "{{ item.service }}"
vars:
_libvirt_listen_stream: "{{ item.listen_address }}"

- name: Configure socket listen address
template:
src: socket.j2
dest: "/etc/systemd/system/{{ item.service }}.d/listen-address.conf"
owner: root
group: root
mode: 0644
become: true
loop: "{{ _libvirt_socket_services | selectattr('enabled') }}"
when:
- item.listen_address is not none
- item.listen_address | length > 0
loop_control:
label: "{{ item.service }}"
vars:
_libvirt_listen_stream: "{{ item.listen_address }}"
notify:
- reload systemd
- restart libvirt

- name: Create directory for TLS certificates and keys
file:
path: "{{ item }}"
state: directory
owner: root
group: root
mode: 0700
become: true
loop: >-
{{ _libvirt_tls_certs.values() |
selectattr('content') |
map(attribute='dest') |
map('dirname') |
unique }}
when:
- libvirt_host_tls_listen | bool

- name: Copy TLS certificates and keys
copy:
content: "{{ _libvirt_loop_item.content }}"
dest: "{{ _libvirt_loop_item.dest }}"
owner: root
group: root
mode: "{{ _libvirt_loop_item.mode }}"
become: true
# NOTE: Loop over keys of _libvirt_tls_certs to avoid leaking the key
# contents.
loop: "{{ _libvirt_tls_certs.keys() }}"
when:
- libvirt_host_tls_listen | bool
- _libvirt_loop_item.content
vars:
_libvirt_loop_item: "{{ _libvirt_tls_certs[item] }}"
notify: restart libvirt

- name: Flush handlers
meta: flush_handlers

- name: Ensure the libvirt daemon is started and enabled
service:
name: libvirtd
state: started
enabled: yes
name: "{{ item.service }}"
state: "{{ item.enabled | bool | ternary('started', 'stopped') }}"
enabled: "{{ item.enabled | bool }}"
become: True
loop: "{{ _libvirt_services }}"
loop_control:
label: "{{ item.service }}"
vars:
_libvirt_services:
- service: libvirtd-tcp.socket
enabled: "{{ libvirt_host_tcp_listen | bool }}"
- service: libvirtd-tls.socket
enabled: "{{ libvirt_host_tls_listen | bool }}"
- service: libvirtd
enabled: true
2 changes: 1 addition & 1 deletion tasks/install-client.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: Ensure libvirt client packages are installed
package:
name: "{{ libvirt_host_libvirt_packages_client }}"
name: "{{ libvirt_host_libvirt_packages_client | select | list }}"
state: present
register: result
until: result is success
Expand Down
3 changes: 2 additions & 1 deletion tasks/install-daemon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
retries: 3
become: True

# NOTE: QEMU emulators are available in EPEL.
# NOTE: QEMU emulators are available in EPEL on CentOS 7.
- name: Ensure the EPEL repository is enabled
yum:
name: epel-release
Expand All @@ -32,6 +32,7 @@
become: True
when:
- ansible_facts.os_family == "RedHat"
- ansible_facts.distribution_major_version | int == 7
- libvirt_host_qemu_emulators | length > 0

- name: Ensure QEMU emulator packages are installed
Expand Down
23 changes: 15 additions & 8 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
---
- include: prelude.yml
- include: validate.yml
- include: install-daemon.yml
- import_tasks: prelude.yml
- import_tasks: validate.yml
- name: Include install-daemon.yml
include_tasks: install-daemon.yml
when: libvirt_host_install_daemon | bool
- include: install-client.yml
- name: Include install-client.yml
include_tasks: install-client.yml
when:
- not libvirt_host_install_daemon | bool
- libvirt_host_install_client | bool
- name: Run post-install stage
include: "{{ post_install_path }}"
include_tasks: "{{ post_install_path }}"
with_first_found:
- files:
- post-install-{{ ansible_facts.distribution }}.yml
- post-install-{{ ansible_facts.os_family }}.yml
skip: true
loop_control:
loop_var: post_install_path
- include: config.yml
- name: Include config.yml
include_tasks: config.yml
when: libvirt_host_install_daemon | bool
- include: pools.yml
- include: networks.yml
- name: Include pools.yml
include_tasks: pools.yml
when: libvirt_host_pools | length > 0
- name: Include networks.yml
include_tasks: networks.yml
when: libvirt_host_networks | length > 0
3 changes: 2 additions & 1 deletion tasks/pools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
loop: "{{ libvirt_host_pools | flatten(levels=1) }}"
become: True

- include_tasks:
- name: include rbd.yml
include_tasks:
file: rbd.yml
apply:
become: True
Expand Down
2 changes: 1 addition & 1 deletion tasks/post-install-Debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
line: " {{ item.path }}/** rwk,"
become: true
when:
- libvirt_host_install_daemon | bool
- libvirt_host_configure_apparmor | bool
- ansible_facts.apparmor.status | default == 'enabled'
- item.type == "dir"
loop: "{{ libvirt_host_pools | flatten(levels=1) }}"
Expand Down
8 changes: 8 additions & 0 deletions templates/libvirtd.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# {{ ansible_managed }}
{% if libvirt_host_socket_dir | length > 0 %}
unix_sock_dir = "{{ libvirt_host_socket_dir }}"
{% endif %}
{% for key, value in libvirt_host_libvirtd_conf.items() %}
{# While the value is not JSON formatted, it is close enough - strings need to be double quoted. #}
{{ key }} = {{ value | to_json }}
{% endfor %}
5 changes: 5 additions & 0 deletions templates/qemu.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# {{ ansible_managed }}
{% for key, value in libvirt_host_qemu_conf.items() %}
{# While the value is not JSON formatted, it is close enough - strings need to be double quoted. #}
{{ key }} = {{ value | to_json }}
{% endfor %}
4 changes: 4 additions & 0 deletions templates/socket.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# {{ ansible_managed }}
[Socket]
ListenStream=
ListenStream={{ _libvirt_listen_stream }}
13 changes: 3 additions & 10 deletions vars/Archlinux.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
# List of package dependencies common to all Debian distributions
# List of default daemon packages to install.
libvirt_host_libvirt_packages_default:
- libvirt
- qemu-headless
- ebtables
- dnsmasq

# List of all client packages to install.
libvirt_host_libvirt_packages_client:
# List of default client packages to install.
libvirt_host_libvirt_packages_client_default:
- libvirt
- libvirt-python
- python-lxml
Expand All @@ -16,13 +16,6 @@ libvirt_host_libvirt_packages_client:
libvirt_host_packages_efi:
- ovmf

# List of all packages to install
libvirt_host_libvirt_packages: >
{{ libvirt_host_libvirt_packages_default +
libvirt_host_libvirt_packages_client +
(libvirt_host_packages_efi if libvirt_host_enable_efi_support else []) | unique
}}
# Packages for RBD volume pool support
libvirt_host_packages_rbd_volume_pool:
- libvirt-storage-rbd
Expand Down
Loading

0 comments on commit a4f0dcc

Please sign in to comment.