forked from vbotka/ansible-linux-postinstall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.yml
114 lines (104 loc) · 4.27 KB
/
service.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
---
# linux_postinstall service
# [TODO] add list of services, e.g. tlp and nm
# 'lp_' ~ item ~ '_services'
# [TODO] Mask a service. Do not allow any service to activate a masked
# service. See tasks/networkmanager.yml
# Collect vars and facts
- name: "service: Set dict my_service_name_vars. Collect lp_<item>_service."
ansible.builtin.set_fact:
my_service_name_vars: "{{ my_service_name_vars|default({})|
combine({item: lookup('vars', 'lp_' ~ item ~ '_service')}) }}"
loop: "{{ lp_service_enable }}"
when: lookup('vars', 'lp_' ~ item, default=false)
tags: [lp_service_debug, lp_service_sanity, lp_service_auto]
- name: "service: Set dict my_service_enable_vars. Collect lp_<item>_enable."
ansible.builtin.set_fact:
my_service_enable_vars: "{{ my_service_enable_vars|default({})|
combine({item: lookup('vars', 'lp_' ~ item ~ '_enable')}) }}"
loop: "{{ lp_service_enable }}"
when: lookup('vars', 'lp_' ~ item, default=false)
tags: [lp_service_debug, lp_service_sanity, lp_service_auto]
- name: "service: Get service_facts and set dict my_services"
block:
- name: "service: Get service_facts"
ansible.builtin.service_facts:
- name: "service: Set dict my_services"
ansible.builtin.set_fact:
my_services: "{{ {'sysv': my_services_sysv,
'systemd': my_services_systemd} }}"
vars:
my_services_sysv: "{{ ansible_facts.services|default({})|dict2items|
json_query('[?value.source==`sysv`].{name: value.name, state: value.service}')|
sort(attribute='name') }}"
my_services_systemd: "{{ ansible_facts.services|default({})|dict2items|
json_query('[?value.source==`systemd`].{name: value.name, state: value.service}')|
sort(attribute='name') }}"
when: lp_service_sanity|bool
tags: [lp_service_sanity, lp_service_auto]
- name: "service: Debug"
ansible.builtin.debug:
msg: "{{ '{}'.format(msg) }}"
vars:
msg: |-
lp_service_sanity: {{ lp_service_sanity }}
my_services:
{{ my_services|default({})|to_yaml|indent(2) }}
lp_service:
{{ lp_service|to_nice_yaml|indent(2) }}
lp_service_enable:
{{ lp_service_enable|to_nice_yaml|indent(2) }}
my_service_name_vars:
{{ my_service_name_vars|default({})|to_nice_yaml|indent(2) }}
my_service_enable_vars:
{{ my_service_enable_vars|default({})|to_nice_yaml|indent(2) }}
when: lp_service_debug|bool
tags: lp_service_debug
# Sanity
- name: "service: Debug sanity lp_service_enable"
ansible.builtin.debug:
msg:
- "{{ my_services|dict2items|json_query('[].value')|flatten|
map(attribute='name')|list|sort }}"
- "{{ my_service_name_vars.values()|list|sort }}"
when:
- lp_service_sanity|bool
- lp_service_debug|bool
tags: lp_service_debug
- name: "service: Sanity lp_service_enable. Service(s) undefined in the system."
ansible.builtin.fail:
msg: "[ERROR] Undefined services: {{ my_services_undef }}"
when:
- not ansible_check_mode
- lp_service_sanity|bool
- my_services_undef|length > 0
vars:
my_services_undef: "{{ my_service_name_vars.values()|list|
difference(my_services|dict2items|
json_query('[].value')|flatten|
map(attribute='name')|list) }}"
tags: [lp_service_sanity, lp_service_auto]
# Manage services
- name: "service: Manage services listed in lp_service_enable"
ansible.builtin.service:
name: "{{ my_service_name_vars[item] }}"
enabled: "{{ my_service_enable_vars[item] }}"
loop: "{{ lp_service_enable }}"
when: lookup('vars', 'lp_' ~ item, default=false)
tags: lp_service_auto
- name: "service: Manage services listed in lp_service"
ansible.builtin.service:
name: "{{ item.name }}"
state: "{{ item.state|default(omit) }}"
enabled: "{{ item.enabled|default(omit) }}"
arguments: "{{ item.arguments|default(omit) }}"
pattern: "{{ item.pattern|default(omit) }}"
runlevel: "{{ item.runlevel|default(omit) }}"
sleep: "{{ item.sleep|default(omit) }}"
use: "{{ item.use|default(omit) }}"
loop: "{{ lp_service }}"
when: (item.state is defined) or
(item.enabled is defined)
tags: lp_service_general
# EOF
...