diff --git a/lib/ansible/modules/system/systemd.py b/lib/ansible/modules/system/systemd.py index c82bed60d1..c970bd3ff7 100644 --- a/lib/ansible/modules/system/systemd.py +++ b/lib/ansible/modules/system/systemd.py @@ -297,6 +297,7 @@ def main(): # check service data, cannot error out on rc as it changes across versions, assume not found (rc, out, err) = module.run_command("%s show '%s'" % (systemctl, unit)) + if rc == 0: # load return of systemctl show into dictionary for easy access and return multival = [] @@ -327,6 +328,18 @@ def main(): if is_systemd and 'LoadError' in result['status']: module.fail_json(msg="Error loading unit file '%s': %s" % (unit, result['status']['LoadError'])) + elif out.find('ignoring request') != -1: + # fallback list-unit-files as show does not work on some systems (chroot) + # not used as primary as it skips some services (like those using init.d) and requires .service notation + if unit.endswith('.service'): + service = unit + else: + service = '%s.service' % unit + (rc, out, err) = module.run_command("%s list-unit-files '%s'" % (systemctl, service)) + if rc == 0: + is_systemd = True + + # Does service exist? found = is_systemd or is_initd if is_initd and not is_systemd: