---
- name: Delete stack
  shell: >
    echo "Executing 'openstack stack delete {{ stack_name }} --yes'" >>  {{ stack_log }};
    openstack stack delete {{ stack_name }} --yes &>> {{ stack_log }} && {
    export COUNTER=0;
      while openstack stack show {{ stack_name }} &>> {{ stack_log }} ; do
        if [ $COUNTER -lt 10 ]; then
          sleep 30;
          if openstack stack show {{ stack_name }} | grep FAILED ; then
            echo "Failed deleting OVB stack" | tee -a {{ stack_log }};
            openstack stack show {{ stack_name }} &>> {{ stack_log }};
            openstack stack failures list --long {{ stack_name }} &>> {{ stack_log }} || echo 'Failed to list stack failures' >> {{ stack_log }};
            # NOTE(emilien) "openstack overcloud failures" was introduced in Rocky
            openstack overcloud failures &>> {{ stack_log }} || echo 'Failed to list deployment failures' >> {{ stack_log }};
            exit 0;
          fi;
          COUNTER=$((COUNTER+1));
        fi;
      done; } || echo "No stack {{ stack_name }} present" >> {{ stack_log }}
  environment:
    OS_USERNAME: "{{ os_username }}"
    OS_PASSWORD: "{{ os_password }}"
    OS_TENANT_NAME: "{{ os_tenant_name }}"
    OS_AUTH_URL: "{{ os_auth_url }}"
  args:
    executable: /bin/bash
  register: delete_stack

- name: Fail if stack was not deleted properly
  fail:
    msg: 'Deletion of stack {{ stack_name }} failed, log: {{ stack_log }}'
  when: '"Failed deleting OVB stack" in delete_stack.stdout'
