From c61b39df605ccdf7acbb6e2ba32ffef6b106acde Mon Sep 17 00:00:00 2001 From: omnom62 Date: Tue, 8 Sep 2026 05:38:12 +1000 Subject: hyper_vm destroy play --- roles/hyperv_vm/tasks/main.yml | 43 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/roles/hyperv_vm/tasks/main.yml b/roles/hyperv_vm/tasks/main.yml index 8089cca..49d0c71 100644 --- a/roles/hyperv_vm/tasks/main.yml +++ b/roles/hyperv_vm/tasks/main.yml @@ -3,13 +3,54 @@ block: - name: Report VM pending decommission (no action taken) - when: status.value == 'decommissioning' + when: status.value == 'decommissioning' and not (confirm_destroy | default(false) | bool) ansible.builtin.debug: msg: >- {{ netbox_name }} is marked 'decommissioning' in Netbox. No action taken — use the separate 'Destroy Appliance VM' job to remove it, with an explicit target and confirmation. + - name: Destroy if decommission is explicitly confirmed + when: status.value == 'decommissioning' and confirm_destroy | default(false) | bool + block: + + - name: Set VM identity and path variables + ansible.builtin.set_fact: + vm_name: "{{ netbox_name }}" + vm_vhdx_path: "{{ hyperv_vm_base_dir }}\\{{ netbox_name }}\\os.vhdx" + + - name: Stop the VM + delegate_to: "{{ hyperv_target }}" + delegate_facts: true + microsoft.hyperv.hv_vm_state: + name: "{{ vm_name }}" + state: stopped + ignore_errors: true + + - name: Remove the VM + delegate_to: "{{ hyperv_target }}" + delegate_facts: true + microsoft.hyperv.hv_vm: + name: "{{ vm_name }}" + state: absent + + - name: Remove the differencing disk file + delegate_to: "{{ hyperv_target }}" + delegate_facts: true + ansible.windows.win_file: + path: "{{ vm_vhdx_path }}" + state: absent + + - name: Remove the VM record from Netbox + delegate_to: localhost + netbox.netbox.netbox_virtual_machine: + netbox_url: "{{ lookup('env', 'NETBOX_API') }}" + netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}" + validate_certs: false + data: + name: "{{ vm_name }}" + state: absent + - name: Skip hosts that are neither staged nor pending decommission when: status.value not in ['staged', 'decommissioning'] ansible.builtin.debug: -- cgit v1.2.3