diff --git a/tasks/preflight-checks.yml b/tasks/preflight-checks.yml index aae429b..77cacd9 100644 --- a/tasks/preflight-checks.yml +++ b/tasks/preflight-checks.yml @@ -15,8 +15,6 @@ run_once: true become: false - - - name: "[PREFLIGHT] Check if running on Proxmox host" ansible.builtin.stat: path: "/etc/pve/nodes" @@ -72,12 +70,6 @@ failed_when: not ssh_key_file.stat.exists changed_when: false -# - name: "[PREFLIGHT] Check SSH key file exists" -# ansible.builtin.stat: -# path: "{{ ssh_key_path | expanduser }}" -# register: ssh_key_file -# failed_when: not ssh_key_file.stat.exists -# changed_when: false - name: "[PREFLIGHT] Validate VM ID is unique" ansible.builtin.command: "test ! -f /etc/pve/qemu-server/{{ vm_id }}.conf" @@ -85,6 +77,20 @@ failed_when: false register: vm_id_check +- name: "[PREFLIGHT] Check if VM ID exists in Proxmox cluster" + ansible.builtin.command: "pvesh get /cluster/resources --type vm" + register: cluster_vms + changed_when: false + +- name: "[PREFLIGHT] Determine if VM ID is already used" + ansible.builtin.set_fact: + vm_id_exists: "{{ cluster_vms.stdout | from_json | selectattr('vmid', 'equalto', vm_id) | list | length > 0 }}" + +- name: "[PREFLIGHT] Fail if VM ID already exists" + ansible.builtin.fail: + msg: "VM ID {{ vm_id }} already exists in the Proxmox cluster!" + when: vm_id_exists + - name: "[PREFLIGHT] Warn if VM ID already exists" ansible.builtin.debug: msg: "WARNING: VM ID {{ vm_id }} already exists. It will be skipped or updated."