diff --git a/changelogs/fragments/71528-systemd-improve-unit-searching.yml b/changelogs/fragments/71528-systemd-improve-unit-searching.yml new file mode 100644 index 00000000000..92e5228ff8e --- /dev/null +++ b/changelogs/fragments/71528-systemd-improve-unit-searching.yml @@ -0,0 +1,4 @@ +bugfixes: + - > + systemd - account for templated unit files using ``@`` when searching for + the unit file (https://github.com/ansible/ansible/pull/72347#issuecomment-730626228) diff --git a/lib/ansible/modules/systemd.py b/lib/ansible/modules/systemd.py index 1c75be55547..c9c226464ab 100644 --- a/lib/ansible/modules/systemd.py +++ b/lib/ansible/modules/systemd.py @@ -415,7 +415,9 @@ def main(): elif err and rc == 1 and 'Failed to parse bus message' in err: result['status'] = parse_systemctl_show(to_native(out).split('\n')) - (rc, out, err) = module.run_command("{systemctl} list-unit-files '{unit}*'".format(systemctl=systemctl, unit=unit)) + unit, sep, suffix = unit.partition('@') + unit_search = '{unit}{sep}*'.format(unit=unit, sep=sep) + (rc, out, err) = module.run_command("{systemctl} list-unit-files '{unit_search}'".format(systemctl=systemctl, unit_search=unit_search)) is_systemd = unit in out (rc, out, err) = module.run_command("{systemctl} is-active '{unit}'".format(systemctl=systemctl, unit=unit))