From 1ab462461279d4f0a0b4e9b2f1ea61ea43ce5523 Mon Sep 17 00:00:00 2001 From: Chih-Hsuan Yen Date: Sat, 25 Apr 2020 07:26:42 +0800 Subject: [PATCH] service_facts: fix for systemd 245 (#68211) * service_facts: fix for systemd 245 Since systemd 245, `systemctl list-unit-files` comes with a new column "VENDOR PRESET" [1] and breaks the service_facts module: $ ansible localhost -m service_facts localhost | FAILED! => { "changed": false, "msg": "Malformed output discovered from systemd list-unit-files: auditd.service disabled disabled " } This patch drops the third column to make it work with old and new systemd. With the new slice operation, IndexError instead of ValueError is raised if the output contains less than 2 columns. Test plan: running `ansible-test integration -v service_facts` on up-to-date Arch Linux [1] https://github.com/systemd/systemd/pull/14445 * add changelog Signed-off-by: Rick Elrod Co-authored-by: Rick Elrod --- .../fragments/68211-systemd-list-unit-files-parsing.yml | 2 ++ lib/ansible/modules/system/service_facts.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/68211-systemd-list-unit-files-parsing.yml diff --git a/changelogs/fragments/68211-systemd-list-unit-files-parsing.yml b/changelogs/fragments/68211-systemd-list-unit-files-parsing.yml new file mode 100644 index 00000000000000..421a28d8c37d3e --- /dev/null +++ b/changelogs/fragments/68211-systemd-list-unit-files-parsing.yml @@ -0,0 +1,2 @@ +bugfixes: + - service_facts - Now correctly parses systemd list-unit-files for systemd >=245 diff --git a/lib/ansible/modules/system/service_facts.py b/lib/ansible/modules/system/service_facts.py index f405abf9462fb5..5f8b4d52d90fff 100644 --- a/lib/ansible/modules/system/service_facts.py +++ b/lib/ansible/modules/system/service_facts.py @@ -212,9 +212,10 @@ def gather_services(self): services[service_name] = {"name": service_name, "state": state_val, "status": "unknown", "source": "systemd"} rc, stdout, stderr = self.module.run_command("%s list-unit-files --no-pager --type service --all" % systemctl_path, use_unsafe_shell=True) for line in [svc_line for svc_line in stdout.split('\n') if '.service' in svc_line and 'not-found' not in svc_line]: + # there is one more column (VENDOR PRESET) from `systemctl list-unit-files` for systemd >= 245 try: - service_name, status_val = line.split() - except ValueError: + service_name, status_val = line.split()[:2] + except IndexError: self.module.fail_json(msg="Malformed output discovered from systemd list-unit-files: {0}".format(line)) if service_name not in services: services[service_name] = {"name": service_name, "state": "unknown", "status": status_val, "source": "systemd"}