Skip to content

Commit 756689b

Browse files
Ensure libvirt_manager cleanup is safe when libvirt is not deployed
1 parent 7527d0c commit 756689b

File tree

1 file changed

+149
-140
lines changed

1 file changed

+149
-140
lines changed
Lines changed: 149 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,156 @@
11
---
2-
- name: List all of the existing virtual machines
3-
register: vms_list
4-
community.libvirt.virt:
5-
command: list_vms
6-
uri: "qemu:///system"
7-
8-
- name: Get pool configuration
9-
community.libvirt.virt_pool:
10-
command: facts
11-
uri: "qemu:///system"
12-
13-
- name: Filter out target environment
14-
ansible.builtin.set_fact:
15-
cleanup_vms: "{{ vms_list.list_vms | select('match', '^cifmw-.*$') }}"
16-
17-
- name: Expose cleanup list
18-
ansible.builtin.debug:
19-
var: cleanup_vms
20-
21-
- name: Destroy machine
22-
register: vm_destroy
23-
community.libvirt.virt:
24-
command: destroy
25-
name: "{{ item }}"
26-
uri: "qemu:///system"
27-
loop: "{{ cleanup_vms }}"
28-
failed_when:
29-
- vm_destroy.rc is defined
30-
- vm_destroy.rc > 1
31-
32-
- name: Undefine machine
33-
community.libvirt.virt:
34-
command: undefine
35-
flags:
36-
- keep_nvram
37-
- snapshots_metadata
38-
name: "{{ item }}"
39-
uri: "qemu:///system"
40-
loop: "{{ cleanup_vms }}"
41-
42-
- name: "(localhost) Clean ssh jumpers"
43-
when:
44-
- inventory_hostname != 'localhost'
45-
delegate_to: localhost
46-
vars:
47-
vm: "{{ item | replace('cifmw-', '') }}"
48-
ansible.builtin.blockinfile:
49-
path: "{{ lookup('env', 'HOME') }}/.ssh/config"
50-
marker: "## {mark} {{ vm }}"
51-
state: absent
52-
create: true
53-
loop: "{{ cleanup_vms }}"
54-
55-
- name: "({{ inventory_hostname }}) Clean ssh jumpers" # noqa: name[template]
56-
vars:
57-
vm: "{{ item | replace('cifmw-', '') }}"
58-
ansible.builtin.blockinfile:
59-
path: "{{ ansible_user_dir }}/.ssh/config"
60-
marker: "## {mark} {{ vm }}"
61-
state: absent
62-
create: true
63-
loop: "{{ cleanup_vms }}"
64-
65-
- name: Get network list
66-
register: nets_list
67-
community.libvirt.virt_net:
68-
command: list_nets
69-
uri: "qemu:///system"
70-
71-
- name: Filter out target nets
72-
ansible.builtin.set_fact:
73-
cleanup_nets: "{{ nets_list.list_nets | select('match', '^cifmw-.*$') }}"
74-
75-
- name: Expose cleanup list
76-
ansible.builtin.debug:
77-
var: cleanup_nets
78-
79-
- name: Destroy networks
80-
register: net_destroy
81-
community.libvirt.virt_net:
82-
command: destroy
83-
name: "{{ item }}"
84-
uri: "qemu:///system"
85-
loop: "{{ cleanup_nets }}"
86-
failed_when:
87-
- net_destroy.rc is defined
88-
- net_destroy.rc > 1
89-
90-
- name: Undefine networks
91-
community.libvirt.virt_net:
92-
command: undefine
93-
name: "{{ item }}"
94-
uri: "qemu:///system"
95-
loop: "{{ cleanup_nets }}"
96-
97-
- name: Remove cifmw storage pool
98-
vars:
99-
action: "delete"
100-
ansible.builtin.include_tasks: storage_pool.yml
101-
102-
- name: Remove custom images from oooq_pool if exists
103-
when:
104-
- item is match('^ocp-[0-9]\.qcow2+$')
105-
ansible.builtin.command:
106-
cmd: >-
107-
virsh -c qemu:///system vol-delete
108-
--vol {{ item }}
109-
--pool oooq_pool
110-
loop: "{{ ansible_libvirt_pools['oooq_pool'].volumes | default([]) }}"
111-
112-
- name: Get temporary key status
113-
register: _tmp_key
114-
ansible.builtin.stat:
115-
path: "{{ ansible_user_dir }}/.ssh/cifmw_reproducer_key.pub"
116-
117-
- name: Remove temporary ssh key from authorized_keys
118-
when:
119-
- _tmp_key.stat.exists
2+
- name: Get installed packages list
3+
ansible.builtin.package_facts: {}
4+
5+
- name: Perform the libvirt cleanup
6+
when: >-
7+
cifmw_libvirt_manager_dependency_packages |
8+
difference(ansible_facts.packages.keys()) |
9+
length == 0
12010
block:
121-
- name: Get public key
122-
register: _pub_key
123-
ansible.builtin.slurp:
124-
path: "{{ ansible_user_dir }}/.ssh/cifmw_reproducer_key.pub"
125-
126-
- name: Remove public key
127-
ansible.posix.authorized_key:
128-
user: "{{ ansible_user_id }}"
129-
key: "{{ _pub_key['content'] | b64decode }}"
11+
- name: List all of the existing virtual machines
12+
register: vms_list
13+
community.libvirt.virt:
14+
command: list_vms
15+
uri: "qemu:///system"
16+
17+
- name: Get pool configuration
18+
community.libvirt.virt_pool:
19+
command: facts
20+
uri: "qemu:///system"
21+
22+
- name: Filter out target environment
23+
ansible.builtin.set_fact:
24+
cleanup_vms: "{{ vms_list.list_vms | select('match', '^cifmw-.*$') }}"
25+
26+
- name: Expose cleanup list
27+
ansible.builtin.debug:
28+
var: cleanup_vms
29+
30+
- name: Destroy machine
31+
register: vm_destroy
32+
community.libvirt.virt:
33+
command: destroy
34+
name: "{{ item }}"
35+
uri: "qemu:///system"
36+
loop: "{{ cleanup_vms }}"
37+
failed_when:
38+
- vm_destroy.rc is defined
39+
- vm_destroy.rc > 1
40+
41+
- name: Undefine machine
42+
community.libvirt.virt:
43+
command: undefine
44+
flags:
45+
- keep_nvram
46+
- snapshots_metadata
47+
name: "{{ item }}"
48+
uri: "qemu:///system"
49+
loop: "{{ cleanup_vms }}"
50+
51+
- name: "(localhost) Clean ssh jumpers"
52+
when:
53+
- inventory_hostname != 'localhost'
54+
delegate_to: localhost
55+
vars:
56+
vm: "{{ item | replace('cifmw-', '') }}"
57+
ansible.builtin.blockinfile:
58+
path: "{{ lookup('env', 'HOME') }}/.ssh/config"
59+
marker: "## {mark} {{ vm }}"
13060
state: absent
61+
create: true
62+
loop: "{{ cleanup_vms }}"
63+
64+
- name: "({{ inventory_hostname }}) Clean ssh jumpers" # noqa: name[template]
65+
vars:
66+
vm: "{{ item | replace('cifmw-', '') }}"
67+
ansible.builtin.blockinfile:
68+
path: "{{ ansible_user_dir }}/.ssh/config"
69+
marker: "## {mark} {{ vm }}"
70+
state: absent
71+
create: true
72+
loop: "{{ cleanup_vms }}"
73+
74+
- name: Get network list
75+
register: nets_list
76+
community.libvirt.virt_net:
77+
command: list_nets
78+
uri: "qemu:///system"
79+
80+
- name: Filter out target nets
81+
ansible.builtin.set_fact:
82+
cleanup_nets: "{{ nets_list.list_nets | select('match', '^cifmw-.*$') }}"
83+
84+
- name: Expose cleanup list
85+
ansible.builtin.debug:
86+
var: cleanup_nets
87+
88+
- name: Destroy networks
89+
register: net_destroy
90+
community.libvirt.virt_net:
91+
command: destroy
92+
name: "{{ item }}"
93+
uri: "qemu:///system"
94+
loop: "{{ cleanup_nets }}"
95+
failed_when:
96+
- net_destroy.rc is defined
97+
- net_destroy.rc > 1
98+
99+
- name: Undefine networks
100+
community.libvirt.virt_net:
101+
command: undefine
102+
name: "{{ item }}"
103+
uri: "qemu:///system"
104+
loop: "{{ cleanup_nets }}"
105+
106+
- name: Remove cifmw storage pool
107+
vars:
108+
action: "delete"
109+
ansible.builtin.include_tasks: storage_pool.yml
110+
111+
- name: Remove custom images from oooq_pool if exists
112+
when:
113+
- item is match('^ocp-[0-9]\.qcow2+$')
114+
ansible.builtin.command:
115+
cmd: >-
116+
virsh -c qemu:///system vol-delete
117+
--vol {{ item }}
118+
--pool oooq_pool
119+
loop: "{{ ansible_libvirt_pools['oooq_pool'].volumes | default([]) }}"
120+
121+
- name: Get temporary key status
122+
register: _tmp_key
123+
ansible.builtin.stat:
124+
path: "{{ ansible_user_dir }}/.ssh/cifmw_reproducer_key.pub"
131125

132-
- name: Remove keypair
126+
- name: Remove temporary ssh key from authorized_keys
127+
when:
128+
- _tmp_key.stat.exists
129+
block:
130+
- name: Get public key
131+
register: _pub_key
132+
ansible.builtin.slurp:
133+
path: "{{ ansible_user_dir }}/.ssh/cifmw_reproducer_key.pub"
134+
135+
- name: Remove public key
136+
ansible.posix.authorized_key:
137+
user: "{{ ansible_user_id }}"
138+
key: "{{ _pub_key['content'] | b64decode }}"
139+
state: absent
140+
141+
- name: Remove keypair
142+
ansible.builtin.file:
143+
state: absent
144+
path: "{{ ansible_user_dir }}/.ssh/{{ item }}"
145+
loop:
146+
- cifmw_reproducer_key.pub
147+
- cifmw_reproducer_key
148+
149+
- name: Remove data directories
133150
ansible.builtin.file:
151+
path: "{{ cifmw_libvirt_manager_basedir }}/{{ item }}"
134152
state: absent
135-
path: "{{ ansible_user_dir }}/.ssh/{{ item }}"
136153
loop:
137-
- cifmw_reproducer_key.pub
138-
- cifmw_reproducer_key
139-
140-
- name: Remove data directories
141-
ansible.builtin.file:
142-
path: "{{ cifmw_libvirt_manager_basedir }}/{{ item }}"
143-
state: absent
144-
loop:
145-
- workload
146-
- images
147-
- volumes
154+
- workload
155+
- images
156+
- volumes

0 commit comments

Comments
 (0)