feat(roles): add the systemd_hardening and network_interfaces roles
Copied from the environment repository's roles and scrubbed for the Collection: galaxy namespace mortis, molecule scenarios call the roles by their FQCN and take their paths from the workflow, README examples use mortis.kamino.* and documentation addresses, repository-local references (issue numbers, ADRs, inventory paths) are dropped. network_interfaces loses its Legacy name guard and the molecule play that tested it: the guard only protected the in-repo rename, and a new consumer never had the old names. mock_roles in .ansible-lint lists both FQCNs. Not verified locally: the molecule scenarios (they run on the kamino-molecule runner in CI) and ansible-test sanity. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
21dda27b00
commit
cf8f4b5bf0
32 files changed
+1044
No files matched your search
@@ -0,0 +1,56 @@
|
||||
# systemd_hardening
|
||||
|
||||
Disables and masks risky services, writes a systemd manager hardening drop-in and renders opt-in per-service sandboxing drop-ins. Variables and their types are defined in [`meta/argument_specs.yml`](meta/argument_specs.yml), which is the source of truth.
|
||||
|
||||
## Supported platforms
|
||||
|
||||
Hosts with systemd and `systemctl list-unit-files`: Ubuntu 24.04 (noble) LXCs, Ubuntu 22.04 (jammy) VMs and Debian 13 (trixie) Proxmox hosts. The role is applied to the three system types in `systemd_hardening_profiles`: `pve` (Proxmox hosts), `ubuntu_lxc` and `ubuntu_vm`. The molecule scenario (`molecule/default/`, CI only) runs the `ubuntu_lxc` profile on Ubuntu 24.04 against stub unit files, because the test image installs none of the services the role disables or masks. It checks the disable and mask results, the manager and service drop-ins (loaded by systemd after the reload) and the refusals; it cannot show that a real `rpcbind` or similar stops. The `pve` and `ubuntu_vm` profiles and Ubuntu 22.04 and Debian 13 are not verified by a test.
|
||||
|
||||
## Requirements and run order
|
||||
|
||||
- `systemd_hardening_system_type` is required and has no default (the role does not read a global such as `system_type`; map it in the inventory). The role fails if it is undefined or not a key of `systemd_hardening_profiles`.
|
||||
- Use it by its fully qualified name, `mortis.kamino.systemd_hardening`.
|
||||
- Run it after your baseline role and before the roles that manage firewalls and jails.
|
||||
- Needs privilege escalation: tasks use `become`.
|
||||
|
||||
## Role variables
|
||||
|
||||
Two variables below, `systemd_hardening_disable_services_extra` and `systemd_hardening_disable_services_effective`, are deliberately not in the spec: their defaults index the profile by `systemd_hardening_system_type`, and the spec is templated before the role's assert runs, so an invalid `systemd_hardening_system_type` would otherwise fail with a templating error instead of the role's own message.
|
||||
|
||||
| Variable | Default | Required | Description |
|
||||
|---|---|---|---|
|
||||
| `systemd_hardening_system_type` | none | yes | Profile key. Every play that includes the role sets it. |
|
||||
| `systemd_hardening_profiles` | `pve`, `ubuntu_lxc`, `ubuntu_vm` profiles | no | Per type `disable_services_extra`. Overriding replaces all profiles. |
|
||||
| `systemd_hardening_disable_services` | `rpcbind`, `avahi-daemon`, `cups`, `bluetooth`, `nfs-server` | no | Disabled on every type. |
|
||||
| `systemd_hardening_disable_services_extra` | profile entry for `systemd_hardening_system_type` | no | Added to the list above. |
|
||||
| `systemd_hardening_disable_services_effective` | base plus extra | no | Result, do not set. |
|
||||
| `systemd_hardening_mask_services` | `telnet`, `rsh`, `rlogin`, `xinetd` | no | Masked. |
|
||||
| `systemd_hardening_services` | `{}` | no | Unit to directive map for drop-ins. |
|
||||
|
||||
## Tags
|
||||
|
||||
Only `always` (the `systemd_hardening_system_type` assert) works as intended. `disable`, `mask`, `harden-manager` and `dropins` sit on the `include_tasks` lines in `tasks/main.yml`, and tags on a dynamic include select only the include itself: the included tasks are untagged, so they are skipped. `--tags disable` or `--tags mask` therefore runs only the assert and the unit listing, and `--tags harden-manager` or `--tags dropins` runs only the assert. Do not use these tags for selective runs; run the role untagged.
|
||||
|
||||
## Example play
|
||||
|
||||
```yaml
|
||||
- name: Harden LXC services
|
||||
hosts: lxc_guests
|
||||
vars:
|
||||
systemd_hardening_system_type: ubuntu_lxc
|
||||
roles:
|
||||
- role: mortis.kamino.systemd_hardening
|
||||
vars:
|
||||
systemd_hardening_services:
|
||||
myservice:
|
||||
ProtectSystem: strict
|
||||
NoNewPrivileges: true
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- Only units that appear in `systemctl list-unit-files --type=service` are disabled or masked, with or without the `.service` suffix.
|
||||
- The manager drop-in (`DefaultLimitNOFILE`, `DefaultLimitNPROC`, `DefaultLimitCORE`, and task, memory and CPU accounting) is written verbatim by `tasks/harden_manager.yml` and has no variable.
|
||||
- Drop-in and manager changes notify `systemd reload`, which runs `daemon-reload`. Handlers are flushed at the end of the role.
|
||||
- Policy lives in defaults, not `vars/`, so group_vars and host_vars can override it.
|
||||
- `no_log` is not used: the role handles no secrets.
|
||||
@@ -0,0 +1,53 @@
|
||||
---
|
||||
# Per-systemd_hardening_system_type policy. Overridable from group_vars/host_vars: set
|
||||
# systemd_hardening_disable_services_extra (replaces the list for every type in that scope), or
|
||||
# override systemd_hardening_profiles (replaces ALL profiles, so copy the ones you
|
||||
# keep). Not in vars/ or include_vars: both outrank group_vars and host_vars.
|
||||
systemd_hardening_profiles:
|
||||
pve:
|
||||
disable_services_extra: []
|
||||
ubuntu_lxc:
|
||||
# Not present or not meaningful in an LXC
|
||||
disable_services_extra:
|
||||
- systemd-journal-remote
|
||||
ubuntu_vm:
|
||||
disable_services_extra:
|
||||
- multipathd
|
||||
|
||||
# Default services to disable globally
|
||||
systemd_hardening_disable_services:
|
||||
- rpcbind
|
||||
- avahi-daemon
|
||||
- cups
|
||||
- bluetooth
|
||||
- nfs-server
|
||||
|
||||
# Added to the list above for this systemd_hardening_system_type. Lazy (read at use), no set_fact,
|
||||
# so a group_vars override of the flat name wins.
|
||||
systemd_hardening_disable_services_extra: "{{ systemd_hardening_profiles[systemd_hardening_system_type].disable_services_extra }}"
|
||||
|
||||
# What the role actually disables: base + extra. Read-only result, do not set.
|
||||
systemd_hardening_disable_services_effective: "{{ systemd_hardening_disable_services + systemd_hardening_disable_services_extra }}"
|
||||
|
||||
# High-risk services to mask completely (security policy)
|
||||
systemd_hardening_mask_services:
|
||||
- telnet
|
||||
- rsh
|
||||
- rlogin
|
||||
- xinetd
|
||||
|
||||
# Per-service hardening overrides (opt-in)
|
||||
# Map of <unit name without .service> -> {Directive: value}. Values render as:
|
||||
# booleans -> yes/no, lists -> space-joined, everything else as-is.
|
||||
# Example:
|
||||
# systemd_hardening_services:
|
||||
# myservice:
|
||||
# ProtectSystem: "strict"
|
||||
# NoNewPrivileges: true
|
||||
#
|
||||
# Do NOT add ssh/sshd here. Sandboxing directives on sshd.service
|
||||
# (ProtectSystem, PrivateTmp, NoNewPrivileges, ...) are inherited by every
|
||||
# SSH login session and everything spawned from it, including sudo. That
|
||||
# makes admin and Ansible sessions see a read-only filesystem / private /tmp
|
||||
# and can break privilege escalation. Harden sshd via the sshd role instead.
|
||||
systemd_hardening_services: {}
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
- name: Reload systemd daemon
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
listen: "systemd reload"
|
||||
@@ -0,0 +1,53 @@
|
||||
---
|
||||
argument_specs:
|
||||
main:
|
||||
short_description: Harden systemd for a system type
|
||||
description:
|
||||
- >-
|
||||
Validates C(systemd_hardening_system_type) against C(systemd_hardening_profiles), then disables the unwanted services and masks the high-risk
|
||||
services that have an installed unit file, writes C(/etc/systemd/system.conf.d/hardening.conf) and renders a drop-in for every
|
||||
unit in C(systemd_hardening_services).
|
||||
- Units that are not installed are skipped. Handlers are flushed at the end of the role.
|
||||
- >-
|
||||
The role also reads C(systemd_hardening_disable_services_extra) (default is the profile entry for C(systemd_hardening_system_type)) and the
|
||||
derived C(systemd_hardening_disable_services_effective) (the base list plus the extra list, a result, do not set it). They are
|
||||
deliberately not listed as options because their defaults index C(systemd_hardening_profiles[systemd_hardening_system_type]) and would fail
|
||||
argument validation on an invalid C(systemd_hardening_system_type), ahead of the role's own assert. Overriding the extra list replaces it for
|
||||
every system type in that scope.
|
||||
options:
|
||||
systemd_hardening_system_type:
|
||||
type: str
|
||||
required: true
|
||||
description:
|
||||
- >-
|
||||
Selects the entry in C(systemd_hardening_profiles). The role default of C(systemd_hardening_disable_services_extra) reads it, and
|
||||
tasks/main.yml asserts that it is a key of C(systemd_hardening_profiles) (C(pve), C(ubuntu_lxc) or C(ubuntu_vm) by default).
|
||||
- Not restricted by C(choices) so that the assert keeps its own message.
|
||||
systemd_hardening_profiles:
|
||||
type: dict
|
||||
description:
|
||||
- >-
|
||||
Per system type policy. Each profile holds C(disable_services_extra), a list of unit names. The role default has C(pve) (empty),
|
||||
C(ubuntu_lxc) (C(systemd-journal-remote)) and C(ubuntu_vm) (C(multipathd)).
|
||||
- Overriding it replaces all profiles, so copy the ones you keep.
|
||||
systemd_hardening_disable_services:
|
||||
type: list
|
||||
elements: str
|
||||
default: [rpcbind, avahi-daemon, cups, bluetooth, nfs-server]
|
||||
description:
|
||||
- Services stopped and disabled on every system type, with or without the C(.service) suffix. Only installed units are touched.
|
||||
systemd_hardening_mask_services:
|
||||
type: list
|
||||
elements: str
|
||||
default: [telnet, rsh, rlogin, xinetd]
|
||||
description:
|
||||
- Services masked and stopped. Only installed units are touched.
|
||||
systemd_hardening_services:
|
||||
type: dict
|
||||
default: {}
|
||||
description:
|
||||
- >-
|
||||
Opt-in drop-ins, a mapping of unit name without C(.service) to a mapping of directive to value. Written to
|
||||
C(/etc/systemd/system/<unit>.service.d/hardening.conf). Booleans render as yes or no, lists are space-joined, anything else as is.
|
||||
- Each value must itself be a mapping, otherwise the role fails before writing anything.
|
||||
- Do not add C(ssh) or C(sshd). Sandboxing directives on sshd.service are inherited by every login session, including sudo.
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
galaxy_info:
|
||||
role_name: systemd_hardening
|
||||
namespace: mortis
|
||||
author: Defame1297
|
||||
license: GPL-3.0-or-later
|
||||
description: Disable and mask risky services per system type, harden the systemd manager and apply opt-in per-service sandboxing drop-ins.
|
||||
min_ansible_version: "2.19"
|
||||
platforms:
|
||||
- name: Ubuntu
|
||||
versions:
|
||||
- jammy
|
||||
- noble
|
||||
- name: Debian
|
||||
versions:
|
||||
- trixie
|
||||
galaxy_tags:
|
||||
- systemd
|
||||
- hardening
|
||||
- security
|
||||
dependencies: []
|
||||
@@ -0,0 +1,102 @@
|
||||
---
|
||||
# The production LXC profile with one opt-in drop-in. The stub units come from prepare.yml.
|
||||
- name: Converge the role with a valid profile
|
||||
hosts: all
|
||||
vars:
|
||||
systemd_hardening_system_type: ubuntu_lxc
|
||||
systemd_hardening_services:
|
||||
testsvc:
|
||||
ProtectSystem: strict
|
||||
NoNewPrivileges: true
|
||||
ReadWritePaths: [/var/tmp, /run]
|
||||
LimitNOFILE: 1024
|
||||
roles:
|
||||
- role: mortis.kamino.systemd_hardening
|
||||
|
||||
# The plays below are refusals. The missing and unknown system types stop before the role
|
||||
# changes anything. The malformed drop-in plays fail at dropins.yml, after the disable, mask
|
||||
# and manager drop-in tasks ran, but those are no-ops after the converge above, so the
|
||||
# idempotence run stays clean. A sentinel in the failure message tells "the role refused"
|
||||
# from "the fail task after the role ran", which a bare rescue cannot.
|
||||
- name: A run without systemd_hardening_system_type is refused
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Apply the role without a system type
|
||||
block:
|
||||
- name: Include the role
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.systemd_hardening
|
||||
- name: Fail when the missing type was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: MOLECULE-ACCEPTED
|
||||
rescue:
|
||||
- name: The role refused the run
|
||||
ansible.builtin.assert:
|
||||
that: "'missing required arguments' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: An unknown system type is refused
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
systemd_hardening_system_type: not_a_type
|
||||
tasks:
|
||||
- name: Apply the role with an unknown type
|
||||
block:
|
||||
- name: Include the role
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.systemd_hardening
|
||||
- name: Fail when the unknown type was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: MOLECULE-ACCEPTED
|
||||
rescue:
|
||||
- name: The profile check stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'is invalid' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: A drop-in entry that is not a mapping is refused
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
systemd_hardening_system_type: ubuntu_lxc
|
||||
systemd_hardening_services:
|
||||
testsvc: not-a-mapping
|
||||
tasks:
|
||||
- name: Apply the role with a malformed drop-in
|
||||
block:
|
||||
- name: Include the role
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.systemd_hardening
|
||||
- name: Fail when the malformed drop-in was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: MOLECULE-ACCEPTED
|
||||
rescue:
|
||||
- name: The drop-in validation stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'must be a mapping' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: A drop-in list that is not a mapping is refused
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
systemd_hardening_system_type: ubuntu_lxc
|
||||
systemd_hardening_services: [testsvc]
|
||||
tasks:
|
||||
- name: Apply the role with a list instead of a mapping
|
||||
block:
|
||||
- name: Include the role
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.systemd_hardening
|
||||
- name: Fail when the list was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: MOLECULE-ACCEPTED
|
||||
rescue:
|
||||
- name: The role refused the run
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'MOLECULE-ACCEPTED' not in ansible_failed_result.msg | default('')"
|
||||
- "'systemd_hardening_services' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). No privileged mode: systemd needs a
|
||||
# writable cgroup and the firewall tools need NET_ADMIN, nothing else.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: systemd-hardening-instance
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
capabilities:
|
||||
- NET_ADMIN
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
verifier:
|
||||
name: ansible
|
||||
@@ -0,0 +1,31 @@
|
||||
---
|
||||
# The image installs none of the services the role touches, so the units it acts on are
|
||||
# stubs in /usr/lib/systemd/system (not /etc: `systemctl mask` refuses to replace a regular
|
||||
# file there). Nothing is started. rpcbind is enabled so the role has something to
|
||||
# disable; telnet only needs to exist; testsvc receives the drop-in.
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Install the stub unit files
|
||||
ansible.builtin.copy:
|
||||
dest: "/usr/lib/systemd/system/{{ item }}.service"
|
||||
content: |
|
||||
[Unit]
|
||||
Description=molecule stub for {{ item }}
|
||||
|
||||
[Service]
|
||||
ExecStart=/bin/sleep 3600
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
loop: [rpcbind, telnet, testsvc]
|
||||
|
||||
- name: Enable rpcbind and reload systemd
|
||||
ansible.builtin.systemd:
|
||||
name: rpcbind
|
||||
enabled: true
|
||||
daemon_reload: true
|
||||
@@ -0,0 +1,65 @@
|
||||
---
|
||||
# What the role is for: the installed risky units are disabled or masked, the manager
|
||||
# drop-in is in place, and the opt-in drop-in renders and is loaded by systemd. The
|
||||
# directives restate the converge vars, not the role's whole policy.
|
||||
- name: Verify
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Ask systemd about the stub units
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ item }}.service"
|
||||
loop: [rpcbind, telnet]
|
||||
register: systemd_hardening_verify_units
|
||||
check_mode: true
|
||||
|
||||
- name: The disabled unit is disabled and the masked unit is masked
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- systemd_hardening_verify_units.results[0].status.UnitFileState == "disabled"
|
||||
- systemd_hardening_verify_units.results[1].status.UnitFileState == "masked"
|
||||
quiet: true
|
||||
|
||||
- name: Read the manager drop-in
|
||||
ansible.builtin.slurp:
|
||||
src: /etc/systemd/system.conf.d/hardening.conf
|
||||
register: systemd_hardening_verify_manager
|
||||
|
||||
- name: The manager drop-in holds the limits
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('[Manager]')
|
||||
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('DefaultLimitNOFILE=65535')
|
||||
- (systemd_hardening_verify_manager.content | b64decode).split('\n') is contains('DefaultLimitCORE=0')
|
||||
quiet: true
|
||||
|
||||
- name: Read the service drop-in
|
||||
ansible.builtin.slurp:
|
||||
src: /etc/systemd/system/testsvc.service.d/hardening.conf
|
||||
register: systemd_hardening_verify_dropin
|
||||
|
||||
- name: The drop-in renders booleans, lists and plain values
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- (systemd_hardening_verify_dropin.content | b64decode).split('\n') | select | list == systemd_hardening_verify_expected
|
||||
quiet: true
|
||||
vars:
|
||||
systemd_hardening_verify_expected:
|
||||
- "[Service]"
|
||||
- ProtectSystem=strict
|
||||
- NoNewPrivileges=yes
|
||||
- ReadWritePaths=/var/tmp /run
|
||||
- LimitNOFILE=1024
|
||||
|
||||
- name: Ask systemd about the drop-in service
|
||||
ansible.builtin.systemd:
|
||||
name: testsvc.service
|
||||
register: systemd_hardening_verify_testsvc
|
||||
check_mode: true
|
||||
|
||||
- name: The daemon reload made systemd load the drop-in
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- systemd_hardening_verify_testsvc.status.NoNewPrivileges == "yes"
|
||||
- systemd_hardening_verify_testsvc.status.ProtectSystem == "strict"
|
||||
quiet: true
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
- name: Disable unwanted services if present
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ item }}"
|
||||
enabled: false
|
||||
state: stopped
|
||||
loop: "{{ systemd_hardening_disable_services_effective }}"
|
||||
# Only act on installed units; items may be given with or without the
|
||||
# ".service" suffix.
|
||||
when: (item | regex_replace('\.service$', '') ~ '.service') in systemd_hardening_installed_units
|
||||
@@ -0,0 +1,41 @@
|
||||
---
|
||||
- name: Apply hardening drop-ins to opted-in services
|
||||
vars:
|
||||
# default(..., true) also covers an explicitly empty/null override
|
||||
systemd_hardening_dropin_map: "{{ systemd_hardening_services | default({}, true) }}"
|
||||
block:
|
||||
- name: Validate systemd_hardening_services
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- systemd_hardening_dropin_map is mapping
|
||||
- systemd_hardening_dropin_map.values() | reject('mapping') | list | length == 0
|
||||
fail_msg: >-
|
||||
systemd_hardening_services must be a mapping of unit -> mapping of
|
||||
directives.
|
||||
quiet: true
|
||||
|
||||
- name: Create drop-in directory for service
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "/etc/systemd/system/{{ systemd_hardening_item.key }}.service.d"
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0755"
|
||||
loop: "{{ systemd_hardening_dropin_map | dict2items }}"
|
||||
loop_control:
|
||||
loop_var: systemd_hardening_item
|
||||
notify: "systemd reload"
|
||||
|
||||
- name: Create drop-in configuration
|
||||
become: true
|
||||
ansible.builtin.template:
|
||||
src: dropin.j2
|
||||
dest: "/etc/systemd/system/{{ systemd_hardening_item.key }}.service.d/hardening.conf"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
loop: "{{ systemd_hardening_dropin_map | dict2items }}"
|
||||
loop_control:
|
||||
loop_var: systemd_hardening_item
|
||||
notify: "systemd reload"
|
||||
@@ -0,0 +1,26 @@
|
||||
---
|
||||
- name: Ensure system.conf.d directory exists
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: /etc/systemd/system.conf.d
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0755"
|
||||
|
||||
- name: Harden systemd manager (global settings)
|
||||
become: true
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system.conf.d/hardening.conf
|
||||
content: |
|
||||
[Manager]
|
||||
DefaultLimitNOFILE=65535
|
||||
DefaultLimitNPROC=16384
|
||||
DefaultLimitCORE=0
|
||||
DefaultTasksAccounting=yes
|
||||
DefaultMemoryAccounting=yes
|
||||
DefaultCPUAccounting=yes
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify: "systemd reload"
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
# 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
|
||||
|
||||
# Not service_facts: it returns a fact named `services`, which (with
|
||||
# inject_facts_as_vars) overrides the host_vars `services` list used by
|
||||
# playbooks/install_services.yml. Only installed unit files are listed, so
|
||||
# units that are merely referenced (LoadState not-found) are excluded.
|
||||
- name: List installed service unit files # noqa: command-instead-of-module
|
||||
become: true
|
||||
ansible.builtin.command: systemctl list-unit-files --type=service --no-legend --plain
|
||||
register: systemd_hardening_unit_files
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
tags: [disable, mask]
|
||||
|
||||
- name: Set installed service units
|
||||
ansible.builtin.set_fact:
|
||||
systemd_hardening_installed_units: "{{ systemd_hardening_unit_files.stdout_lines | map('split') | map('first') | list }}"
|
||||
tags: [disable, mask]
|
||||
|
||||
- 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
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
- name: Mask high-risk services
|
||||
become: true
|
||||
ansible.builtin.systemd:
|
||||
name: "{{ item }}"
|
||||
masked: true
|
||||
state: stopped
|
||||
loop: "{{ systemd_hardening_mask_services }}"
|
||||
# A unit that isn't installed can't be started, so masking it is unnecessary.
|
||||
when: (item | regex_replace('\.service$', '') ~ '.service') in systemd_hardening_installed_units
|
||||
@@ -0,0 +1,19 @@
|
||||
[Service]
|
||||
{#
|
||||
Rendering rules (checked in this order):
|
||||
- booleans -> yes/no
|
||||
- real lists -> space-joined (strings are Jinja sequences too, so
|
||||
they must be excluded explicitly, otherwise
|
||||
"strict" renders as "s t r i c t")
|
||||
- strings/numbers/etc -> rendered as-is
|
||||
systemd_hardening_item.value is validated as a mapping in tasks/dropins.yml before rendering.
|
||||
#}
|
||||
{% for k, v in systemd_hardening_item.value.items() %}
|
||||
{% if v is boolean %}
|
||||
{{ k }}={{ 'yes' if v else 'no' }}
|
||||
{% elif v is iterable and v is not string and v is not mapping %}
|
||||
{{ k }}={{ v | join(' ') }}
|
||||
{% else %}
|
||||
{{ k }}={{ v }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
Reference in new issue
Block a user