refactor ♻️: Integrate centralized helper functions for improved code consistency and idempotency across VM management tasks
This commit is contained in:
@@ -11,14 +11,14 @@
|
||||
- name: "[CLONES] Process each clone"
|
||||
block:
|
||||
- name: "[CLONES] Check if clone already exists"
|
||||
ansible.builtin.stat:
|
||||
path: "/etc/pve/qemu-server/{{ clone.id }}.conf"
|
||||
register: clone_conf
|
||||
changed_when: false
|
||||
ansible.builtin.include_tasks: helpers.yml
|
||||
vars:
|
||||
helper_task: check_vm_exists
|
||||
target_vm_id: "{{ clone.id }}"
|
||||
|
||||
- name: "[CLONES] Display clone status"
|
||||
ansible.builtin.debug:
|
||||
msg: "Clone {{ clone.id }} ({{ clone.hostname }}) - Status: {{ 'EXISTS' if clone_conf.stat.exists else 'WILL BE CREATED' }}"
|
||||
msg: "Clone {{ clone.id }} ({{ clone.hostname }}) - Status: {{ 'EXISTS' if vm_exists else 'WILL BE CREATED' }}"
|
||||
|
||||
- name: "[CLONES] Clone VM from template"
|
||||
block:
|
||||
@@ -28,19 +28,26 @@
|
||||
--name {{ clone.hostname }}
|
||||
--full {{ clone.full | default(0) }}
|
||||
register: clone_cmd
|
||||
when: not clone_conf.stat.exists
|
||||
when: not vm_exists
|
||||
|
||||
- name: "[CLONES] Verify clone was created"
|
||||
ansible.builtin.stat:
|
||||
path: "/etc/pve/qemu-server/{{ clone.id }}.conf"
|
||||
register: clone_verify
|
||||
changed_when: false
|
||||
failed_when: not clone_verify.stat.exists
|
||||
ansible.builtin.include_tasks: helpers.yml
|
||||
vars:
|
||||
helper_task: check_vm_exists
|
||||
target_vm_id: "{{ clone.id }}"
|
||||
when: not vm_exists
|
||||
|
||||
- name: "[CLONES] Ensure clone creation succeeded"
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- vm_exists | bool
|
||||
fail_msg: "Failed to create clone {{ clone.id }}"
|
||||
when: not vm_exists
|
||||
|
||||
- name: "[CLONES] Wait for clone to be ready"
|
||||
ansible.builtin.pause:
|
||||
seconds: 2
|
||||
when: not clone_conf.stat.exists
|
||||
when: not vm_exists
|
||||
|
||||
rescue:
|
||||
- name: "[CLONES] Handle clone creation error"
|
||||
@@ -57,13 +64,13 @@
|
||||
--hostname {{ clone.hostname }}
|
||||
--ipconfig0 "ip={{ clone.ip }},gw={{ clone.gateway }}"
|
||||
register: clone_config
|
||||
when: not clone_conf.stat.exists
|
||||
when: not vm_exists
|
||||
|
||||
- name: "[CLONES] Apply SSH keys to clone"
|
||||
ansible.builtin.command: >
|
||||
qm set {{ clone.id }}
|
||||
--sshkeys local:snippets/{{ vm_id }}-sshkey.pub
|
||||
when: not clone_conf.stat.exists
|
||||
when: not vm_exists
|
||||
|
||||
rescue:
|
||||
- name: "[CLONES] Handle clone configuration error"
|
||||
@@ -76,7 +83,7 @@
|
||||
retries: "{{ max_retries }}"
|
||||
delay: "{{ retry_delay }}"
|
||||
until: clone_start is succeeded
|
||||
when: not clone_conf.stat.exists
|
||||
when: not vm_exists
|
||||
|
||||
- name: "[CLONES] Wait for clone to boot"
|
||||
ansible.builtin.pause:
|
||||
@@ -85,11 +92,15 @@
|
||||
- name: "[CLONES] Display clone creation result"
|
||||
ansible.builtin.debug:
|
||||
msg: |
|
||||
{% if vm_exists %}
|
||||
ℹ Clone {{ clone.id }} ({{ clone.hostname }}) already exists - skipped
|
||||
{% else %}
|
||||
✓ Clone created and started
|
||||
- ID: {{ clone.id }}
|
||||
- Hostname: {{ clone.hostname }}
|
||||
- IP: {{ clone.ip }}
|
||||
- Full clone: {{ clone.full | default(0) }}
|
||||
{% endif %}
|
||||
|
||||
loop: "{{ clones }}"
|
||||
loop_control:
|
||||
|
||||
Reference in New Issue
Block a user