Refactor VM ID existence check in preflight tasks to use JSON output format
This commit is contained in:
@@ -77,17 +77,27 @@
|
|||||||
failed_when: false
|
failed_when: false
|
||||||
register: vm_id_check
|
register: vm_id_check
|
||||||
|
|
||||||
- name: "[PREFLIGHT] Check if VM ID exists in Proxmox cluster"
|
- name: "[PREFLIGHT] Query VM list from cluster"
|
||||||
ansible.builtin.command: "pvesh get /cluster/resources --type vm"
|
ansible.builtin.command: >
|
||||||
|
pvesh get /cluster/resources
|
||||||
|
--type vm
|
||||||
|
--output-format json
|
||||||
register: cluster_vms
|
register: cluster_vms
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
failed_when: false
|
||||||
|
|
||||||
- name: "[PREFLIGHT] Determine if VM ID is already used"
|
- name: "[PREFLIGHT] Determine if VM ID exists"
|
||||||
ansible.builtin.set_fact:
|
set_fact:
|
||||||
vm_id_exists: "{{ cluster_vms.stdout | from_json | selectattr('vmid', 'equalto', vm_id) | list | length > 0 }}"
|
vm_id_exists: >-
|
||||||
|
{{
|
||||||
|
(cluster_vms.stdout | from_json)
|
||||||
|
| selectattr('vmid', 'equalto', vm_id)
|
||||||
|
| list
|
||||||
|
| length > 0
|
||||||
|
}}
|
||||||
|
|
||||||
- name: "[PREFLIGHT] Fail if VM ID already exists"
|
- name: "[PREFLIGHT] Fail if VM ID exists"
|
||||||
ansible.builtin.fail:
|
fail:
|
||||||
msg: "VM ID {{ vm_id }} already exists in the Proxmox cluster!"
|
msg: "VM ID {{ vm_id }} already exists in the Proxmox cluster!"
|
||||||
when: vm_id_exists
|
when: vm_id_exists
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user