---
- name: Basic-auth | Manage the basic auth username and password on deb systems
  when:
    - ansible_distribution_file_variety == "Debian"
    - ansible_distribution_version is version_compare('18.04', '>=')
    - nginx_basic_auth
  tags: nginx
  block:
    - name: Basic-auth |Install the python passlib library on deb based distributions
      ansible.builtin.apt:
        name: python3-passlib
        state: present
        cache_valid_time: 3600

    - name: Basic-auth |Create the htpasswd file needed by the basic auth
      ansible.builtin.htpasswd: 
        path: '{{ item.file | default ("/etc/nginx/htpasswd") }}' 
        name: '{{ item.name }}'
        password: '{{ item.pwd }}'
        state: '{{ item.state | default("present") }}'
        crypt_scheme: '{{ item.crypt | default("sha256_crypt") }}'
      with_items:
        - '{{ nginx_basic_auth_users }}'

- name: Basic-auth | Manage the basic auth username and password on EL systems
  when:
    - ansible_distribution_file_variety == "RedHat"
    - nginx_basic_auth
  tags: nginx
  block:
    - name: Basic-auth |Install the python passlib library on RH based distributions
      ansible.builtin.dnf:
        pkg: python-passlib
        state: present

    - name: Basic-auth |Create the htpasswd file needed by the basic auth
      ansible.builtin.htpasswd:
        path: '{{ item.file | default ("/etc/nginx/htpasswd") }}'
        name: '{{ item.name }}'
        password: '{{ item.pwd }}'
        state: '{{ item.state | default("present") }}'
        crypt_scheme: '{{ item.crypt | default("sha256_crypt") }}'
      with_items: 
        - '{{ nginx_basic_auth_users }}'
