--- # 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 | rejectattr('key', 'search', '[*]') }}" register: __system_baseline_sysctl_live changed_when: false failed_when: false check_mode: false become: true # The directory is missing when the kernel runs without IPv6; a mistyped IPv6 key still fails when it exists. - name: Check that the kernel has IPv6 ansible.builtin.stat: path: /proc/sys/net/ipv6/conf register: __system_baseline_sysctl_ipv6 # `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 - __system_baseline_sysctl_ipv6.stat.exists or item.item.key is not match('net[.]ipv6[.]') 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 # After the plain keys: an interface created from here on inherits the new `default`. A wildcard without a match is a typo, as # an unknown plain key is, unless the kernel has no IPv6. sysctl exits 0 when an interface disappears while it lists them. - name: Read the live values a wildcard matches ansible.builtin.command: argv: [/usr/sbin/sysctl, -a, -e, -r, "^{{ item.key | replace('.', '[.]') | replace('*', '[^.]*') }}$"] loop: "{{ __system_baseline_sysctl | dict2items | selectattr('key', 'search', '[*]') }}" register: __system_baseline_sysctl_wildcard changed_when: false failed_when: >- __system_baseline_sysctl_wildcard.rc != 0 or (__system_baseline_sysctl_wildcard.stdout | length == 0 and (__system_baseline_sysctl_ipv6.stat.exists or item.key is not match('net[.]ipv6[.]'))) check_mode: false become: true # A key with its own entry keeps that value, as at boot. -e skips an interface that is gone, unless it goes during the write itself. - name: Apply the sysctl wildcards ansible.builtin.command: argv: "{{ ['/usr/sbin/sysctl', '-e', '-w'] + (__system_baseline_sysctl_pending | product([item.item.value]) | map('join', '=')) }}" removes: /proc/sys environment: LC_ALL: C vars: __system_baseline_sysctl_pending: >- {{ item.stdout_lines | reject('search', ' = ' ~ (item.item.value | regex_escape) ~ '$') | map('regex_replace', ' = .*', '') | difference(__system_baseline_sysctl.keys() | list) }} loop: "{{ __system_baseline_sysctl_wildcard.results }}" loop_control: label: "{{ item.item.key }}={{ item.item.value }}" when: __system_baseline_sysctl_pending | length > 0 register: __system_baseline_sysctl_wildcard_apply failed_when: >- __system_baseline_sysctl_wildcard_apply.stderr_lines | default([]) | reject('search', 'No such file or directory') | length > 0 become: true