# 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-IA-5(1)(c)
  - NIST-800-53-IA-5(1).1(v)
  - NIST-800-53-IA-7
  - NIST-800-53-IA-7.1
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - set_password_hashing_yescrypt_cost_factor_logindefs
- (xccdf-var var_password_yescrypt_cost_factor_login_defs)

- name: Set yescrypt Cost Factor in /etc/login.defs
  block:

  - name: Check for duplicate values
    ansible.builtin.lineinfile:
      path: /etc/login.defs
      create: true
      regexp: (?i)^\s*YESCRYPT_COST_FACTOR\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*YESCRYPT_COST_FACTOR\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*YESCRYPT_COST_FACTOR\s*
      line: YESCRYPT_COST_FACTOR {{ var_password_yescrypt_cost_factor_login_defs }}
      state: present
  when: '"linux-base" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-5(1)(c)
  - NIST-800-53-IA-5(1).1(v)
  - NIST-800-53-IA-7
  - NIST-800-53-IA-7.1
  - configure_strategy
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - set_password_hashing_yescrypt_cost_factor_logindefs
