# platform = multi_platform_all
# reboot = false
# strategy = restrict
# complexity = low
# disruption = low
- name: Gather the package facts
  package_facts:
    manager: auto
  tags:
  - DISA-STIG-UBTU-22-653060
  - NIST-800-53-AC-6(1)
  - NIST-800-53-AU-9
  - NIST-800-53-CM-6(a)
  - directory_permissions_var_log_audit
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy

- name: System Audit Logs Must Have Mode 0750 or Less Permissive - Get audit log file
    from /etc/audit/auditd.conf
  ansible.builtin.command: grep -iw ^log_file /etc/audit/auditd.conf
  check_mode: false
  failed_when: false
  changed_when: false
  register: log_file_exists
  when:
  - '"auditd" in ansible_facts.packages'
  - '"linux-base" in ansible_facts.packages'
  tags:
  - DISA-STIG-UBTU-22-653060
  - NIST-800-53-AC-6(1)
  - NIST-800-53-AU-9
  - NIST-800-53-CM-6(a)
  - directory_permissions_var_log_audit
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy

- name: System Audit Logs Must Have Mode 0750 or Less Permissive - Set audit log directory
    path
  ansible.builtin.set_fact:
    log_file_dir: '{{ (log_file_exists.stdout | default('''') | split('' '') | last
      | dirname) | default(''/var/log/audit'', true) }}'
  when:
  - '"auditd" in ansible_facts.packages'
  - '"linux-base" in ansible_facts.packages'
  tags:
  - DISA-STIG-UBTU-22-653060
  - NIST-800-53-AC-6(1)
  - NIST-800-53-AU-9
  - NIST-800-53-CM-6(a)
  - directory_permissions_var_log_audit
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy

- name: System Audit Logs Must Have Mode 0750 or Less Permissive - Get audit log group
    from /etc/audit/auditd.conf
  ansible.builtin.command: grep -iw ^log_group /etc/audit/auditd.conf
  check_mode: false
  failed_when: false
  changed_when: false
  register: log_group_exists
  when:
  - '"auditd" in ansible_facts.packages'
  - '"linux-base" in ansible_facts.packages'
  tags:
  - DISA-STIG-UBTU-22-653060
  - NIST-800-53-AC-6(1)
  - NIST-800-53-AU-9
  - NIST-800-53-CM-6(a)
  - directory_permissions_var_log_audit
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy

- name: System Audit Logs Must Have Mode 0750 or Less Permissive - Set audit log group
  ansible.builtin.set_fact:
    log_group: '{{ (log_group_exists.stdout | default('''') | split('' '') | last)
      | default(''root'', true) }}'
  when:
  - '"auditd" in ansible_facts.packages'
  - '"linux-base" in ansible_facts.packages'
  tags:
  - DISA-STIG-UBTU-22-653060
  - NIST-800-53-AC-6(1)
  - NIST-800-53-AU-9
  - NIST-800-53-CM-6(a)
  - directory_permissions_var_log_audit
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy

- name: System Audit Logs Must Have Mode 0750 or Less Permissive - Set audit log directory
    permissions
  ansible.builtin.file:
    path: '{{ log_file_dir }}'
    state: directory
    mode: '{{ ''0700'' if log_group == ''root'' else ''0750'' }}'
  when:
  - '"auditd" in ansible_facts.packages'
  - '"linux-base" in ansible_facts.packages'
  tags:
  - DISA-STIG-UBTU-22-653060
  - NIST-800-53-AC-6(1)
  - NIST-800-53-AU-9
  - NIST-800-53-CM-6(a)
  - directory_permissions_var_log_audit
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
