---
- name: Converge
  hosts: all
  gather_facts: false
  vars:
    https_cert_ssl_failure: {}
    ca_cert_path: "{{ lookup('env', 'ca_cert_path') }}"
    path_for_import_cert: "{{ lookup('env', 'path_for_import_cert') }}"

  tasks:
    - name: Fetching firmware version for IDRAC
      ansible.builtin.include_tasks:
        file: ../__extract_firmware_version.yml
      vars:
        idrac_ip: "{{ lookup('env', 'IDRAC_IP') }}"
        idrac_user: "{{ lookup('env', 'IDRAC_USER') }}"
        idrac_password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"

    - name: Set expected firmware version
      ansible.builtin.set_fact:
        firmware_version_expected: "6.00.02.00"

    - name: Import Https certificate using ssl_key
      when: idrac_certificate_firmware_version is defined and
             "idrac_certificate_firmware_version >= firmware_version_expected"
      block:
        - name: Set fact
          ansible.builtin.set_fact:
            per_mode: "0755"

        - name: Create Directory
          ansible.builtin.file:
            path: "{{ path_for_import_cert }}"
            state: directory
            mode: "{{ per_mode }}"
          register: idrac_certificate_created_directory
          check_mode: false

        - name: Setting up certificate path
          ansible.builtin.stat:
            path: "{{ path_for_import_cert }}"
          register: idrac_certificate_check_file_created
          check_mode: false

        - name: Create SSL Key ans self-signed certificate
          when: idrac_certificate_check_file_created.stat.exists
          ansible.builtin.include_tasks:
            file: ../__get_ssl_key.yml

        - name: Importing HTTPS certificate using ssl_key
          ansible.builtin.import_role:
            name: dellemc.openmanage.idrac_certificate
          vars:
            hostname: "{{ lookup('env', 'IDRAC_IP') }}"
            username: "{{ lookup('env', 'IDRAC_USER') }}"
            password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"
            validate_certs: false
            command: "import"
            certificate_type: "HTTPS"
            certificate_path: "{{ path_for_import_cert }}/cert.pem"
            ssl_key: "{{ path_for_import_cert }}/cert.key"

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

        - 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 mode.
          ansible.builtin.assert:
            that: idrac_certificate_out.msg == "Successfully performed the SSL
                  key upload and 'import' certificate operation.
                  iDRAC has been reset successfully."
          when: not ansible_check_mode and idrac_certificate_out.changed

        - name: Asserting operation with idempotence.
          ansible.builtin.assert:
            that: idrac_certificate_out.msg == "No changes found to be applied."
          when: not ansible_check_mode and not idrac_certificate_out.changed

      rescue:
        - name: Set the failure messages for SSLKEY
          ansible.builtin.set_fact:
            https_cert_ssl_failure: "{{  https_cert_ssl_failure |
                     combine({'HTTPS_SSL_KEY_CERT_IMPORT':
                     {'msg': ansible_failed_result.msg,
                     'failed_task_name': ansible_failed_task.name}}) }}"

      always:
        - name: Deleting the directory
          ansible.builtin.include_tasks:
            file: ../__delete_directory.yml

    - name: Collecting failure
      ansible.builtin.debug:
        var: https_cert_ssl_failure
      when: https_cert_ssl_failure
      failed_when: true
