Use Vagrantfile to build virtual site
[icn.git] / deploy / metal3-vm / vm-setup / roles / libvirt / tasks / vm_teardown_tasks.yml
1 # NB: We use `virsh` here instead of the `virt` module because
2 # these tasks may be called before the dependencies of the `virt`
3 # module are satisfied.
4
5 - name: Check if libvirt is available
6   command: >
7     virsh uri
8   ignore_errors: true
9   changed_when: false
10   register: libvirt_check
11   environment:
12     LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
13
14 # If libvirt isn't available we can skip everything else.
15 - when: libvirt_check is success
16   environment:
17     LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
18   block:
19
20     - when: vm_nodes
21       block:
22
23         # Check if the vm nodes exist.
24         - name: Check vm vms
25           command: >
26             virsh domid "{{ item.name }}"
27           with_items: "{{ vm_nodes }}"
28           ignore_errors: true
29           register: vm_check
30
31         # Destroy and undefine the vm nodes.
32         - name: Destroy vm vms
33           command:
34             virsh destroy "{{ item.item.name }}"
35           when: item is success
36           with_items: "{{ vm_check.results }}"
37           ignore_errors: true
38
39         - name: Undefine vm vms
40           command:
41             virsh undefine "{{ item.item.name }}"
42           when: item is success
43           with_items: "{{ vm_check.results }}"
44
45         # The `virsh vol-dumpxml ... > /dev/null` is here (and elsewhere) due to
46         # [1293804].
47         #
48         # [1293804]: https://bugzilla.redhat.com/show_bug.cgi?id=1293804
49         - name: Delete baremetal vm storage
50           shell: |
51             virsh vol-dumpxml --pool '{{ libvirt_volume_pool }}' \
52               '{{ item.name }}'.qcow2 2>&1 > /dev/null
53             virsh vol-delete --pool '{{ libvirt_volume_pool }}' \
54               '{{ item.name }}'.qcow2
55           with_items: "{{ vm_nodes }}"
56           ignore_errors: true
57
58     - name: Check volume pool
59       command: >
60         virsh pool-uuid "{{ libvirt_volume_pool }}"
61       register: pool_check
62       ignore_errors: true
63
64     # See https://www.redhat.com/archives/libvirt-users/2016-March/msg00123.html
65     # TL;DR: ensure that the pool really exists if the previous
66     # task says it does.
67     - name: Work around libvirt bug
68       shell: |
69         virsh pool-dumpxml "{{ libvirt_volume_pool }}" |
70         virsh pool-define /dev/stdin
71       when: pool_check is success
72
73     - name: Destroy volume pool
74       command: >
75         virsh pool-destroy "{{ libvirt_volume_pool }}"
76       when: pool_check is success
77       ignore_errors: true
78
79     - name: Undefine volume pool
80       command: >
81         virsh pool-undefine "{{ libvirt_volume_pool }}"
82       when: pool_check is success
83
84     - name: Get UID of pool user
85       command: id -u "{{ ansible_user_id }}"
86       register: pool_uid
87       changed_when: false
88       when: pool_check is success
89
90     - name: Destroy pool definition file
91       file:
92         path: "/run/user/{{ pool_uid.stdout }}/libvirt/storage/run/{{ libvirt_volume_pool }}.xml"
93         state: absent
94       when: pool_check is success