--- - name: Install auditd and its plugins ansible.builtin.package: name: "{{ ['audit', 'audispd-plugins'] if ansible_facts['os_family'] == 'RedHat' else ['auditd', 'audispd-plugins'] }}" state: present - name: Write the rules ansible.builtin.template: src: 50-iac.rules.j2 dest: /etc/audit/rules.d/50-iac.rules owner: root group: root mode: "0640" notify: Load the rules # -e 2 must be the last rule loaded, so it gets a file that sorts after all # the others. - name: Lock the rules until reboot ansible.builtin.copy: dest: /etc/audit/rules.d/99-iac-lock.rules content: "## Managed by Ansible. Rules cannot change until reboot.\n-e 2\n" owner: root group: root mode: "0640" when: audit_rules_immutable | bool notify: Load the rules - name: Remove the lock when it is turned off ansible.builtin.file: path: /etc/audit/rules.d/99-iac-lock.rules state: absent when: not audit_rules_immutable | bool notify: Load the rules # The syslog plugin ships switched off. Turned on, every audit record also # goes to syslog, which the host log template forwards. - name: Send audit records to syslog ansible.builtin.lineinfile: path: /etc/audit/plugins.d/syslog.conf regexp: '^active\s*=' line: "active = {{ 'yes' if audit_rules_to_syslog else 'no' }}" notify: Restart auditd - name: Make sure auditd is enabled and running ansible.builtin.service: name: auditd enabled: true state: started - name: Flush handlers so the check below reads the loaded rules ansible.builtin.meta: flush_handlers - name: Read back the rules the kernel has ansible.builtin.command: auditctl -l changed_when: false register: audit_rules_loaded when: not ansible_check_mode - name: Confirm the rules are loaded ansible.builtin.assert: that: - "'-k identity' in audit_rules_loaded.stdout or 'key=identity' in audit_rules_loaded.stdout" fail_msg: >- The kernel does not have the rules on {{ inventory_hostname }}. If the rules were locked with -e 2 before this change, reboot to load the new set. success_msg: Audit rules are loaded. when: not ansible_check_mode