# platform = multi_platform_all
# reboot = false
# strategy = restrict
# complexity = low
# disruption = low
- name: Gather the package facts
  package_facts:
    manager: auto
  tags:
  - accounts_have_homedir_login_defs
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy

- name: Ensure new users receive home directories
  block:

  - name: Check for duplicate values
    ansible.builtin.lineinfile:
      path: /etc/login.defs
      create: true
      regexp: (?i)^\s*CREATE_HOME\s+
      state: absent
    check_mode: true
    changed_when: false
    register: dupes

  - name: Deduplicate values from /etc/login.defs
    ansible.builtin.lineinfile:
      path: /etc/login.defs
      create: true
      regexp: (?i)^\s*CREATE_HOME\s+
      state: absent
    when: dupes.found is defined and dupes.found > 1

  - name: Insert correct line to /etc/login.defs
    ansible.builtin.lineinfile:
      path: /etc/login.defs
      create: true
      regexp: (?i)^\s*CREATE_HOME\s+
      line: CREATE_HOME yes
      state: present
  when: ( "login" in ansible_facts.packages and "linux-base" in ansible_facts.packages
    )
  tags:
  - accounts_have_homedir_login_defs
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
