diff --git a/changelogs/fragments/systemd-warn-on-chroot.yaml b/changelogs/fragments/systemd-warn-on-chroot.yaml new file mode 100644 index 00000000000..148db8a5f01 --- /dev/null +++ b/changelogs/fragments/systemd-warn-on-chroot.yaml @@ -0,0 +1,2 @@ +bugfixes: + - systemd - warn when exeuting in a chroot environment rather than failing (https://github.com/ansible/ansible/pull/43904) diff --git a/lib/ansible/modules/system/systemd.py b/lib/ansible/modules/system/systemd.py index 6f1eba0afc1..1cd45df761c 100644 --- a/lib/ansible/modules/system/systemd.py +++ b/lib/ansible/modules/system/systemd.py @@ -254,6 +254,7 @@ status: ''' # NOQA from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.facts.system.chroot import is_chroot from ansible.module_utils.service import sysv_exists, sysv_is_enabled, fail_if_missing from ansible.module_utils._text import to_native @@ -493,6 +494,9 @@ def main(): (rc, out, err) = module.run_command("%s %s '%s'" % (systemctl, action, unit)) if rc != 0: module.fail_json(msg="Unable to %s service %s: %s" % (action, unit, err)) + # check for chroot + elif is_chroot(): + module.warn("Target is a chroot. This can lead to false positives or prevent the init system tools from working.") else: # this should not happen? module.fail_json(msg="Service is in unknown state", status=result['status'])