| 1 |
# Copyright (c) Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
- name: Gather all units from shell |
| 6 |
ansible.builtin.command: systemctl list-units --no-pager --type service,target,socket,mount,timer --all --plain --no-legend |
| 7 |
register: all_units |
| 8 |
|
| 9 |
- name: Assert command run successfully |
| 10 |
ansible.builtin.assert: |
| 11 |
that: |
| 12 |
- all_units.rc == 0 |
| 13 |
|
| 14 |
- name: Gather all units |
| 15 |
community.general.systemd_info: |
| 16 |
register: units_all |
| 17 |
|
| 18 |
- name: Check all units exists |
| 19 |
ansible.builtin.assert: |
| 20 |
that: |
| 21 |
- units_all is defined |
| 22 |
- units_all.units | length == all_units.stdout_lines | length |
| 23 |
success_msg: "Success: All units collected." |
| 24 |
|
| 25 |
- name: Build all units list |
| 26 |
set_fact: |
| 27 |
shell_units: "{{ all_units.stdout_lines | map('split') | list }}" |
| 28 |
|
| 29 |
- name: Check all units properties |
| 30 |
ansible.builtin.assert: |
| 31 |
that: |
| 32 |
- units_all.units[item[0]].name == item[0] |
| 33 |
- units_all.units[item[0]].loadstate == item[1] |
| 34 |
- units_all.units[item[0]].activestate == item[2] |
| 35 |
- units_all.units[item[0]].substate == item[3] |
| 36 |
loop: "{{ shell_units }}" |
| 37 |
loop_control: |
| 38 |
label: "{{ item[0] }}" |
| 39 |
|
| 40 |
- name: Gather systemd-journald.service properties from shell |
| 41 |
ansible.builtin.command: systemctl show systemd-journald.service -p Id,LoadState,ActiveState,SubState,FragmentPath,MainPID,ExecMainPID,UnitFileState,UnitFilePreset,Description,Restart |
| 42 |
register: journald_prop |
| 43 |
|
| 44 |
- name: Assert command run successfully |
| 45 |
ansible.builtin.assert: |
| 46 |
that: |
| 47 |
- journald_prop.rc == 0 |
| 48 |
|
| 49 |
- name: Gather systemd-journald.service |
| 50 |
community.general.systemd_info: |
| 51 |
unitname: |
| 52 |
- systemd-journald.service |
| 53 |
register: journal_unit |
| 54 |
|
| 55 |
- name: Check unit facts and all properties |
| 56 |
ansible.builtin.assert: |
| 57 |
that: |
| 58 |
- journal_unit.units is defined |
| 59 |
- journal_unit.units['systemd-journald.service'] is defined |
| 60 |
- journal_unit.units['systemd-journald.service'].name is defined |
| 61 |
- journal_unit.units['systemd-journald.service'].loadstate is defined |
| 62 |
- journal_unit.units['systemd-journald.service'].activestate is defined |
| 63 |
- journal_unit.units['systemd-journald.service'].substate is defined |
| 64 |
- journal_unit.units['systemd-journald.service'].fragmentpath is defined |
| 65 |
- journal_unit.units['systemd-journald.service'].mainpid is defined |
| 66 |
- journal_unit.units['systemd-journald.service'].execmainpid is defined |
| 67 |
- journal_unit.units['systemd-journald.service'].unitfilestate is defined |
| 68 |
- journal_unit.units['systemd-journald.service'].unitfilepreset is defined |
| 69 |
success_msg: "Success: All properties collected." |
| 70 |
|
| 71 |
- name: Create dict of properties from shell |
| 72 |
ansible.builtin.set_fact: |
| 73 |
journald_shell: "{{ dict(journald_prop.stdout_lines | map('split', '=', 1) | list) }}" |
| 74 |
|
| 75 |
- name: Check properties content |
| 76 |
ansible.builtin.assert: |
| 77 |
that: |
| 78 |
- journal_unit.units['systemd-journald.service'].name == journald_shell.Id |
| 79 |
- journal_unit.units['systemd-journald.service'].loadstate == journald_shell.LoadState |
| 80 |
- journal_unit.units['systemd-journald.service'].activestate == journald_shell.ActiveState |
| 81 |
- journal_unit.units['systemd-journald.service'].substate == journald_shell.SubState |
| 82 |
- journal_unit.units['systemd-journald.service'].fragmentpath == journald_shell.FragmentPath |
| 83 |
- journal_unit.units['systemd-journald.service'].mainpid == journald_shell.MainPID |
| 84 |
- journal_unit.units['systemd-journald.service'].execmainpid == journald_shell.ExecMainPID |
| 85 |
- journal_unit.units['systemd-journald.service'].unitfilestate == journald_shell.UnitFileState |
| 86 |
- journal_unit.units['systemd-journald.service'].unitfilepreset == journald_shell.UnitFilePreset |
| 87 |
success_msg: "Success: Property values are correct." |
| 88 |
|
| 89 |
- name: Gather systemd-journald.service extra properties |
| 90 |
community.general.systemd_info: |
| 91 |
unitname: |
| 92 |
- systemd-journald.service |
| 93 |
extra_properties: |
| 94 |
- Description |
| 95 |
- Restart |
| 96 |
register: journal_extra |
| 97 |
|
| 98 |
- name: Check new properties |
| 99 |
ansible.builtin.assert: |
| 100 |
that: |
| 101 |
- journal_extra.units is defined |
| 102 |
- journal_extra.units['systemd-journald.service'] is defined |
| 103 |
- journal_extra.units['systemd-journald.service'].description is defined |
| 104 |
- journal_extra.units['systemd-journald.service'].restart is defined |
| 105 |
- journal_extra.units['systemd-journald.service'].description == journald_shell.Description |
| 106 |
- journal_extra.units['systemd-journald.service'].restart == journald_shell.Restart |
| 107 |
success_msg: "Success: Extra property values are correct." |
| 108 |
|
| 109 |
- name: Gather info using wildcard pattern for services |
| 110 |
community.general.systemd_info: |
| 111 |
unitname: |
| 112 |
- '*.service' |
| 113 |
extra_properties: |
| 114 |
- Description |
| 115 |
register: result_wildcards |
| 116 |
|
| 117 |
- name: Assert that at least one service unit was returned |
| 118 |
ansible.builtin.assert: |
| 119 |
that: |
| 120 |
- result_wildcards.units | length > 0 |
| 121 |
|
| 122 |
- name: Gather info using multiple wildcard patterns |
| 123 |
community.general.systemd_info: |
| 124 |
unitname: |
| 125 |
- '*.service' |
| 126 |
- 'ssh*' |
| 127 |
register: result_multi |
| 128 |
|
| 129 |
- name: Debug multi-wildcard results |
| 130 |
ansible.builtin.debug: |
| 131 |
var: result_multi.units |
| 132 |
|
| 133 |
- name: Assert deduplication of units |
| 134 |
ansible.builtin.assert: |
| 135 |
that: |
| 136 |
- unique_keys | length == all_keys | length |
| 137 |
vars: |
| 138 |
all_keys: "{{ result_multi.units | dict2items | map(attribute='key') | list }}" |
| 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." |