From 00ead98595577f110165d2c87c6c5c38c27b4b74 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Tue, 2 Jun 2020 18:52:12 +0300 Subject: [PATCH] systemd: should fail in check_mode when service not found on host (#68136) * systemd: should fail in check_mode when service not found on host --- ...l_in_check_mode_when_service_not_found.yml | 2 ++ lib/ansible/module_utils/service.py | 5 +--- .../targets/service/tasks/tests.yml | 29 +++++++++++++++++++ .../targets/systemd/defaults/main.yml | 1 + .../targets/systemd/tasks/main.yml | 25 ++++++++++++++++ 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/68136-systemd_should_fail_in_check_mode_when_service_not_found.yml create mode 100644 test/integration/targets/systemd/defaults/main.yml diff --git a/changelogs/fragments/68136-systemd_should_fail_in_check_mode_when_service_not_found.yml b/changelogs/fragments/68136-systemd_should_fail_in_check_mode_when_service_not_found.yml new file mode 100644 index 00000000000..eee15151f4d --- /dev/null +++ b/changelogs/fragments/68136-systemd_should_fail_in_check_mode_when_service_not_found.yml @@ -0,0 +1,2 @@ +bugfixes: +- systemd - the module should fail in check_mode when service not found on host (https://github.com/ansible/ansible/pull/68136). diff --git a/lib/ansible/module_utils/service.py b/lib/ansible/module_utils/service.py index 9b0c65310ab..3e57b0217bb 100644 --- a/lib/ansible/module_utils/service.py +++ b/lib/ansible/module_utils/service.py @@ -114,10 +114,7 @@ def fail_if_missing(module, found, service, msg=''): :kw msg: extra info to append to error/success msg when missing ''' if not found: - if module.check_mode: - module.exit_json(msg="Service %s not found on %s, assuming it will exist on full run" % (service, msg), changed=True) - else: - module.fail_json(msg='Could not find the requested service %s: %s' % (service, msg)) + module.fail_json(msg='Could not find the requested service %s: %s' % (service, msg)) def fork_process(): diff --git a/test/integration/targets/service/tasks/tests.yml b/test/integration/targets/service/tasks/tests.yml index fd66918b076..de66bf5ca7f 100644 --- a/test/integration/targets/service/tasks/tests.yml +++ b/test/integration/targets/service/tasks/tests.yml @@ -194,3 +194,32 @@ that: - "remove_result.path == '/usr/sbin/ansible_test_service'" - "remove_result.state == 'absent'" + +- name: the module must fail when a service is not found + service: + name: 'nonexisting' + state: stopped + register: result + ignore_errors: yes + when: ansible_distribution != 'FreeBSD' + +- assert: + that: + - result is failed + - result is search("Could not find the requested service nonexisting") + when: ansible_distribution != 'FreeBSD' + +- name: the module must fail in check_mode as well when a service is not found + service: + name: 'nonexisting' + state: stopped + register: result + check_mode: yes + ignore_errors: yes + when: ansible_distribution != 'FreeBSD' + +- assert: + that: + - result is failed + - result is search("Could not find the requested service nonexisting") + when: ansible_distribution != 'FreeBSD' diff --git a/test/integration/targets/systemd/defaults/main.yml b/test/integration/targets/systemd/defaults/main.yml new file mode 100644 index 00000000000..33063b86d91 --- /dev/null +++ b/test/integration/targets/systemd/defaults/main.yml @@ -0,0 +1 @@ +fake_service: nonexisting diff --git a/test/integration/targets/systemd/tasks/main.yml b/test/integration/targets/systemd/tasks/main.yml index a3078540ecf..282988b356a 100644 --- a/test/integration/targets/systemd/tasks/main.yml +++ b/test/integration/targets/systemd/tasks/main.yml @@ -47,4 +47,29 @@ - 'not systemd_test0.changed' - 'systemd_test0.state == "started"' + - name: the module must fail when a service is not found + systemd: + name: '{{ fake_service }}' + state: stopped + register: result + ignore_errors: yes + + - assert: + that: + - result is failed + - result is search("Could not find the requested service {{ fake_service }}") + + - name: the module must fail in check_mode as well when a service is not found + systemd: + name: '{{ fake_service }}' + state: stopped + register: result + check_mode: yes + ignore_errors: yes + + - assert: + that: + - result is failed + - result is search("Could not find the requested service {{ fake_service }}") + when: 'systemctl_check.rc == 0'