Files
Kamino/roles/systemd_hardening/tasks/main.yml
T
Dragonish f256c1c7e5
Build / build (pull_request) Successful in 18s
Lint / lint (pull_request) Successful in 1m11s
Molecule / molecule (pull_request) Successful in 1m2s
fix(systemd_hardening): disable and mask the socket of a listed service too
Look up every listed name as a socket and as a service and act on each
installed unit, the socket first. A service whose socket stays active is
started again by the next connection to its port.

Assisted-by: Claude Code
2026-11-23 11:18:49 +00:00

80 lines
3.4 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.
# Every listed name is looked up as a socket and as a service, socket first: stopping a service
# whose socket is still listening does not close the port, systemd starts the service again on the
# next connection. A name may be given with or without .service or .socket.
- name: Find which of the listed units are installed
tags: [disable, mask]
vars:
__systemd_hardening_probe_units: >-
{{ (systemd_hardening_disable_services_effective + systemd_hardening_mask_services)
| map('regex_replace', '[.](service|socket)$', '') | unique
| product(['socket', 'service']) | map('join', '.') | list }}
block:
- name: Look up the listed 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 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 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