Files
homeserver/ansible/roles/destroy-vms/tasks/main.yaml
Taqi Tahmid dc1cd79217 ansible: update destroy-vms playbook
- update destroy-vms playbook to correctly reflect VM state
2025-06-26 23:30:09 +03:00

73 lines
2.0 KiB
YAML

- name: Get VM current state
delegate_to: localhost
vars:
ansible_python_interpreter: "{{ ansible_venv }}"
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_url }}"
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token }}"
name: "{{ item.name }}"
node: "{{ node }}"
state: current
register: vm_state
ignore_errors: yes
loop: "{{ vm_list }}"
loop_control:
index_var: vm_index
tags:
- vm_delete
- name: Debug VM state
debug:
msg: "VM {{ item.name }} state: {{ vm_state.results[vm_index].status }}"
when: vm_state.results[vm_index] is defined and vm_state.results[vm_index] is succeeded
loop: "{{ vm_list }}"
loop_control:
index_var: vm_index
- name: Stop VM
delegate_to: localhost
vars:
ansible_python_interpreter: "{{ ansible_venv }}"
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_url }}"
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token }}"
name: "{{ item.name }}"
node: "{{ node }}"
state: stopped
force: true
when: >
vm_state.results[vm_index] is defined and
vm_state.results[vm_index] is succeeded and
vm_state.results[vm_index].status != 'absent'
loop: "{{ vm_list }}"
loop_control:
index_var: vm_index
tags:
- vm_delete
- name: Delete VM
delegate_to: localhost
vars:
ansible_python_interpreter: "{{ ansible_venv }}"
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_url }}"
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token }}"
name: "{{ item.name }}"
node: "{{ node }}"
state: absent
when: >
vm_state.results[vm_index] is defined and
vm_state.results[vm_index] is succeeded and
vm_state.results[vm_index].status != 'absent'
loop: "{{ vm_list }}"
loop_control:
index_var: vm_index
tags:
- vm_delete