# platform = multi_platform_all
# reboot = false
# strategy = configure
# complexity = low
# disruption = low
- name: Gather the package facts
  package_facts:
    manager: auto
  tags:
  - NIST-800-53-AU-4(1)
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - rsyslog_encrypt_offload_defaultnetstreamdriver

- name: Ensure Rsyslog Encrypts Off-Loaded Audit Records - Ensure /etc/rsyslog.conf
    exists
  ansible.builtin.file:
    path: /etc/rsyslog.conf
    state: touch
    modification_time: preserve
    access_time: preserve
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"rsyslog" in ansible_facts.packages'
  tags:
  - NIST-800-53-AU-4(1)
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - rsyslog_encrypt_offload_defaultnetstreamdriver

- name: Ensure Rsyslog Encrypts Off-Loaded Audit Records - Ensure /etc/rsyslog.d directory
    exists
  ansible.builtin.file:
    path: /etc/rsyslog.d
    state: directory
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"rsyslog" in ansible_facts.packages'
  tags:
  - NIST-800-53-AU-4(1)
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - rsyslog_encrypt_offload_defaultnetstreamdriver

- name: Ensure Rsyslog Encrypts Off-Loaded Audit Records - Remove RainerScript global()
    entries with DefaultNetstreamDriver from rsyslog.conf
  ansible.builtin.shell: |
    sed -i '/^[[:space:]]*global(/ { :a; N; /)/!ba; /DefaultNetstreamDriver/d }' /etc/rsyslog.conf
  changed_when: true
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"rsyslog" in ansible_facts.packages'
  tags:
  - NIST-800-53-AU-4(1)
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - rsyslog_encrypt_offload_defaultnetstreamdriver

- name: Ensure Rsyslog Encrypts Off-Loaded Audit Records - Remove RainerScript global()
    entries with DefaultNetstreamDriver from rsyslog.d/*.conf
  ansible.builtin.shell: |
    find /etc/rsyslog.d -type f -name "*.conf" -exec sed -i '/^[[:space:]]*global(/ { :a; N; /)/!ba; /DefaultNetstreamDriver/d }' {} +
  changed_when: true
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"rsyslog" in ansible_facts.packages'
  tags:
  - NIST-800-53-AU-4(1)
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - rsyslog_encrypt_offload_defaultnetstreamdriver

- name: Ensure Rsyslog Encrypts Off-Loaded Audit Records - Check if the parameter
    $DefaultNetstreamDriver is configured
  ansible.builtin.find:
    paths:
    - /etc/rsyslog.conf
    - /etc/rsyslog.d
    contains: ^\s*{{ "$DefaultNetstreamDriver"| regex_escape }}
  register: _sshd_config_has_parameter
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"rsyslog" in ansible_facts.packages'
  tags:
  - NIST-800-53-AU-4(1)
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - rsyslog_encrypt_offload_defaultnetstreamdriver

- name: Ensure Rsyslog Encrypts Off-Loaded Audit Records - Check if the parameter
    $DefaultNetstreamDriver is configured correctly
  ansible.builtin.find:
    paths:
    - /etc/rsyslog.conf
    - /etc/rsyslog.d
    contains: ^\s*{{ "$DefaultNetstreamDriver"| regex_escape }} gtls$
  register: _sshd_config_correctly
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"rsyslog" in ansible_facts.packages'
  tags:
  - NIST-800-53-AU-4(1)
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - rsyslog_encrypt_offload_defaultnetstreamdriver

- name: Ensure Rsyslog Encrypts Off-Loaded Audit Records
  block:

  - name: Deduplicate values from /etc/rsyslog.conf
    ansible.builtin.lineinfile:
      path: /etc/rsyslog.conf
      create: false
      regexp: '(?i)^\s*{{ "$DefaultNetstreamDriver"| regex_escape }} '
      state: absent

  - name: Check if /etc/rsyslog.d exists
    ansible.builtin.stat:
      path: /etc/rsyslog.d
    register: _etc_rsyslog_d_exists

  - name: Check if the parameter $DefaultNetstreamDriver is present in /etc/rsyslog.d
    ansible.builtin.find:
      paths: /etc/rsyslog.d
      recurse: 'yes'
      follow: 'no'
      contains: '^\s*{{ "$DefaultNetstreamDriver"| regex_escape }} '
    register: _etc_rsyslog_d_has_parameter
    when: _etc_rsyslog_d_exists.stat.isdir is defined and _etc_rsyslog_d_exists.stat.isdir

  - name: Remove parameter from files in /etc/rsyslog.d
    ansible.builtin.lineinfile:
      path: '{{ item.path }}'
      create: false
      regexp: '(?i)^\s*{{ "$DefaultNetstreamDriver"| regex_escape }} '
      state: absent
    with_items: '{{ _etc_rsyslog_d_has_parameter.files }}'
    when: _etc_rsyslog_d_has_parameter.matched | bool

  - name: Insert correct line to /etc/rsyslog.conf
    ansible.builtin.lineinfile:
      path: /etc/rsyslog.conf
      create: true
      regexp: '(?i)^\s*{{ "$DefaultNetstreamDriver"| regex_escape }} '
      line: $DefaultNetstreamDriver gtls
      state: present
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"rsyslog" in ansible_facts.packages'
  - _sshd_config_correctly.matched == 0 or _sshd_config_has_parameter.matched != 1
  tags:
  - NIST-800-53-AU-4(1)
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - rsyslog_encrypt_offload_defaultnetstreamdriver
