1
0
IaC/ansible/roles/vm_destroy/tasks/destroy.yaml
2022-10-30 23:57:26 +13:00

58 lines
1.4 KiB
YAML

---
- name: set vm_name
ansible.builtin.set_fact:
vm_name: "{{ vm_name_prefix }}{{ vm_number }}"
- name: debug vm_name
ansible.builtin.debug:
msg:
- "VM name is {{ vm_name }}"
- name: list vms
community.libvirt.virt:
command: list_vms
register: vm_list
- name: debug vm list
ansible.builtin.debug:
var: vm_list.list_vms
- name: proceed if vm exists
when: vm_name in vm_list.list_vms
block:
- name: shutdown the vm
community.libvirt.virt:
state: shutdown
name: "{{ vm_name }}"
# - name: destroy the vm
# community.libvirt.virt:
# state: destroyed
# name: "{{ vm_name }}"
- name: undefine the vm
community.libvirt.virt:
state: undefine
name: "{{ vm_name }}"
- name: delete the firmware vars file
ansible.builtin.file:
path: "{{ firmware_vhd_pool_dir }}/{{ vm_name }}_VARS.fd"
state: absent
- name: delete the root vhd
when: delete_root_vhd
ansible.builtin.file:
path: "{{ root_vhd_pool_dir }}/{{ vm_name }}_vda.qcow2"
state: absent
- name: delete the data vhds
when: delete_data_vhd
ansible.builtin.file:
path: "{{ item }}"
state: absent
with_items:
- "{{ containers_vhd_pool_dir }}/{{ vm_name }}_vdb.qcow2"
- "{{ data_nvme_vhd_pool_dir }}/{{ vm_name }}_vdc.qcow2"
- "{{ data_hdd_vhd_pool_dir }}/{{ vm_name }}_vdd.qcow2"