Skip to content

Commit e1f2fdc

Browse files
committed
Make libvirtd.conf and qemu.conf configurable
Adds support for configuring libvirtd.conf via libvirt_host_libvirtd_conf and qemu.conf via libvirt_host_qemu_conf. This will overwrite any existing configuration in these files. Configuring these files may be avoided via libvirt_host_libvirtd_conf_enabled and libvirt_host_qemu_conf_enabled respectively.
1 parent d11ba10 commit e1f2fdc

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ daemon. Default is `true`.
103103
`libvirt_host_install_client`: Whether to install and enable the libvirt
104104
client. Default is `true`.
105105

106+
`libvirt_host_libvirtd_conf_enabled`: Whether to configure `libvirtd.conf`.
107+
Default is `true`.
108+
109+
`libvirt_host_libvirtd_conf`: Configuration for `libvirtd.conf`. Dict mapping
110+
option names to values. Default is an empty dict.
111+
112+
`libvirt_host_qemu_conf_enabled`: Whether to configure `qemu.conf`. Default is
113+
`true`.
114+
115+
`libvirt_host_qemu_conf`: Configuration for `qemu.conf`. Dict mapping option
116+
names to values. Default is an empty dict.
117+
106118
Dependencies
107119
------------
108120

defaults/main.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,13 @@ libvirt_host_install_daemon: true
7373

7474
# Whether to install and enable the libvirt client.
7575
libvirt_host_install_client: true
76+
77+
# Whether to configure libvirtd.conf.
78+
libvirt_host_libvirtd_conf_enabled: true
79+
# Configuration for libvirtd.conf. Dict mapping option names to values.
80+
libvirt_host_libvirtd_conf: {}
81+
82+
# Whether to configure qemu.conf.
83+
libvirt_host_qemu_conf_enabled: true
84+
# Configuration for qemu.conf. Dict mapping option names to values.
85+
libvirt_host_qemu_conf: {}

tasks/config.yml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
---
22
# Configure services - runs after the install stage
33

4-
- name: Set socket directory in libvirtd.conf
5-
lineinfile:
6-
path: /etc/libvirt/libvirtd.conf
7-
insertafter: '^#unix_sock_dir ='
8-
regexp: '^unix_sock_dir ='
9-
line: unix_sock_dir = "{{ libvirt_host_socket_dir }}"
10-
become: true
11-
when: libvirt_host_socket_dir | length > 0
12-
notify: restart libvirt
13-
144
- name: Create directory for libvirt socket
155
file:
166
state: directory
@@ -31,6 +21,28 @@
3121
notify:
3222
- restart libvirt
3323

24+
- name: Ensure configuration files exist
25+
template:
26+
src: "{{ item.src }}"
27+
dest: "{{ item.dest }}"
28+
owner: root
29+
group: root
30+
mode: 0644
31+
become: true
32+
loop: "{{ _libvirt_config_files | selectattr('enabled') }}"
33+
loop_control:
34+
label: "{{ item.dest | basename }}"
35+
vars:
36+
_libvirt_config_files:
37+
- src: libvirtd.conf.j2
38+
dest: /etc/libvirt/libvirtd.conf
39+
enabled: "{{ libvirt_host_libvirtd_conf_enabled | bool }}"
40+
- src: qemu.conf.j2
41+
dest: /etc/libvirt/qemu.conf
42+
enabled: "{{ libvirt_host_qemu_conf_enabled | bool }}"
43+
notify:
44+
- restart libvirt
45+
3446
- name: Flush handlers
3547
meta: flush_handlers
3648

templates/libvirtd.conf.j2

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# {{ ansible_managed }}
2+
{% if libvirt_host_socket_dir | length > 0 %}
3+
unix_sock_dir = "{{ libvirt_host_socket_dir }}"
4+
{% endif %}
5+
{% for key, value in libvirt_host_libvirtd_conf.items() %}
6+
{# While the value is not JSON formatted, it is close enough - strings need to be double quoted. #}
7+
{{ key }} = {{ value | to_json }}
8+
{% endfor %}

templates/qemu.conf.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# {{ ansible_managed }}
2+
{% for key, value in libvirt_host_qemu_conf.items() %}
3+
{# While the value is not JSON formatted, it is close enough - strings need to be double quoted. #}
4+
{{ key }} = {{ value | to_json }}
5+
{% endfor %}

0 commit comments

Comments
 (0)