refactor(cleanup): restructure dispatch to use hypervisor_type include

This commit is contained in:
2026-02-20 21:16:28 +01:00
parent cd8e477534
commit 5ca1c7f570

View File

@@ -6,16 +6,7 @@
ansible.builtin.include_tasks: shutdown.yml ansible.builtin.include_tasks: shutdown.yml
- name: Cleanup hypervisor resources - name: Cleanup hypervisor resources
ansible.builtin.include_tasks: proxmox.yml ansible.builtin.include_tasks: "{{ hypervisor_type }}.yml"
- name: Cleanup vCenter resources
ansible.builtin.include_tasks: vmware.yml
- name: Cleanup libvirt resources
ansible.builtin.include_tasks: libvirt.yml
- name: Cleanup Xen resources
ansible.builtin.include_tasks: xen.yml
- name: Determine post-reboot connectivity - name: Determine post-reboot connectivity
ansible.builtin.set_fact: ansible.builtin.set_fact:
@@ -34,7 +25,6 @@
) )
) | bool ) | bool
}} }}
changed_when: false
- name: Check VM accessibility after reboot - name: Check VM accessibility after reboot
when: when:
@@ -53,6 +43,7 @@
when: when:
- cleanup_vm_connection_check is defined - cleanup_vm_connection_check is defined
- cleanup_vm_connection_check.failed | bool - cleanup_vm_connection_check.failed | bool
- virtualization_vm_created_in_run | default(false) | bool
block: block:
- name: VM boot failure detected - removing VM - name: VM boot failure detected - removing VM
ansible.builtin.debug: ansible.builtin.debug:
@@ -61,151 +52,111 @@
This VM was created in the current playbook run and will be removed This VM was created in the current playbook run and will be removed
to prevent orphaned resources. to prevent orphaned resources.
- name: Remove VM for libvirt - name: Remove failed libvirt VM
when: when: hypervisor_type == "libvirt"
- hypervisor_type == "libvirt"
- virtualization_vm_created_in_run | default(false) | bool
delegate_to: localhost delegate_to: localhost
become: false become: false
community.libvirt.virt: block:
name: "{{ hostname }}" - name: Destroy libvirt VM
state: destroyed community.libvirt.virt:
name: "{{ hostname }}"
state: destroyed
failed_when: false
- name: Undefine VM for libvirt - name: Undefine libvirt VM
when: community.libvirt.virt:
- hypervisor_type == "libvirt" name: "{{ hostname }}"
- virtualization_vm_created_in_run | default(false) | bool command: undefine
delegate_to: localhost
become: false
community.libvirt.virt:
name: "{{ hostname }}"
command: undefine
- name: Remove VM disk for libvirt - name: Remove libvirt VM disks
when: ansible.builtin.file:
- hypervisor_type == "libvirt" path: "{{ item.path }}"
- virtualization_vm_created_in_run | default(false) | bool state: absent
delegate_to: localhost loop: "{{ virtualization_libvirt_disks | default([]) }}"
become: false loop_control:
ansible.builtin.file: label: "{{ item.path }}"
path: "{{ item.path }}"
state: absent
loop: "{{ virtualization_libvirt_disks | default([]) }}"
loop_control:
label: "{{ item.path }}"
- name: Remove cloud-init disk for libvirt - name: Remove libvirt cloud-init disk
when: ansible.builtin.file:
- hypervisor_type == "libvirt" path: "{{ virtualization_libvirt_cloudinit_path }}"
- virtualization_vm_created_in_run | default(false) | bool state: absent
delegate_to: localhost
become: false
ansible.builtin.file:
path: "{{ virtualization_libvirt_cloudinit_path }}"
state: absent
- name: Remove VM for proxmox - name: Remove failed Proxmox VM
when: when: hypervisor_type == "proxmox"
- hypervisor_type == "proxmox"
- virtualization_vm_created_in_run | default(false) | bool
delegate_to: localhost delegate_to: localhost
become: false become: false
community.proxmox.proxmox_kvm: module_defaults:
api_host: "{{ hypervisor_cfg.url }}" community.proxmox.proxmox_kvm:
api_user: "{{ hypervisor_cfg.username }}" api_host: "{{ hypervisor_cfg.url }}"
api_password: "{{ hypervisor_cfg.password }}" api_user: "{{ hypervisor_cfg.username }}"
node: "{{ hypervisor_cfg.host }}" api_password: "{{ hypervisor_cfg.password }}"
name: "{{ hostname }}" node: "{{ hypervisor_cfg.host }}"
vmid: "{{ system_cfg.id }}"
state: stopped
no_log: true no_log: true
block:
- name: Stop Proxmox VM
community.proxmox.proxmox_kvm:
name: "{{ hostname }}"
vmid: "{{ system_cfg.id }}"
state: stopped
- name: Delete VM for proxmox - name: Delete Proxmox VM
when: community.proxmox.proxmox_kvm:
- hypervisor_type == "proxmox" name: "{{ hostname }}"
- virtualization_vm_created_in_run | default(false) | bool vmid: "{{ system_cfg.id }}"
state: absent
unprivileged: false
- name: Remove failed VMware VM
when: hypervisor_type == "vmware"
delegate_to: localhost delegate_to: localhost
become: false become: false
community.proxmox.proxmox_kvm: module_defaults:
api_host: "{{ hypervisor_cfg.url }}" community.vmware.vmware_guest:
api_user: "{{ hypervisor_cfg.username }}" hostname: "{{ hypervisor_cfg.url }}"
api_password: "{{ hypervisor_cfg.password }}" username: "{{ hypervisor_cfg.username }}"
node: "{{ hypervisor_cfg.host }}" password: "{{ hypervisor_cfg.password }}"
name: "{{ hostname }}" validate_certs: "{{ hypervisor_cfg.certs | bool }}"
vmid: "{{ system_cfg.id }}"
state: absent
unprivileged: false
no_log: true no_log: true
block:
- name: Power off VMware VM
community.vmware.vmware_guest:
name: "{{ hostname }}"
folder: "{{ system_cfg.path | default('/') }}"
state: poweredoff
- name: Remove VM for VMware - name: Delete VMware VM
when: community.vmware.vmware_guest:
- hypervisor_type == "vmware" name: "{{ hostname }}"
- virtualization_vm_created_in_run | default(false) | bool folder: "{{ system_cfg.path | default('/') }}"
state: absent
- name: Remove failed Xen VM
when: hypervisor_type == "xen"
delegate_to: localhost delegate_to: localhost
become: false become: false
community.vmware.vmware_guest: block:
hostname: "{{ hypervisor_cfg.url }}" - name: Destroy Xen VM if running
username: "{{ hypervisor_cfg.username }}" ansible.builtin.command:
password: "{{ hypervisor_cfg.password }}" argv:
validate_certs: "{{ hypervisor_cfg.certs | bool }}" - xl
name: "{{ hostname }}" - destroy
folder: "{{ system_cfg.path | default('/') }}" - "{{ hostname }}"
state: poweredoff register: cleanup_xen_destroy
no_log: true failed_when: false
changed_when: cleanup_xen_destroy.rc == 0
- name: Delete VM for VMware - name: Remove Xen VM disks
when: ansible.builtin.file:
- hypervisor_type == "vmware" path: "{{ item.path }}"
- virtualization_vm_created_in_run | default(false) | bool state: absent
delegate_to: localhost loop: "{{ virtualization_xen_disks | default([]) }}"
become: false loop_control:
community.vmware.vmware_guest: label: "{{ item.path }}"
hostname: "{{ hypervisor_cfg.url }}"
username: "{{ hypervisor_cfg.username }}"
password: "{{ hypervisor_cfg.password }}"
validate_certs: "{{ hypervisor_cfg.certs | bool }}"
name: "{{ hostname }}"
folder: "{{ system_cfg.path | default('/') }}"
state: absent
no_log: true
- name: Destroy Xen VM if running - name: Remove Xen VM config file
when: ansible.builtin.file:
- hypervisor_type == "xen" path: "/tmp/xen-{{ hostname }}.cfg"
- virtualization_vm_created_in_run | default(false) | bool state: absent
delegate_to: localhost
become: false
ansible.builtin.command:
argv:
- xl
- destroy
- "{{ hostname }}"
register: cleanup_xen_destroy
failed_when: false
changed_when: cleanup_xen_destroy.rc == 0
- name: Remove Xen VM disk
when:
- hypervisor_type == "xen"
- virtualization_vm_created_in_run | default(false) | bool
delegate_to: localhost
become: false
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ virtualization_xen_disks | default([]) }}"
loop_control:
label: "{{ item.path }}"
- name: Remove Xen VM config file
when:
- hypervisor_type == "xen"
- virtualization_vm_created_in_run | default(false) | bool
delegate_to: localhost
become: false
ansible.builtin.file:
path: "/tmp/xen-{{ hostname }}.cfg"
state: absent
- name: VM cleanup completed - name: VM cleanup completed
ansible.builtin.debug: ansible.builtin.debug: