/
systemd.yml
180 lines (166 loc) · 6.31 KB
/
systemd.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
# linux_postinstall systemd
# Vars
- name: "systemd: Create empty dictionary my_services"
ansible.builtin.set_fact:
my_services: {}
tags:
- lp_systemd_debug
- lp_systemd_packages
- lp_systemd_conf
- name: "systemd: Set my_services"
ansible.builtin.set_fact:
my_services: "{{ my_services|
combine({my_name: {'state': my_state,
'restart_or_reload': my_restart_or_reload }}) }}"
loop: "{{ lp_systemd_unit }}"
loop_control:
label: "{{ my_name }}"
vars:
my_name: "{{ item.name }}.{{ item.type|default(lp_systemd_unit_type) }}"
my_state: "{{ item.control.state|default('started') }}"
my_restart_or_reload: "{{ item.control.restart_or_reload|default('reloaded') }}"
tags:
- lp_systemd_debug
- lp_systemd_packages
- lp_systemd_conf
# Debug
- name: "systemd: Debug"
vars:
msg: |-
ansible_os_family: {{ ansible_os_family }}
lp_systemd_install: {{ lp_systemd_install }}
lp_systemd_packages:
{{ lp_systemd_packages|to_nice_yaml(indent=2)|indent(2) }}
lp_systemd_dir: {{ lp_systemd_dir }}
lp_systemd_owner: {{ lp_systemd_owner }}
lp_systemd_group: {{ lp_systemd_group }}
lp_systemd_mode: {{ lp_systemd_mode }}
lp_systemd_conf_no_extra_spaces: {{ lp_systemd_conf_no_extra_spaces }}
lp_systemd_conf:
{{ lp_systemd_conf|to_yaml(indent=2)|indent(2) }}
lp_systemd_unit_dir: {{ lp_systemd_unit_dir }}
lp_systemd_unit_type: {{ lp_systemd_unit_type }}
lp_systemd_unit:
{{ lp_systemd_unit|to_nice_yaml(indent=2)|indent(2) }}
my_services:
{{ my_services|to_yaml(indent=2)|indent(2) }}
lp_backup_conf: {{ lp_backup_conf }}
ansible.builtin.debug:
msg: "{{ '{}'.format(msg) }}"
when: lp_systemd_debug|bool
tags: lp_systemd_debug
# Packages
- name: "systemd: Install packages"
ansible.builtin.import_role:
name: vbotka.linux_lib
tasks_from: install_package.yml
when: lp_systemd_install|bool
vars:
ll_ipkg_list: "{{ lp_systemd_packages }}"
ll_ipkg_state: "{{ lp_package_state }}"
ll_ipkg_autoremove: "{{ lp_packages_autoremove|bool }}"
ll_ipkg_rescue_end_host: "{{ lp_packages_rescue_end_host|bool }}"
ll_debug: "{{ lp_systemd_debug|bool }}"
tags: lp_systemd_packages
# Conf
- name: "systemd: Configure systemd"
community.general.ini_file:
path: "{{ item.0.key }}"
section: "{{ item.1.section }}"
option: "{{ item.1.key }}"
value: "{{ item.1.val }}"
owner: "{{ item.0.value.owner|default(lp_systemd_owner) }}"
group: "{{ item.0.value.group|default(lp_systemd_group) }}"
mode: "{{ item.0.value.mode|default(lp_systemd_mode) }}"
no_extra_spaces: "{{ lp_systemd_conf_no_extra_spaces }}"
backup: "{{ lp_backup_conf }}"
register: lp_systemd_conf_changes
with_subelements:
- "{{ lp_systemd_conf|dict2items }}"
- value.conf
loop_control:
label: "{{ item.0.key }} {{ item.1.section }} {{ item.1.key }} {{ item.1.val }}"
notify: reload systemd conf
tags: lp_systemd_conf
- name: "systemd: Debug conf results"
ansible.builtin.debug:
msg: "{{ lp_systemd_conf_changes.results }}"
when: lp_systemd_debug|bool
tags: lp_systemd_conf
- name: "systemd: Debug loop conf results"
ansible.builtin.debug:
msg: "{{ my_service }} {{ my_state }} {{ my_restart_or_reload }}"
loop: "{{ lp_systemd_conf_changes.results }}"
loop_control:
label: "{{ item.path|default('NA') }}" # 'lp_systemd: false' complains "no attribute 'path'"
vars:
my_service: "{{ item.item.0.value.reload_service }}"
my_state: "{{ my_services[my_service].state|default('started') }}"
my_restart_or_reload: "{{ my_services[my_service].restart_or_reload|default('reloaded') }}"
when:
- lp_systemd_debug|bool
- lp_systemd_conf_changes.results|default([])|length > 0
tags: lp_systemd_conf
# Units
- name: "systemd: Create units"
ansible.builtin.template:
src: systemd-unit.j2
dest: "{{ item.path|default(lp_systemd_unit_dir) }}/{{ item.name }}.{{ item.type|default(lp_systemd_unit_type) }}"
owner: "{{ item.owner|default(lp_systemd_owner) }}"
group: "{{ item.group|default(lp_systemd_group) }}"
mode: "{{ item.mode|default(lp_systemd_mode) }}"
backup: "{{ lp_backup_conf }}"
register: lp_systemd_unit_create_changes
loop: "{{ lp_systemd_unit }}"
loop_control:
label: "{{ item.name }}.{{ item.type|default(lp_systemd_unit_type) }}"
notify: reload systemd units
when: item.state|default('noop') == 'create'
tags: lp_systemd_unit_create
- name: "systemd: Debug unit create results"
ansible.builtin.debug:
var: lp_systemd_unit_create_changes
when: lp_systemd_debug|bool
tags: lp_systemd_unit_create
- name: "systemd: Remove units"
ansible.builtin.file:
state: absent
path: "{{ item.path|default(lp_systemd_unit_dir) }}/{{ item.name }}.{{ item.type|default(lp_systemd_unit_type) }}"
register: lp_systemd_unit_remove_changes
loop: "{{ lp_systemd_unit }}"
loop_control:
label: "{{ item.name }}.{{ item.type|default(lp_systemd_unit_type) }}"
when: item.state|default('noop') == 'absent'
tags: lp_systemd_unit_remove
- name: "systemd: Debug unit remove results"
ansible.builtin.debug:
var: lp_systemd_unit_remove_changes
when: lp_systemd_debug|bool
tags: lp_systemd_unit_remove
- name: "systemd: Control units"
ansible.builtin.systemd:
name: "{{ item.name }}.{{ item.type|default(lp_systemd_unit_type) }}"
daemon_reexec: "{{ item.control.daemon_reexec|default(omit) }}"
daemon_reload: "{{ item.control.daemon_reload|default(omit) }}"
enabled: "{{ item.control.enabled|default(omit) }}"
force: "{{ item.control.force|default(omit) }}"
masked: "{{ item.control.masked|default(omit) }}"
no_block: "{{ item.control.no_block|default(omit) }}"
scope: "{{ item.control.scope|default(omit) }}"
state: "{{ item.control.state|default(omit) }}"
register: lp_systemd_unit_control_changes
loop: "{{ lp_systemd_unit }}"
loop_control:
label: "{{ item.name }}.{{ item.type|default(lp_systemd_unit_type) }}"
when:
- item.control|default([])|length > 0
- not (item.name|regex_search('^(.*)@\.\w+$'))
tags: lp_systemd_unit_control
- name: "systemd: Debug unit control results"
ansible.builtin.debug:
var: lp_systemd_unit_control_changes
when: lp_systemd_debug|bool
tags: lp_systemd_unit_control
# EOF
...