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
pull/69845/head
Andrew Klychkov 5 years ago committed by GitHub
parent 6a4455ff54
commit 00ead98595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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).

@ -114,9 +114,6 @@ def fail_if_missing(module, found, service, msg=''):
:kw msg: extra info to append to error/success msg when missing :kw msg: extra info to append to error/success msg when missing
''' '''
if not found: 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))

@ -194,3 +194,32 @@
that: that:
- "remove_result.path == '/usr/sbin/ansible_test_service'" - "remove_result.path == '/usr/sbin/ansible_test_service'"
- "remove_result.state == 'absent'" - "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'

@ -47,4 +47,29 @@
- 'not systemd_test0.changed' - 'not systemd_test0.changed'
- 'systemd_test0.state == "started"' - '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' when: 'systemctl_check.rc == 0'

Loading…
Cancel
Save