---
- name: delete the mirror vm
  hosts: 127.0.0.1
  gather_facts: no
  become: yes
  tasks:
    - name: mirbr0 network exists
      command: "virsh -c qemu:///system net-dumpxml mirbr0"
      changed_when: false
      ignore_errors: true
      register: net_exists

    - name: storage pool exists
      command: "virsh -c qemu:///system pool-dumpxml mirror"
      changed_when: false
      ignore_errors: true
      register: pool_exists

    - name: mirror vm exists
      command: "virsh -c qemu:///system dumpxml mirror"
      changed_when: false
      ignore_errors: true
      register: vm_exists

    - name: delete vm
      command: "virsh -c qemu:///system {{ item }}"
      loop:
        - destroy mirror
        - undefine --nvram mirror
      when: vm_exists.rc == 0

    - name: delete storage pool
      command: "virsh -c qemu:///system {{ item }}"
      loop:
        - pool-destroy mirror
        - pool-undefine mirror
      when: pool_exists.rc == 0

    - name: delete mirbr0 bridge network
      command: "virsh -c qemu:///system {{ item }}"
      loop: 
        - net-destroy mirbr0
        - net-undefine mirbr0
      when: net_exists.rc == 0

    - name: delete directory for vm
      file:
        path: "{{ playbook_dir }}/vm"
        state: absent