[2.9] service_facts: fix for systemd 245

* 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:

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: Chih-Hsuan Yen <yan12125@gmail.com>
Signed-off-by: Rick Elrod <rick@elrod.me>
Co-authored-by: Rick Elrod <rick@elrod.me>
(cherry picked from commit bd4fdb1ca2)
pull/69271/head
Chih-Hsuan Yen 5 years ago committed by Matt Clay
parent 28cc8e6155
commit fbf93db4a8

@ -0,0 +1,2 @@
bugfixes:
- service_facts - Now correctly parses systemd list-unit-files for systemd >=245

@ -212,9 +212,10 @@ class SystemctlScanService(BaseService):
services[service_name] = {"name": service_name, "state": state_val, "status": "unknown", "source": "systemd"} 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) 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]: 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: try:
service_name, status_val = line.split() service_name, status_val = line.split()[:2]
except ValueError: except IndexError:
self.module.fail_json(msg="Malformed output discovered from systemd list-unit-files: {0}".format(line)) self.module.fail_json(msg="Malformed output discovered from systemd list-unit-files: {0}".format(line))
if service_name not in services: if service_name not in services:
services[service_name] = {"name": service_name, "state": "unknown", "status": status_val, "source": "systemd"} services[service_name] = {"name": service_name, "state": "unknown", "status": status_val, "source": "systemd"}

Loading…
Cancel
Save