feat(bootstrap): add rescue block with VM cleanup on failure
This commit is contained in:
79
roles/virtualization/tasks/delete.yml
Normal file
79
roles/virtualization/tasks/delete.yml
Normal file
@@ -0,0 +1,79 @@
|
||||
---
|
||||
- name: Delete VMware VM
|
||||
when: hypervisor_type == "vmware"
|
||||
delegate_to: localhost
|
||||
community.vmware.vmware_guest:
|
||||
hostname: "{{ hypervisor_cfg.url }}"
|
||||
username: "{{ hypervisor_cfg.username }}"
|
||||
password: "{{ hypervisor_cfg.password }}"
|
||||
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
|
||||
datacenter: "{{ hypervisor_cfg.datacenter }}"
|
||||
name: "{{ hostname }}"
|
||||
folder: "{{ system_cfg.path if system_cfg.path | string | length > 0 else omit }}"
|
||||
state: absent
|
||||
force: true
|
||||
no_log: true
|
||||
|
||||
- name: Delete Proxmox VM
|
||||
when: hypervisor_type == "proxmox"
|
||||
delegate_to: localhost
|
||||
community.proxmox.proxmox_kvm:
|
||||
api_host: "{{ hypervisor_cfg.url }}"
|
||||
api_user: "{{ hypervisor_cfg.username }}"
|
||||
api_password: "{{ hypervisor_cfg.password }}"
|
||||
node: "{{ hypervisor_cfg.node }}"
|
||||
vmid: "{{ system_cfg.id | default(omit, true) }}"
|
||||
name: "{{ hostname }}"
|
||||
state: absent
|
||||
force: true
|
||||
no_log: true
|
||||
|
||||
- name: Destroy libvirt VM
|
||||
when: hypervisor_type == "libvirt"
|
||||
delegate_to: localhost
|
||||
block:
|
||||
- name: Stop libvirt VM
|
||||
community.libvirt.virt:
|
||||
name: "{{ hostname }}"
|
||||
state: destroyed
|
||||
uri: "{{ libvirt_uri | default('qemu:///system') }}"
|
||||
failed_when: false
|
||||
|
||||
- name: Undefine libvirt VM
|
||||
community.libvirt.virt:
|
||||
name: "{{ hostname }}"
|
||||
command: undefine
|
||||
uri: "{{ libvirt_uri | default('qemu:///system') }}"
|
||||
failed_when: false
|
||||
|
||||
- name: Remove libvirt disk images
|
||||
ansible.builtin.file:
|
||||
path: "{{ item.path }}"
|
||||
state: absent
|
||||
loop: "{{ virtualization_libvirt_disks | default([]) }}"
|
||||
loop_control:
|
||||
label: "{{ item.path | default('unknown') }}"
|
||||
|
||||
- name: Remove libvirt cloud-init disk
|
||||
ansible.builtin.file:
|
||||
path: "{{ virtualization_libvirt_cloudinit_path | default('/dev/null') }}"
|
||||
state: absent
|
||||
when: virtualization_libvirt_cloudinit_path is defined
|
||||
|
||||
- name: Destroy Xen VM
|
||||
when: hypervisor_type == "xen"
|
||||
delegate_to: localhost
|
||||
block:
|
||||
- name: Stop Xen VM
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- xl
|
||||
- destroy
|
||||
- "{{ hostname }}"
|
||||
failed_when: false
|
||||
|
||||
- name: Remove Xen VM config
|
||||
ansible.builtin.file:
|
||||
path: "/etc/xen/{{ hostname }}.cfg"
|
||||
state: absent
|
||||
failed_when: false
|
||||
Reference in New Issue
Block a user