Files
ansible/remove_vm.yml
2024-09-12 09:27:25 +00:00

51 lines
1.5 KiB
YAML

---
- name: Remove VM on Proxmox
hosts: proxmox.localdomain
gather_facts: false
vars:
api_host: 192.168.1.162
api_user: root@pam
api_token_id: mytoki
api_token_secret: ea5114f5-ed39-4277-ac09-a32984be5bc9
proxmox_node: pve
vars_prompt:
- name: vm_name_to_remove
prompt: Enter the name of the VM to remove
private: false
tasks:
- name: List all VMs and templates
community.general.proxmox_vm_info:
api_host: "{{ api_host }}"
api_user: "{{ api_user }}"
api_token_id: "{{ api_token_id }}"
api_token_secret: "{{ api_token_secret }}"
validate_certs: false
register: vms_info
- name: Find VMID based on VM name
loop: "{{ vms_info.proxmox_vms }}"
when: item.name == vm_name_to_remove
ansible.builtin.set_fact:
vmid_to_remove: "{{ item.vmid }}"
- name: Debug VMID to remove
when: vmid_to_remove is defined
ansible.builtin.debug:
msg: "VMID to remove: {{ vmid_to_remove }}"
- name: Remove the VM
community.general.proxmox_kvm:
api_user: "{{ api_user }}"
api_token_id: "{{ api_token_id }}"
api_token_secret: "{{ api_token_secret }}"
api_host: "{{ api_host }}"
vmid: "{{ vmid_to_remove }}"
node: "{{ proxmox_node }}"
state: absent
when: vmid_to_remove is defined
- name: Print message if VM was not found
when: vmid_to_remove is not defined
ansible.builtin.debug:
msg: VM {{ vm_name_to_remove }} not found.