2025-11-15 17:22:21 +01:00
|
|
|
|
---
|
|
|
|
|
|
# create-clones.yml - Create and configure clones from template with error handling
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Validate clone list is not empty"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.fail:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
msg: "No clones defined in 'clones' variable"
|
|
|
|
|
|
when:
|
|
|
|
|
|
- create_clones | default(false)
|
|
|
|
|
|
- clones is not defined or clones | length == 0
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Process each clone"
|
|
|
|
|
|
block:
|
|
|
|
|
|
- name: "[CLONES] Check if clone already exists"
|
2025-11-16 08:42:26 +01:00
|
|
|
|
ansible.builtin.include_tasks: helpers.yml
|
|
|
|
|
|
vars:
|
|
|
|
|
|
helper_task: check_vm_exists
|
|
|
|
|
|
target_vm_id: "{{ clone.id }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Display clone status"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.debug:
|
2025-11-16 08:42:26 +01:00
|
|
|
|
msg: "Clone {{ clone.id }} ({{ clone.hostname }}) - Status: {{ 'EXISTS' if vm_exists else 'WILL BE CREATED' }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Clone VM from template"
|
|
|
|
|
|
block:
|
|
|
|
|
|
- name: "[CLONES] Execute clone command"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.command: >
|
2025-11-15 17:22:21 +01:00
|
|
|
|
qm clone {{ vm_id }} {{ clone.id }}
|
|
|
|
|
|
--name {{ clone.hostname }}
|
|
|
|
|
|
--full {{ clone.full | default(0) }}
|
|
|
|
|
|
register: clone_cmd
|
2025-11-16 08:42:26 +01:00
|
|
|
|
when: not vm_exists
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Verify clone was created"
|
2025-11-16 08:42:26 +01:00
|
|
|
|
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
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Wait for clone to be ready"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.pause:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
seconds: 2
|
2025-11-16 08:42:26 +01:00
|
|
|
|
when: not vm_exists
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
rescue:
|
|
|
|
|
|
- name: "[CLONES] Handle clone creation error"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.fail:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
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"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.command: >
|
2025-11-15 17:22:21 +01:00
|
|
|
|
qm set {{ clone.id }}
|
|
|
|
|
|
--hostname {{ clone.hostname }}
|
|
|
|
|
|
--ipconfig0 "ip={{ clone.ip }},gw={{ clone.gateway }}"
|
|
|
|
|
|
register: clone_config
|
2025-11-16 08:42:26 +01:00
|
|
|
|
when: not vm_exists
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Apply SSH keys to clone"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.command: >
|
2025-11-15 17:22:21 +01:00
|
|
|
|
qm set {{ clone.id }}
|
|
|
|
|
|
--sshkeys local:snippets/{{ vm_id }}-sshkey.pub
|
2025-11-16 08:42:26 +01:00
|
|
|
|
when: not vm_exists
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
rescue:
|
|
|
|
|
|
- name: "[CLONES] Handle clone configuration error"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.debug:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
msg: "WARNING: Could not fully configure clone {{ clone.id }}. You may need to configure manually."
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Start clone VM"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.command: "qm start {{ clone.id }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
register: clone_start
|
2025-11-16 08:02:18 +01:00
|
|
|
|
retries: "{{ max_retries }}"
|
|
|
|
|
|
delay: "{{ retry_delay }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
until: clone_start is succeeded
|
2025-11-16 08:42:26 +01:00
|
|
|
|
when: not vm_exists
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Wait for clone to boot"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.pause:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
seconds: 3
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Display clone creation result"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.debug:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
msg: |
|
2025-11-16 08:42:26 +01:00
|
|
|
|
{% if vm_exists %}
|
|
|
|
|
|
ℹ Clone {{ clone.id }} ({{ clone.hostname }}) already exists - skipped
|
|
|
|
|
|
{% else %}
|
2025-11-15 17:22:21 +01:00
|
|
|
|
✓ Clone created and started
|
|
|
|
|
|
- ID: {{ clone.id }}
|
|
|
|
|
|
- Hostname: {{ clone.hostname }}
|
|
|
|
|
|
- IP: {{ clone.ip }}
|
|
|
|
|
|
- Full clone: {{ clone.full | default(0) }}
|
2025-11-16 08:42:26 +01:00
|
|
|
|
{% endif %}
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
loop: "{{ clones }}"
|
|
|
|
|
|
loop_control:
|
|
|
|
|
|
loop_var: clone
|
|
|
|
|
|
when: create_clones | default(false)
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[CLONES] Skip clone creation (disabled)"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.debug:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
msg: "ℹ Clone creation is disabled. Set 'create_clones: true' to enable."
|
|
|
|
|
|
when: not (create_clones | default(false))
|