diff --git a/changelogs/fragments/mask_me.yml b/changelogs/fragments/mask_me.yml new file mode 100644 index 00000000000..57aac99aa9a --- /dev/null +++ b/changelogs/fragments/mask_me.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - systemd_service - handle mask operation failure (https://github.com/ansible/ansible/issues/81649). diff --git a/lib/ansible/modules/systemd_service.py b/lib/ansible/modules/systemd_service.py index 34aef891fd3..dc9e4fd3ffe 100644 --- a/lib/ansible/modules/systemd_service.py +++ b/lib/ansible/modules/systemd_service.py @@ -495,6 +495,8 @@ def main(): if rc != 0: # some versions of system CAN mask/unmask non existing services, we only fail on missing if they don't fail_if_missing(module, found, unit, msg='host') + # here if service was not missing, but failed for other reasons + module.fail_json(msg=f"Failed to {action} the service ({unit}): {err.strip()}") # Enable/disable service startup at boot if requested if module.params['enabled'] is not None: diff --git a/test/integration/targets/systemd/handlers/main.yml b/test/integration/targets/systemd/handlers/main.yml index 11053b7cb30..f0df2bb9e77 100644 --- a/test/integration/targets/systemd/handlers/main.yml +++ b/test/integration/targets/systemd/handlers/main.yml @@ -15,3 +15,8 @@ file: path: /etc/systemd/system/baz.service state: absent + +- name: remove mask unit file + file: + path: /etc/systemd/system/mask_me.service + state: absent diff --git a/test/integration/targets/systemd/tasks/main.yml b/test/integration/targets/systemd/tasks/main.yml index 4dc5d124402..503e4d95caf 100644 --- a/test/integration/targets/systemd/tasks/main.yml +++ b/test/integration/targets/systemd/tasks/main.yml @@ -121,3 +121,4 @@ - import_tasks: test_unit_template.yml - import_tasks: test_indirect_service.yml - import_tasks: test_enabled_runtime.yml +- import_tasks: test_mask.yml diff --git a/test/integration/targets/systemd/tasks/test_mask.yml b/test/integration/targets/systemd/tasks/test_mask.yml new file mode 100644 index 00000000000..1ab583c58a9 --- /dev/null +++ b/test/integration/targets/systemd/tasks/test_mask.yml @@ -0,0 +1,25 @@ +- name: Copy service file for mask operation + template: + src: mask_me.service + dest: /etc/systemd/system/mask_me.service + owner: root + group: root + mode: '0644' + notify: remove unit file + +- name: Reload systemd + systemd: + daemon_reload: true + +- name: Try to mask already masked service + systemd: + name: mask_me.service + masked: true + register: mask_test_1 + ignore_errors: true + +- name: Test mask service test + assert: + that: + - mask_test_1 is not changed + - "'Failed to mask' in mask_test_1.msg" diff --git a/test/integration/targets/systemd/templates/mask_me.service b/test/integration/targets/systemd/templates/mask_me.service new file mode 100644 index 00000000000..be858c74f1e --- /dev/null +++ b/test/integration/targets/systemd/templates/mask_me.service @@ -0,0 +1,9 @@ +[Unit] +Description=Mask Me Server +Documentation=Mask + +[Service] +ExecStart=/bin/yes + +[Install] +WantedBy=default.target