---
- name: Converge
  hosts: all
  gather_facts: false

  tasks:
    - name: Reset HTTPS certificate
      ansible.builtin.import_role:
        name: idrac_certificate
      vars:
        hostname: "{{ lookup('env', 'IDRAC_IP') }}"
        username: "{{ lookup('env', 'IDRAC_USER') }}"
        password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"
        validate_certs: false
        command: "reset"
        certificate_type: "HTTPS"

    - name: Asserting operation with check mode.
      ansible.builtin.assert:
        that: idrac_certificate_out.msg == "Changes found to be applied."
      when: ansible_check_mode

    - name: Asserting operation with Normal/Idempotence mode.
      ansible.builtin.assert:
        that: idrac_certificate_out.msg == "Successfully performed the
                                        'reset' certificate operation.iDRAC
                                        has been reset successfully."
      when: not ansible_check_mode and idrac_certificate_out.changed

    - name: Waiting for idrac readiness
      ansible.builtin.wait_for:
        timeout: 30
      when:
        - not ansible_check_mode
        - idrac_certificate_out is defined
        - idrac_certificate_out.changed
