refactor(vars): add system/hypervisor dict inputs
This commit is contained in:
@@ -13,3 +13,196 @@
|
||||
|
||||
- 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
|
||||
ansible.builtin.set_fact:
|
||||
cleanup_post_reboot_can_connect: >-
|
||||
{{
|
||||
(
|
||||
post_reboot_can_connect
|
||||
if post_reboot_can_connect is defined
|
||||
else (
|
||||
(ansible_connection | default('ssh')) != 'ssh'
|
||||
or ((system_cfg.ip | default('') | string | length) > 0)
|
||||
or (
|
||||
install_type == 'physical'
|
||||
and (ansible_host | default('') | string | length) > 0
|
||||
)
|
||||
)
|
||||
) | bool
|
||||
}}
|
||||
changed_when: false
|
||||
|
||||
- name: Check VM accessibility after reboot
|
||||
when:
|
||||
- install_type == "virtual"
|
||||
- cleanup_post_reboot_can_connect | bool
|
||||
block:
|
||||
- name: Attempt to connect to VM
|
||||
delegate_to: "{{ inventory_hostname }}"
|
||||
ansible.builtin.wait_for_connection:
|
||||
timeout: 300
|
||||
register: cleanup_vm_connection_check
|
||||
failed_when: false
|
||||
changed_when: false
|
||||
|
||||
- name: VM failed to boot - initiate cleanup
|
||||
when:
|
||||
- cleanup_vm_connection_check is defined
|
||||
- cleanup_vm_connection_check.failed | bool
|
||||
block:
|
||||
- name: VM boot failure detected - removing VM
|
||||
ansible.builtin.debug:
|
||||
msg: |
|
||||
VM {{ hostname }} failed to boot after provisioning.
|
||||
This VM was created in the current playbook run and will be removed
|
||||
to prevent orphaned resources.
|
||||
|
||||
- name: Remove VM for libvirt
|
||||
when:
|
||||
- hypervisor == "libvirt"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
community.libvirt.virt:
|
||||
name: "{{ hostname }}"
|
||||
state: destroyed
|
||||
|
||||
- name: Undefine VM for libvirt
|
||||
when:
|
||||
- hypervisor == "libvirt"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
community.libvirt.virt:
|
||||
name: "{{ hostname }}"
|
||||
command: undefine
|
||||
|
||||
- name: Remove VM disk for libvirt
|
||||
when:
|
||||
- hypervisor == "libvirt"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
ansible.builtin.file:
|
||||
path: "{{ item.path }}"
|
||||
state: absent
|
||||
loop: "{{ virtualization_libvirt_disks | default([]) }}"
|
||||
loop_control:
|
||||
label: "{{ item.path }}"
|
||||
|
||||
- name: Remove cloud-init disk for libvirt
|
||||
when:
|
||||
- hypervisor == "libvirt"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
ansible.builtin.file:
|
||||
path: "{{ virtualization_libvirt_cloudinit_path }}"
|
||||
state: absent
|
||||
|
||||
- name: Remove VM for proxmox
|
||||
when:
|
||||
- hypervisor == "proxmox"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
community.proxmox.proxmox_kvm:
|
||||
api_host: "{{ hypervisor_url }}"
|
||||
api_user: "{{ hypervisor_username }}"
|
||||
api_password: "{{ hypervisor_password }}"
|
||||
node: "{{ hypervisor_node }}"
|
||||
name: "{{ hostname }}"
|
||||
vmid: "{{ system_cfg.id }}"
|
||||
state: stopped
|
||||
|
||||
- name: Delete VM for proxmox
|
||||
when:
|
||||
- hypervisor == "proxmox"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
community.proxmox.proxmox_kvm:
|
||||
api_host: "{{ hypervisor_url }}"
|
||||
api_user: "{{ hypervisor_username }}"
|
||||
api_password: "{{ hypervisor_password }}"
|
||||
node: "{{ hypervisor_node }}"
|
||||
name: "{{ hostname }}"
|
||||
vmid: "{{ system_cfg.id }}"
|
||||
state: absent
|
||||
unprivileged: false
|
||||
|
||||
- name: Remove VM for VMware
|
||||
when:
|
||||
- hypervisor == "vmware"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
community.vmware.vmware_guest:
|
||||
hostname: "{{ hypervisor_url }}"
|
||||
username: "{{ hypervisor_username }}"
|
||||
password: "{{ hypervisor_password }}"
|
||||
validate_certs: "{{ hypervisor_validate_certs }}"
|
||||
name: "{{ hostname }}"
|
||||
folder: "{{ system_cfg.path | default('/') }}"
|
||||
state: poweredoff
|
||||
|
||||
- name: Delete VM for VMware
|
||||
when:
|
||||
- hypervisor == "vmware"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
delegate_to: localhost
|
||||
become: false
|
||||
community.vmware.vmware_guest:
|
||||
hostname: "{{ hypervisor_url }}"
|
||||
username: "{{ hypervisor_username }}"
|
||||
password: "{{ hypervisor_password }}"
|
||||
validate_certs: "{{ hypervisor_validate_certs }}"
|
||||
name: "{{ hostname }}"
|
||||
folder: "{{ system_cfg.path | default('/') }}"
|
||||
state: absent
|
||||
|
||||
- name: Destroy Xen VM if running
|
||||
when:
|
||||
- hypervisor == "xen"
|
||||
- virtualization_vm_created_in_run | default(false) | bool
|
||||
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 == "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 == "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
|
||||
ansible.builtin.debug:
|
||||
msg: VM {{ hostname }} has been successfully removed due to boot failure.
|
||||
|
||||
Reference in New Issue
Block a user