Files
Kamino/roles/systemd_hardening/tasks/main.yml
T
Dragonish 6c2d124b3f
Build / build (pull_request) Successful in 20s
Molecule / molecule (pull_request) Canceled after 0s
Lint / lint (pull_request) Canceled after 38s
fix(systemd_hardening): look up the listed units instead of listing every unit file
The role listed every service unit file to answer one question: is each
unit in its disable and mask lists installed? The listing keeps PID 1 busy
for as long as the host has unit files. Ask about the listed units only,
with one `systemctl show --property=LoadState`; a unit with no unit file
reports `not-found` and is skipped as before.

An instance name such as [email protected] is now matched when its template
exists. A template name such as [email protected] makes the lookup fail, where
the listing matched it.

Closes #283

Assisted-by: Claude Code
2026-11-22 19:41:23 +00:00

76 lines
3.1 KiB
YAML

---
# Required input with no default: the role does not read a global such as system_type;
# the inventory maps it onto systemd_hardening_system_type.
- name: Require systemd_hardening_system_type
ansible.builtin.assert:
that:
- systemd_hardening_system_type is defined
fail_msg: >-
systemd_hardening_system_type is required and has no default. Set it in
the play or the inventory.
tags: always
- name: Validate systemd_hardening_system_type variable
ansible.builtin.assert:
that:
- systemd_hardening_system_type in systemd_hardening_profiles
fail_msg: >-
systemd_hardening_system_type '{{ systemd_hardening_system_type }}' is invalid.
Must be one of: {{ systemd_hardening_profiles.keys() | list | join(', ') }}.
success_msg: "systemd_hardening_system_type '{{ systemd_hardening_system_type }}' is valid."
tags: always
# Ask systemd about the listed units only: listing every unit file keeps PID 1 busy for as long as
# the host has unit files, which on slow storage can outlast a short hardware watchdog. `systemctl show`
# prints one block per argument in the order given, `LoadState=not-found` for a unit with no unit
# file, and keeps the order for an alias. Not service_facts: it sets a fact named `services`, which
# overrides an inventory variable of that name.
- name: Find which of the listed service units are installed
tags: [disable, mask]
vars:
__systemd_hardening_probe_units: >-
{{ (systemd_hardening_disable_services_effective + systemd_hardening_mask_services)
| map('regex_replace', '[.]service$', '') | unique | map('regex_replace', '$', '.service') | list }}
block:
- name: Look up the listed service units # noqa: command-instead-of-module
become: true
ansible.builtin.command:
argv: "{{ ['systemctl', 'show', '--property=LoadState', '--'] + __systemd_hardening_probe_units }}"
register: __systemd_hardening_probe
changed_when: false
check_mode: false
when: __systemd_hardening_probe_units | length > 0
- name: Set installed service units
ansible.builtin.set_fact:
systemd_hardening_installed_units: >-
{{ __systemd_hardening_probe_units | zip(__systemd_hardening_probe_states)
| rejectattr('1', 'eq', 'LoadState=not-found') | map(attribute='0') | list }}
vars:
__systemd_hardening_probe_states: "{{ (__systemd_hardening_probe.stdout | default('')).strip().split('\n\n') }}"
when: __systemd_hardening_probe_units | length > 0
- name: Set no installed service units
ansible.builtin.set_fact:
systemd_hardening_installed_units: []
when: __systemd_hardening_probe_units | length == 0
- name: Disable unwanted services
ansible.builtin.include_tasks: disable_services.yml
tags: disable
- name: Mask high-risk services
ansible.builtin.include_tasks: mask_services.yml
tags: mask
- name: Harden systemd manager globally
ansible.builtin.include_tasks: harden_manager.yml
tags: harden-manager
- name: Apply per-service drop-ins
ansible.builtin.include_tasks: dropins.yml
tags: dropins
- name: Flush handlers early
ansible.builtin.meta: flush_handlers