|
1 | 1 | ---
|
2 | 2 | # Configure services - runs after the install stage
|
3 | 3 |
|
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 |
| - |
14 | 4 | - name: Create directory for libvirt socket
|
15 | 5 | file:
|
16 | 6 | state: directory
|
|
31 | 21 | notify:
|
32 | 22 | - restart libvirt
|
33 | 23 |
|
| 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 | + |
| 46 | +- name: Create systemd drop-in directory for socket listen address |
| 47 | + file: |
| 48 | + path: "/etc/systemd/system/{{ item.service }}.d" |
| 49 | + state: directory |
| 50 | + owner: root |
| 51 | + group: root |
| 52 | + mode: 0755 |
| 53 | + become: true |
| 54 | + loop: "{{ _libvirt_socket_services | selectattr('enabled') }}" |
| 55 | + when: |
| 56 | + - item.listen_address is not none |
| 57 | + - item.listen_address | length > 0 |
| 58 | + loop_control: |
| 59 | + label: "{{ item.service }}" |
| 60 | + vars: |
| 61 | + _libvirt_listen_stream: "{{ item.listen_address }}" |
| 62 | + |
| 63 | +- name: Configure socket listen address |
| 64 | + template: |
| 65 | + src: socket.j2 |
| 66 | + dest: "/etc/systemd/system/{{ item.service }}.d/listen-address.conf" |
| 67 | + owner: root |
| 68 | + group: root |
| 69 | + mode: 0644 |
| 70 | + become: true |
| 71 | + loop: "{{ _libvirt_socket_services | selectattr('enabled') }}" |
| 72 | + when: |
| 73 | + - item.listen_address is not none |
| 74 | + - item.listen_address | length > 0 |
| 75 | + loop_control: |
| 76 | + label: "{{ item.service }}" |
| 77 | + vars: |
| 78 | + _libvirt_listen_stream: "{{ item.listen_address }}" |
| 79 | + notify: |
| 80 | + - reload systemd |
| 81 | + - restart libvirt |
| 82 | + |
| 83 | +- name: Create directory for TLS certificates and keys |
| 84 | + file: |
| 85 | + path: "{{ item }}" |
| 86 | + state: directory |
| 87 | + owner: root |
| 88 | + group: root |
| 89 | + mode: 0700 |
| 90 | + become: true |
| 91 | + loop: >- |
| 92 | + {{ _libvirt_tls_certs.values() | |
| 93 | + selectattr('content') | |
| 94 | + map(attribute='dest') | |
| 95 | + map('dirname') | |
| 96 | + unique }} |
| 97 | + when: |
| 98 | + - libvirt_host_tls_listen | bool |
| 99 | + |
| 100 | +- name: Copy TLS certificates and keys |
| 101 | + copy: |
| 102 | + content: "{{ _libvirt_loop_item.content }}" |
| 103 | + dest: "{{ _libvirt_loop_item.dest }}" |
| 104 | + owner: root |
| 105 | + group: root |
| 106 | + mode: "{{ _libvirt_loop_item.mode }}" |
| 107 | + become: true |
| 108 | + # NOTE: Loop over keys of _libvirt_tls_certs to avoid leaking the key |
| 109 | + # contents. |
| 110 | + loop: "{{ _libvirt_tls_certs.keys() }}" |
| 111 | + when: |
| 112 | + - libvirt_host_tls_listen | bool |
| 113 | + - _libvirt_loop_item.content |
| 114 | + vars: |
| 115 | + _libvirt_loop_item: "{{ _libvirt_tls_certs[item] }}" |
| 116 | + notify: restart libvirt |
| 117 | + |
34 | 118 | - name: Flush handlers
|
35 | 119 | meta: flush_handlers
|
36 | 120 |
|
37 | 121 | - name: Ensure the libvirt daemon is started and enabled
|
38 | 122 | service:
|
39 |
| - name: libvirtd |
40 |
| - state: started |
41 |
| - enabled: yes |
| 123 | + name: "{{ item.service }}" |
| 124 | + state: "{{ item.enabled | bool | ternary('started', 'stopped') }}" |
| 125 | + enabled: "{{ item.enabled | bool }}" |
42 | 126 | become: True
|
| 127 | + loop: "{{ _libvirt_services }}" |
| 128 | + loop_control: |
| 129 | + label: "{{ item.service }}" |
| 130 | + vars: |
| 131 | + _libvirt_services: |
| 132 | + - service: libvirtd-tcp.socket |
| 133 | + enabled: "{{ libvirt_host_tcp_listen | bool }}" |
| 134 | + - service: libvirtd-tls.socket |
| 135 | + enabled: "{{ libvirt_host_tls_listen | bool }}" |
| 136 | + - service: libvirtd |
| 137 | + enabled: true |
0 commit comments