Skip to content

Commit f2c6153

Browse files
committed
fix: unmask should run at the begin to allow the role to manage the units
Cause: The role was doing the unmask after attemtping to start the given unit. Consequence: The role was not able to both unmask and start a unit that was masked. Fix: The role does the unmask first before doing the start. Result: Masked units can be unmasked and started in a single role invocation. Signed-off-by: Rich Megginson <[email protected]>
1 parent 9fbffa4 commit f2c6153

File tree

2 files changed

+109
-8
lines changed

2 files changed

+109
-8
lines changed

tasks/main.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,22 @@
136136
loop_control:
137137
loop_var: managed_units
138138
loop:
139+
- name: systemd_unmasked_units
140+
masked: "false"
141+
- name: systemd_enabled_units
142+
enabled: "true"
139143
- name: systemd_started_units
140144
state: started
141-
- name: systemd_stopped_units
142-
state: stopped
143145
- name: systemd_restarted_units
144146
state: restarted
145147
- name: systemd_reloaded_units
146148
state: reloaded
147-
- name: systemd_enabled_units
148-
enabled: "true"
149-
- name: systemd_disabled_units
150-
enabled: "false"
149+
- name: systemd_stopped_units
150+
state: stopped
151151
- name: systemd_masked_units
152152
masked: "true"
153-
- name: systemd_unmasked_units
154-
masked: "false"
153+
- name: systemd_disabled_units
154+
enabled: "false"
155155

156156
- name: DebugBefore
157157
debug:

tests/tests_basic.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,104 @@
210210
- name: Foo.service shouldn't be in systemd_units
211211
fail:
212212
when: ansible_facts['systemd_units']['foo.service'] is defined
213+
214+
- name: Test unmask and start
215+
hosts: all
216+
gather_facts: false
217+
vars:
218+
# we need a
219+
# * system service provided in /usr/lib/systemd/system
220+
# * service exists on all platforms
221+
# * service can be masked/stopped without breaking the test
222+
# e.g. sshd is used by the test - stopping will break test
223+
# look for these units in order
224+
test_units:
225+
- crond.service
226+
- systemd-hostnamed.service
227+
- systemd-journald.service
228+
test_unit: crond.service
229+
tasks:
230+
- name: Get list of services that exist
231+
shell:
232+
executable: /bin/bash
233+
cmd: |
234+
set -euo pipefail
235+
for unit in {{ test_units | join(" ") }}; do
236+
if systemctl list-unit-files "$unit" > /dev/null 2>&1; then
237+
echo "$unit"
238+
exit 0
239+
fi
240+
done
241+
exit 1
242+
changed_when: false
243+
register: __find_test_unit
244+
245+
- name: Set test unit
246+
set_fact:
247+
test_unit: "{{ __find_test_unit.stdout | trim }}"
248+
249+
- name: Ensure test unit is running and unmasked
250+
include_role:
251+
name: linux-system-roles.systemd
252+
vars:
253+
systemd_started_units:
254+
- "{{ test_unit }}"
255+
systemd_unmasked_units:
256+
- "{{ test_unit }}"
257+
258+
- name: Get test unit state
259+
# noqa command-instead-of-module
260+
command: systemctl show -p UnitFileState -p SubState "{{ test_unit }}"
261+
register: test_unit_state
262+
changed_when: false
263+
264+
- name: Ensure test unit running and unmasked
265+
assert:
266+
that:
267+
- test_unit_state.stdout is search("UnitFileState=enabled") or
268+
test_unit_state.stdout is search("UnitFileState=static")
269+
- test_unit_state.stdout is search("SubState=running")
270+
271+
- name: Stop and mask test unit
272+
include_role:
273+
name: linux-system-roles.systemd
274+
vars:
275+
systemd_stopped_units:
276+
- "{{ test_unit }}"
277+
systemd_masked_units:
278+
- "{{ test_unit }}"
279+
280+
- name: Get test unit state
281+
# noqa command-instead-of-module
282+
command: systemctl show -p UnitFileState -p SubState "{{ test_unit }}"
283+
register: test_unit_state
284+
changed_when: false
285+
286+
- name: Ensure test unit stopped and masked
287+
assert:
288+
that:
289+
- test_unit_state.stdout is search("UnitFileState=masked") or
290+
test_unit_state.stdout is search("UnitFileState=bad")
291+
- test_unit_state.stdout is search("SubState=dead")
292+
293+
- name: Ensure test unit is running and unmasked
294+
include_role:
295+
name: linux-system-roles.systemd
296+
vars:
297+
systemd_started_units:
298+
- "{{ test_unit }}"
299+
systemd_unmasked_units:
300+
- "{{ test_unit }}"
301+
302+
- name: Get test unit state
303+
# noqa command-instead-of-module
304+
command: systemctl show -p UnitFileState -p SubState "{{ test_unit }}"
305+
register: test_unit_state
306+
changed_when: false
307+
308+
- name: Ensure test unit running and unmasked
309+
assert:
310+
that:
311+
- test_unit_state.stdout is search("UnitFileState=enabled") or
312+
test_unit_state.stdout is search("UnitFileState=static")
313+
- test_unit_state.stdout is search("SubState=running")

0 commit comments

Comments
 (0)