From 7f6d7fb418d5f3e20069d3a2582be2c7533268fe Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 3 Dec 2025 17:43:55 +0100 Subject: [PATCH] Refactor VM ID existence check in preflight tasks to use JSON output format --- tasks/preflight-checks.yml | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tasks/preflight-checks.yml b/tasks/preflight-checks.yml index 77cacd9..0fa5d5d 100644 --- a/tasks/preflight-checks.yml +++ b/tasks/preflight-checks.yml @@ -77,17 +77,27 @@ 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" +- name: "[PREFLIGHT] Query VM list from cluster" + ansible.builtin.command: > + pvesh get /cluster/resources + --type vm + --output-format json register: cluster_vms changed_when: false + failed_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] Determine if VM ID exists" + 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: +- name: "[PREFLIGHT] Fail if VM ID exists" + fail: msg: "VM ID {{ vm_id }} already exists in the Proxmox cluster!" when: vm_id_exists