refactor ♻️: Integrate centralized helper functions for improved code consistency and idempotency across VM management tasks
This commit is contained in:
@@ -2,45 +2,49 @@
|
||||
# create-vm.yml - Create base VM on Proxmox
|
||||
|
||||
- name: "[VM] Check if VM already exists"
|
||||
ansible.builtin.stat:
|
||||
path: "/etc/pve/qemu-server/{{ vm_id }}.conf"
|
||||
register: vm_conf
|
||||
changed_when: false
|
||||
ansible.builtin.include_tasks: helpers.yml
|
||||
vars:
|
||||
helper_task: check_vm_exists
|
||||
target_vm_id: "{{ vm_id }}"
|
||||
|
||||
- name: "[VM] Display VM status"
|
||||
ansible.builtin.debug:
|
||||
msg: "VM {{ vm_id }} ({{ hostname }}) - Status: {{ 'ALREADY EXISTS' if vm_conf.stat.exists else 'WILL BE CREATED' }}"
|
||||
msg: "VM {{ vm_id }} ({{ hostname }}) - Status: {{ 'ALREADY EXISTS' if vm_exists else 'WILL BE CREATED' }}"
|
||||
|
||||
- name: "[VM] Create base VM"
|
||||
ansible.builtin.command: >
|
||||
qm create {{ vm_id }}
|
||||
--name {{ hostname }}
|
||||
--memory {{ memory }}
|
||||
--cores {{ cores }}
|
||||
--cpu {{ cpu_type }}
|
||||
--net0 virtio,bridge={{ bridge }},macaddr={{ mac_address }}
|
||||
--agent 1
|
||||
register: vm_create
|
||||
when: not vm_conf.stat.exists
|
||||
changed_when: vm_create.rc == 0
|
||||
- name: "[VM] Create base VM (idempotent - skipped if already exists)"
|
||||
block:
|
||||
- name: "[VM] Create base VM"
|
||||
ansible.builtin.command: >
|
||||
qm create {{ vm_id }}
|
||||
--name {{ hostname }}
|
||||
--memory {{ memory }}
|
||||
--cores {{ cores }}
|
||||
--cpu {{ cpu_type }}
|
||||
--net0 virtio,bridge={{ bridge }},macaddr={{ mac_address }}
|
||||
--agent 1
|
||||
register: vm_create
|
||||
changed_when: vm_create.rc == 0
|
||||
|
||||
- name: "[VM] Handle VM creation error"
|
||||
ansible.builtin.fail:
|
||||
msg: |
|
||||
Failed to create VM {{ vm_id }}:
|
||||
{{ vm_create.stderr | default('No error message') }}
|
||||
when:
|
||||
- not vm_conf.stat.exists
|
||||
- vm_create is failed
|
||||
- name: "[VM] Verify VM was created"
|
||||
ansible.builtin.include_tasks: helpers.yml
|
||||
vars:
|
||||
helper_task: check_vm_exists
|
||||
target_vm_id: "{{ vm_id }}"
|
||||
|
||||
- name: "[VM] Verify VM was created"
|
||||
ansible.builtin.stat:
|
||||
path: "/etc/pve/qemu-server/{{ vm_id }}.conf"
|
||||
register: vm_conf_verify
|
||||
changed_when: false
|
||||
failed_when: not vm_conf_verify.stat.exists
|
||||
- name: "[VM] Ensure VM creation succeeded"
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- vm_exists | bool
|
||||
fail_msg: "Failed to create VM {{ vm_id }}"
|
||||
|
||||
- name: "[VM] Display VM creation result"
|
||||
ansible.builtin.debug:
|
||||
msg: "✓ VM {{ vm_id }} created successfully"
|
||||
when: not vm_conf.stat.exists
|
||||
- name: "[VM] Display VM creation result"
|
||||
ansible.builtin.debug:
|
||||
msg: "✓ VM {{ vm_id }} created successfully"
|
||||
|
||||
when: not vm_exists
|
||||
rescue:
|
||||
- name: "[VM] Handle VM creation error"
|
||||
ansible.builtin.fail:
|
||||
msg: |
|
||||
Failed to create VM {{ vm_id }}:
|
||||
{{ ansible_failed_result | default('Unknown error') }}
|
||||
|
||||
Reference in New Issue
Block a user