Skip to content

Commit

Permalink
systemd: should fail in check_mode when service not found on host (an…
Browse files Browse the repository at this point in the history
…sible#68136)

* systemd: should fail in check_mode when service not found on host
  • Loading branch information
Andersson007 authored Jun 2, 2020
1 parent 6a4455f commit 00ead98
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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).
5 changes: 1 addition & 4 deletions lib/ansible/module_utils/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
29 changes: 29 additions & 0 deletions test/integration/targets/service/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions test/integration/targets/systemd/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fake_service: nonexisting
25 changes: 25 additions & 0 deletions test/integration/targets/systemd/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 00ead98

Please sign in to comment.