fix(system_baseline): persist sysctls in an own sysctl.d drop-in
systemd-sysctl on Debian 13 does not read /etc/sysctl.conf, so the hardened values were lost on reboot. The role now applies the live values on their own, writes them to a drop-in under /etc/sysctl.d and removes its keys from /etc/sysctl.conf. Refs #510 Assisted-by: Claude Code:claude-opus-5-5
This commit is contained in:
1 parent
cf99893855
commit
d88e5d237a
14 files changed
+282
-17
No files matched your search
@@ -38,7 +38,12 @@
|
||||
when: system_baseline_apt_daily_timeout | length > 0
|
||||
tags: apt-daily-timeout
|
||||
|
||||
- name: Harden sysctl
|
||||
# Applied first, so a value the kernel refuses stops the run before it is persisted. molecule's containers have a read-only /proc/sys.
|
||||
- name: Apply the sysctl hardening
|
||||
ansible.builtin.import_tasks: sysctl_apply.yml
|
||||
tags: [sysctl, molecule-notest]
|
||||
|
||||
- name: Persist the sysctl hardening
|
||||
ansible.builtin.import_tasks: sysctl.yml
|
||||
tags: sysctl
|
||||
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
---
|
||||
- name: Apply sysctl hardening settings
|
||||
ansible.posix.sysctl:
|
||||
name: "{{ item.name }}"
|
||||
value: "{{ item.value }}"
|
||||
sysctl_set: true
|
||||
reload: true
|
||||
loop: "{{ system_baseline_sysctl_hardening }}"
|
||||
# Values persist in the role's own drop-in: systemd-sysctl does not read /etc/sysctl.conf on every supported release.
|
||||
- name: Persist the sysctl hardening settings
|
||||
ansible.builtin.template:
|
||||
src: sysctl.conf.j2
|
||||
dest: "{{ system_baseline_sysctl_file }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
become: true
|
||||
|
||||
# Not ansible.posix.sysctl, which rewrites every other line of the file. A missing file is left missing.
|
||||
- name: Remove the role's keys from /etc/sysctl.conf
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/sysctl.conf
|
||||
regexp: '^\s*{{ item | regex_escape }}\s*='
|
||||
state: absent
|
||||
loop: "{{ __system_baseline_sysctl.keys() | list }}"
|
||||
become: true
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
# Not ansible.posix.sysctl, which always manages a file too. The absolute path is for a PATH without the sbin directories.
|
||||
# A key that cannot be read (unknown or write-only) counts as different, so the write below decides.
|
||||
- name: Read the live sysctl values
|
||||
ansible.builtin.command:
|
||||
argv: [/usr/sbin/sysctl, -e, -n, "{{ item.key }}"]
|
||||
loop: "{{ __system_baseline_sysctl | dict2items }}"
|
||||
register: __system_baseline_sysctl_live
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
check_mode: false
|
||||
become: true
|
||||
|
||||
# `removes` names a path that always exists: with it, check mode reports the pending write as changed instead of skipping the command.
|
||||
# sysctl exits 0 for a read-only key and only warns (`setting key ...`), so stderr counts too.
|
||||
- name: Apply the sysctl hardening settings
|
||||
ansible.builtin.command:
|
||||
argv: [/usr/sbin/sysctl, -w, "{{ item.item.key }}={{ item.item.value }}"]
|
||||
removes: /proc/sys
|
||||
environment:
|
||||
LC_ALL: C
|
||||
loop: "{{ __system_baseline_sysctl_live.results }}"
|
||||
loop_control:
|
||||
label: "{{ item.item.key }}={{ item.item.value }}"
|
||||
when: item.stdout | default('') | split != item.item.value | split
|
||||
register: __system_baseline_sysctl_apply
|
||||
failed_when: >-
|
||||
__system_baseline_sysctl_apply.rc | default(0) != 0
|
||||
or __system_baseline_sysctl_apply.stderr | default('') is search('setting key')
|
||||
become: true
|
||||
@@ -62,6 +62,18 @@
|
||||
quiet: true
|
||||
tags: always
|
||||
|
||||
# The role owns the whole file, and systemd-sysctl reads only *.conf. 99-sysctl.conf can be the distribution's link to /etc/sysctl.conf.
|
||||
- name: Validate the sysctl drop-in path
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- system_baseline_sysctl_file is match('^/etc/sysctl\.d/[A-Za-z0-9_][A-Za-z0-9._-]*\.conf\Z')
|
||||
- system_baseline_sysctl_file != '/etc/sysctl.d/99-sysctl.conf'
|
||||
fail_msg: >-
|
||||
system_baseline_sysctl_file must be a .conf file directly under /etc/sysctl.d (letters, digits, . _ and - in the name), other than
|
||||
99-sysctl.conf.
|
||||
quiet: true
|
||||
tags: always
|
||||
|
||||
# The value is written into a unit file: one line, starting with a digit, so no line break can add a second directive. systemd itself
|
||||
# refuses a value it cannot parse (it ignores the setting and logs it).
|
||||
- name: Validate the apt-daily start timeout
|
||||
|
||||
Reference in new issue
Block a user