Skip to content

Commit

Permalink
service_facts: fix for systemd 245 (#68211)
Browse files Browse the repository at this point in the history
* 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] systemd/systemd#14445

* add changelog

Signed-off-by: Rick Elrod <rick@elrod.me>

Co-authored-by: Rick Elrod <rick@elrod.me>
  • Loading branch information
Chih-Hsuan Yen and relrod committed Apr 24, 2020
1 parent d7da1d9 commit bd4fdb1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
@@ -0,0 +1,2 @@
bugfixes:
- service_facts - Now correctly parses systemd list-unit-files for systemd >=245
5 changes: 3 additions & 2 deletions lib/ansible/modules/system/service_facts.py
Expand Up @@ -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"}
Expand Down

0 comments on commit bd4fdb1

Please sign in to comment.