From 50499903772926cd2f942950f5416c978be127f6 Mon Sep 17 00:00:00 2001 From: Jose Date: Sat, 6 Dec 2025 11:53:45 +0100 Subject: [PATCH] fix: refactor clone creation logic into separate iteration file --- tasks/clone_iteration.yml | 94 +++++++++++++++++++++++++++++++++++++ tasks/create-clones.yml | 98 ++------------------------------------- 2 files changed, 97 insertions(+), 95 deletions(-) create mode 100644 tasks/clone_iteration.yml diff --git a/tasks/clone_iteration.yml b/tasks/clone_iteration.yml new file mode 100644 index 0000000..9a9fd11 --- /dev/null +++ b/tasks/clone_iteration.yml @@ -0,0 +1,94 @@ +--- +# clone_iteration.yml - Single clone iteration (called from create-clones.yml loop) + +- name: "[CLONES] Check if clone already exists" + 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 vm_exists else 'WILL BE CREATED' }}" + +- name: "[CLONES] Clone VM from template" + block: + - name: "[CLONES] Execute clone command" + ansible.builtin.command: > + qm clone {{ vm_id }} {{ clone.id }} + --name {{ clone.hostname }} + --full {{ clone.full | default(0) }} + register: clone_cmd + when: not vm_exists + + - name: "[CLONES] Verify clone was created" + 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 vm_exists + + rescue: + - name: "[CLONES] Handle clone creation error" + ansible.builtin.fail: + msg: | + Failed to clone VM {{ vm_id }} to {{ clone.id }}: + {{ ansible_failed_result | default('Unknown error') }} + +- name: "[CLONES] Configure Cloud-Init for clone (if needed)" + block: + - name: "[CLONES] Set clone hostname and IP" + ansible.builtin.command: > + qm set {{ clone.id }} + --hostname {{ clone.hostname }} + --ipconfig0 "ip={{ clone.ip }},gw={{ clone.gateway }}" + register: clone_config + 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 vm_exists + + rescue: + - name: "[CLONES] Handle clone configuration error" + ansible.builtin.debug: + msg: "WARNING: Could not fully configure clone {{ clone.id }}. You may need to configure manually." + +- name: "[CLONES] Start clone VM" + ansible.builtin.command: "qm start {{ clone.id }}" + register: clone_start + retries: "{{ max_retries }}" + delay: "{{ retry_delay }}" + until: clone_start is succeeded + when: not vm_exists + +- name: "[CLONES] Wait for clone to boot" + ansible.builtin.pause: + seconds: 3 + +- 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 %} diff --git a/tasks/create-clones.yml b/tasks/create-clones.yml index 698a0ac..e7c2848 100644 --- a/tasks/create-clones.yml +++ b/tasks/create-clones.yml @@ -9,102 +9,10 @@ - clones is not defined or clones | length == 0 - name: "[CLONES] Process each clone" - block: - - name: "[CLONES] Check if clone already exists" - 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 vm_exists else 'WILL BE CREATED' }}" - - - name: "[CLONES] Clone VM from template" - block: - - name: "[CLONES] Execute clone command" - ansible.builtin.command: > - qm clone {{ vm_id }} {{ clone.id }} - --name {{ clone.hostname }} - --full {{ clone.full | default(0) }} - register: clone_cmd - when: not vm_exists - - - name: "[CLONES] Verify clone was created" - 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 vm_exists - - rescue: - - name: "[CLONES] Handle clone creation error" - ansible.builtin.fail: - msg: | - Failed to clone VM {{ vm_id }} to {{ clone.id }}: - {{ ansible_failed_result | default('Unknown error') }} - - - name: "[CLONES] Configure Cloud-Init for clone (if needed)" - block: - - name: "[CLONES] Set clone hostname and IP" - ansible.builtin.command: > - qm set {{ clone.id }} - --hostname {{ clone.hostname }} - --ipconfig0 "ip={{ clone.ip }},gw={{ clone.gateway }}" - register: clone_config - 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 vm_exists - - rescue: - - name: "[CLONES] Handle clone configuration error" - ansible.builtin.debug: - msg: "WARNING: Could not fully configure clone {{ clone.id }}. You may need to configure manually." - - - name: "[CLONES] Start clone VM" - ansible.builtin.command: "qm start {{ clone.id }}" - register: clone_start - retries: "{{ max_retries }}" - delay: "{{ retry_delay }}" - until: clone_start is succeeded - when: not vm_exists - - - name: "[CLONES] Wait for clone to boot" - ansible.builtin.pause: - seconds: 3 - - - 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 %} - + include_tasks: clone_iteration.yml + vars: + clone: "{{ item }}" loop: "{{ clones }}" - loop_control: - loop_var: clone when: create_clones | default(false) - name: "[CLONES] Skip clone creation (disabled)"