Skip to content

Commit 7f93f61

Browse files
authored
add systemd version and features fact (#83083)
Signed-off-by: Thomas Sjögren <konstruktoid@users.noreply.github.com>
1 parent 5dac5d3 commit 7f93f61

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- facts - add systemd version and features

lib/ansible/module_utils/facts/default_collectors.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from ansible.module_utils.facts.system.selinux import SelinuxFactCollector
5454
from ansible.module_utils.facts.system.service_mgr import ServiceMgrFactCollector
5555
from ansible.module_utils.facts.system.ssh_pub_keys import SshPubKeyFactCollector
56+
from ansible.module_utils.facts.system.systemd import SystemdFactCollector
5657
from ansible.module_utils.facts.system.user import UserFactCollector
5758

5859
from ansible.module_utils.facts.hardware.base import HardwareCollector
@@ -118,7 +119,8 @@
118119
EnvFactCollector,
119120
LoadAvgFactCollector,
120121
SshPubKeyFactCollector,
121-
UserFactCollector
122+
UserFactCollector,
123+
SystemdFactCollector
122124
] # type: t.List[t.Type[BaseFactCollector]]
123125

124126
# virtual, this might also limit hardware/networking
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Get systemd version and features
2+
#
3+
# This file is part of Ansible
4+
#
5+
# Ansible is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Ansible is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17+
18+
from __future__ import annotations
19+
20+
import ansible.module_utils.compat.typing as t
21+
22+
from ansible.module_utils.facts.collector import BaseFactCollector
23+
from ansible.module_utils.facts.system.service_mgr import ServiceMgrFactCollector
24+
25+
26+
class SystemdFactCollector(BaseFactCollector):
27+
name = "systemd"
28+
_fact_ids = set() # type: t.Set[str]
29+
30+
def collect(self, module=None, collected_facts=None):
31+
systemctl_bin = module.get_bin_path("systemctl")
32+
if systemctl_bin and ServiceMgrFactCollector.is_systemd_managed(module):
33+
rc, stdout, stderr = module.run_command(
34+
[systemctl_bin, "--version"],
35+
check_rc=False,
36+
)
37+
38+
systemd_facts = {}
39+
40+
if rc != 0:
41+
return systemd_facts
42+
43+
systemd_facts["systemd"] = {}
44+
systemd_facts["systemd"]["features"] = str(stdout.split("\n")[1])
45+
systemd_facts["systemd"]["version"] = int(stdout.split(" ")[1])
46+
47+
return systemd_facts

lib/ansible/modules/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
V(processor_count), V(python), V(python_version), V(real_user_id), V(selinux), V(service_mgr),
2626
V(ssh_host_key_dsa_public), V(ssh_host_key_ecdsa_public), V(ssh_host_key_ed25519_public),
2727
V(ssh_host_key_rsa_public), V(ssh_host_pub_keys), V(ssh_pub_keys), V(system), V(system_capabilities),
28-
V(system_capabilities_enforced), V(user), V(user_dir), V(user_gecos), V(user_gid), V(user_id),
28+
V(system_capabilities_enforced), V(systemd), V(user), V(user_dir), V(user_gecos), V(user_gid), V(user_id),
2929
V(user_shell), V(user_uid), V(virtual), V(virtualization_role), V(virtualization_type).
3030
Can specify a list of values to specify a larger subset.
3131
Values can also be used with an initial C(!) to specify that

test/integration/targets/systemd/tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,5 @@
121121
- import_tasks: test_unit_template.yml
122122
- import_tasks: test_indirect_service.yml
123123
- import_tasks: test_enabled_runtime.yml
124+
- import_tasks: test_systemd_version.yml
124125
- import_tasks: test_mask.yml
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
- name: Show Gathered Facts
3+
ansible.builtin.debug:
4+
msg: "{{ ansible_systemd }}"
5+
6+
- name: Assert the systemd version fact
7+
ansible.builtin.assert:
8+
that:
9+
- ansible_systemd.version | int
10+
- ansible_systemd.version is match('^[1-9][0-9][0-9]$')
11+
- ansible_systemd.features | regex_search('(\\+|-)(PAM|AUDIT)')

0 commit comments

Comments
 (0)