diff --git a/changelogs/fragments/fix_systemd_facts.yml b/changelogs/fragments/fix_systemd_facts.yml new file mode 100644 index 00000000000..f024143bed3 --- /dev/null +++ b/changelogs/fragments/fix_systemd_facts.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - facts - add systemd full_version and ensure version is integer diff --git a/lib/ansible/module_utils/facts/system/systemd.py b/lib/ansible/module_utils/facts/system/systemd.py index cb6f4c7931d..5f8a0bd26a2 100644 --- a/lib/ansible/module_utils/facts/system/systemd.py +++ b/lib/ansible/module_utils/facts/system/systemd.py @@ -18,6 +18,8 @@ from __future__ import annotations import typing as t +import contextlib + from ansible.module_utils.facts.collector import BaseFactCollector from ansible.module_utils.facts.system.service_mgr import ServiceMgrFactCollector @@ -39,9 +41,16 @@ class SystemdFactCollector(BaseFactCollector): if rc != 0: return systemd_facts - systemd_facts["systemd"] = { - "features": str(stdout.split("\n")[1]), - "version": int(stdout.split(" ")[1]), - } + systemd_version = stdout.split(" ")[1] + + systemd_facts["systemd"] = {} + systemd_facts["systemd"]["features"] = stdout.split("\n")[1] + systemd_facts["systemd"]["full_version"] = str( + stdout.split(" ")[2].split(")")[0][1:], + ) + with contextlib.suppress(ValueError): + systemd_version = int(systemd_version) + + systemd_facts["systemd"]["version"] = systemd_version return systemd_facts diff --git a/test/integration/targets/systemd/tasks/test_systemd_version.yml b/test/integration/targets/systemd/tasks/test_systemd_version.yml index 2039d6fbd19..c38df01e620 100644 --- a/test/integration/targets/systemd/tasks/test_systemd_version.yml +++ b/test/integration/targets/systemd/tasks/test_systemd_version.yml @@ -1,5 +1,5 @@ --- -- name: Show Gathered Facts +- name: Show systemd facts ansible.builtin.debug: msg: "{{ ansible_systemd }}" @@ -9,3 +9,5 @@ - ansible_systemd.version | int > 0 - ansible_systemd.version is match('^[1-9][0-9][0-9]$') - ansible_systemd.features | regex_search('(\\+|-)(PAM|AUDIT)') is not none + - ansible_systemd.full_version is match('^[1-9][0-9][0-9](.|-|~)|^[1-9][0-9][0-9]$') + - (ansible_systemd.full_version | type_debug == 'AnsibleUnsafeText' or ansible_systemd.full_version | type_debug == 'str')