Skip to content

Commit 4a2cc71

Browse files
authored
systemd_info - extend support to timer unit (#9891)
* systemd_info - extend support to timer unit * systemd_info - add changelogs fragments * systemd_info - fix description and base_props
1 parent e5eac9f commit 4a2cc71

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- systemd_info - extend support to timer units (https://github.com/ansible-collections/community.general/pull/9891).

plugins/modules/systemd_info.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
module: systemd_info
1414
short_description: Gather C(systemd) unit info
1515
description:
16-
- This module gathers info about systemd units (services, targets, sockets, mount).
16+
- This module gathers info about systemd units (services, targets, sockets, mounts, timers).
17+
- Timer units are supported since community.general 10.5.0.
1718
- It runs C(systemctl list-units) (or processes selected units) and collects properties
1819
for each unit using C(systemctl show).
20+
- In case a unit has multiple properties with the same name, only the value of the first one will be collected.
1921
- Even if a unit has a RV(units.loadstate) of V(not-found) or V(masked), it is returned,
2022
but only with the minimal properties (RV(units.name), RV(units.loadstate), RV(units.activestate), RV(units.substate)).
2123
- When O(unitname) and O(extra_properties) are used, the module first checks if the unit exists,
@@ -27,7 +29,8 @@
2729
unitname:
2830
description:
2931
- List of unit names to process.
30-
- It supports C(.service), C(.target), C(.socket), and C(.mount) units type.
32+
- It supports C(.service), C(.target), C(.socket), C(.mount) and C(.timer) units type.
33+
- C(.timer) units are supported since community.general 10.5.0.
3134
- Each name must correspond to the full name of the C(systemd) unit or to a wildcard expression like V('ssh*') and V('*.service').
3235
- Wildcard expressions in O(unitname) are supported since community.general 10.5.0.
3336
type: list
@@ -49,7 +52,7 @@
4952

5053
EXAMPLES = r'''
5154
---
52-
# Gather info for all systemd services, targets, sockets and mount
55+
# Gather info for all systemd services, targets, sockets, mount and timer
5356
- name: Gather all systemd unit info
5457
community.general.systemd_info:
5558
register: results
@@ -72,6 +75,15 @@
7275
unitname:
7376
- 'systemd-*'
7477
register: results
78+
79+
# Gather info for systemd-tmpfiles-clean.timer with extra properties
80+
- name: Gather info of systemd-tmpfiles-clean.timer and extra AccuracyUSec
81+
community.general.systemd_info:
82+
unitname:
83+
- systemd-tmpfiles-clean.timer
84+
extra_properties:
85+
- AccuracyUSec
86+
register: results
7587
'''
7688

7789
RETURN = r'''
@@ -255,6 +267,8 @@ def determine_category(unit):
255267
return 'socket'
256268
elif unit.endswith('.mount'):
257269
return 'mount'
270+
elif unit.endswith('.timer'):
271+
return 'timer'
258272
else:
259273
return None
260274

@@ -275,7 +289,8 @@ def get_category_base_props(category):
275289
'service': ['FragmentPath', 'UnitFileState', 'UnitFilePreset', 'MainPID', 'ExecMainPID'],
276290
'target': ['FragmentPath', 'UnitFileState', 'UnitFilePreset'],
277291
'socket': ['FragmentPath', 'UnitFileState', 'UnitFilePreset'],
278-
'mount': ['Where', 'What', 'Options', 'Type']
292+
'mount': ['Where', 'What', 'Options', 'Type'],
293+
'timer': ['FragmentPath', 'UnitFileState', 'UnitFilePreset'],
279294
}
280295
return base_props.get(category, [])
281296

@@ -364,7 +379,7 @@ def main():
364379
state_props = ['LoadState', 'ActiveState', 'SubState']
365380
results = {}
366381

367-
unit_types = ["service", "target", "socket", "mount"]
382+
unit_types = ["service", "target", "socket", "mount", "timer"]
368383

369384
list_output = list_units(base_runner, unit_types)
370385
units_info = {}

tests/integration/targets/systemd_info/tasks/tests.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: GPL-3.0-or-later
44

55
- name: Gather all units from shell
6-
ansible.builtin.command: systemctl list-units --no-pager --type service,target,socket,mount --all --plain --no-legend
6+
ansible.builtin.command: systemctl list-units --no-pager --type service,target,socket,mount,timer --all --plain --no-legend
77
register: all_units
88

99
- name: Assert command run successfully
@@ -136,4 +136,28 @@
136136
- unique_keys | length == all_keys | length
137137
vars:
138138
all_keys: "{{ result_multi.units | dict2items | map(attribute='key') | list }}"
139-
unique_keys: "{{ all_keys | unique }}"
139+
unique_keys: "{{ all_keys | unique }}"
140+
141+
- name: Gather info of systemd-tmpfiles-clean.timer and extra AccuracyUSec
142+
community.general.systemd_info:
143+
unitname:
144+
- systemd-tmpfiles-clean.timer
145+
extra_properties:
146+
- AccuracyUSec
147+
register: result_timer
148+
149+
- name: Check timer unit properties
150+
ansible.builtin.assert:
151+
that:
152+
- result_timer.units is defined
153+
- result_timer.units['systemd-tmpfiles-clean.timer'] is defined
154+
- result_timer.units['systemd-tmpfiles-clean.timer'].name is defined
155+
- result_timer.units['systemd-tmpfiles-clean.timer'].loadstate is defined
156+
- result_timer.units['systemd-tmpfiles-clean.timer'].activestate is defined
157+
- result_timer.units['systemd-tmpfiles-clean.timer'].substate is defined
158+
- result_timer.units['systemd-tmpfiles-clean.timer'].fragmentpath is defined
159+
- result_timer.units['systemd-tmpfiles-clean.timer'].unitfilestate is defined
160+
- result_timer.units['systemd-tmpfiles-clean.timer'].unitfilepreset is defined
161+
- result_timer.units['systemd-tmpfiles-clean.timer'].accuracyusec is defined
162+
- result_timer.units['systemd-tmpfiles-clean.timer'].accuracyusec | length > 0
163+
success_msg: "Success: All properties collected."

0 commit comments

Comments
 (0)