---
- name: Test ipauser random password generation
  hosts: ipaserver
  become: true

  tasks:
  - name: Users user1 and user2 absent
    ipauser:
      ipaadmin_password: SomeADMINpassword
      name:
      - user1
      - user2
      state: absent

  - name: User user1 present with random password
    ipauser:
      ipaadmin_password: SomeADMINpassword
      name: user1
      first: first1
      last: last1
      random: yes
      update_password: on_create
    register: ipauser
    failed_when: not ipauser.changed or
                 ipauser.user.randompassword is not defined or
                 ipauser.failed

  - name: Print generated random password
    ansible.builtin.debug:
      var: ipauser.user.randompassword

  - name: User user1 absent
    ipauser:
      ipaadmin_password: SomeADMINpassword
      name:
      - user1
      state: absent

  - name: User user1 is present with random password using users parameter
    ipauser:
      ipaadmin_password: SomeADMINpassword
      users:
      - name: user1
        first: first1
        last: last1
        random: yes
      update_password: on_create
    register: ipauser
    failed_when: not ipauser.changed or
                 ipauser.user.user1.randompassword is not defined or
                 ipauser.failed

  - name: User user1 absent
    ipauser:
      ipaadmin_password: SomeADMINpassword
      name:
      - user1
      state: absent

  - name: Users user1 and user2 present with random password
    ipauser:
      ipaadmin_password: SomeADMINpassword
      users:
      - name: user1
        first: first1
        last: last1
        random: yes
      - name: user2
        first: first2
        last: last2
        random: yes
      update_password: on_create
    register: ipauser
    failed_when: not ipauser.changed or
                 ipauser.user.user1.randompassword is not defined or
                 ipauser.user.user2.randompassword is not defined or
                 ipauser.failed

  - name: Print generated random password for user1
    ansible.builtin.debug:
      var: ipauser.user.user1.randompassword

  - name: Print generated random password for user2
    ansible.builtin.debug:
      var: ipauser.user.user2.randompassword

  - name: Users user1 and user2 absent
    ipauser:
      ipaadmin_password: SomeADMINpassword
      name:
      - user1
      - user2
      state: absent
