---
- name: Destroy
  hosts: localhost
  connection: local
  gather_facts: false
  no_log: "{{ molecule_no_log }}"
  become: "{{ not (item.rootless | default(true)) }}"
  vars:
    podman_exec: "{{ lookup('env','MOLECULE_PODMAN_EXECUTABLE') | default('podman',true) }}"
  tasks:
    - name: Get passwd entries for USER env
      ansible.builtin.getent:
        database: passwd
        key: "{{ lookup('env', 'USER') }}"

    - name: Get shell async_dir
      ansible.builtin.set_fact:
        _shell_async_dir: >-
          {{ lookup('ansible.builtin.config', 'async_dir', plugin_type='shell', plugin_name='sh')
             | regex_replace('^~', ansible_facts.getent_passwd[lookup('env', 'USER')][4]) }}

    - name: Set async_dir for HOME env
      ansible.builtin.set_fact:
        ansible_async_dir: >-
          {{ _shell_async_dir
             | regex_replace('^' + ansible_facts.getent_passwd[lookup('env', 'USER')][4], lookup('env', 'HOME')) }}
      when: lookup('env', 'HOME') != ansible_facts.getent_passwd[lookup('env', 'USER')][4]

    - name: Destroy molecule instance(s)
      ansible.builtin.shell: "{{ podman_exec }} container exists {{ item.name }} && {{ podman_exec }} rm -f {{ item.name }} || true"
      register: server
      with_items: "{{ molecule_yml.platforms }}"
      async: 7200
      poll: 0
      changed_when: true

    - name: Wait for instance(s) deletion to complete
      become: "{{ not item.item.rootless | default(omit) }}"
      ansible.builtin.async_status:
        jid: "{{ item.ansible_job_id }}"
      register: podman_jobs
      until: podman_jobs is finished
      retries: 300
      with_items: "{{ server.results }}"

    - name: Delete podman network dedicated to this scenario
      containers.podman.podman_network:
        name: "{{ item.network }}"
        executable: "{{ podman_exec }}"
        state: absent
      when:
        - item.network is defined
      loop: "{{ molecule_yml.platforms | flatten(levels=1) }}"
      loop_control:
        extended: true
        label: "{{ item.name }}: {{ item.network | default('None specified') }}"
