2025-11-15 17:22:21 +01:00
|
|
|
|
---
|
|
|
|
|
|
# create-template.yml - Convert VM to template with proper idempotency
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Check if VM is already a template"
|
2025-11-16 08:42:26 +01:00
|
|
|
|
ansible.builtin.include_tasks: helpers.yml
|
|
|
|
|
|
vars:
|
|
|
|
|
|
helper_task: check_template
|
|
|
|
|
|
target_vm_id: "{{ vm_id }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Display template status"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.debug:
|
2025-11-16 08:42:26 +01:00
|
|
|
|
msg: "Template status for VM {{ vm_id }}: {{ 'ALREADY A TEMPLATE' if is_template else 'NOT YET A TEMPLATE' }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Verify VM is stopped before converting"
|
|
|
|
|
|
block:
|
|
|
|
|
|
- name: "[TEMPLATE] Check VM status"
|
2025-11-16 08:42:26 +01:00
|
|
|
|
ansible.builtin.include_tasks: helpers.yml
|
|
|
|
|
|
vars:
|
|
|
|
|
|
helper_task: check_vm_status
|
|
|
|
|
|
target_vm_id: "{{ vm_id }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Stop VM if running"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.command: "qm stop {{ vm_id }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
register: vm_stop
|
2025-11-16 08:42:26 +01:00
|
|
|
|
when: vm_status != 'stopped'
|
|
|
|
|
|
changed_when: vm_stop.rc == 0
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Wait for VM to stop"
|
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: vm_status != 'stopped'
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
rescue:
|
|
|
|
|
|
- name: "[TEMPLATE] Handle VM stop error"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.debug:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
msg: "WARNING: Could not verify/stop VM {{ vm_id }}. Continuing..."
|
|
|
|
|
|
|
2025-11-16 08:42:26 +01:00
|
|
|
|
- name: "[TEMPLATE] Convert VM to template (idempotent - skipped if already template)"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
block:
|
|
|
|
|
|
- name: "[TEMPLATE] Convert to template"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.command: "qm template {{ vm_id }}"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
register: template_convert
|
2025-11-16 08:42:26 +01:00
|
|
|
|
when: not is_template
|
2025-11-15 17:22:21 +01:00
|
|
|
|
changed_when: template_convert.rc == 0
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Verify conversion"
|
2025-11-16 08:42:26 +01:00
|
|
|
|
ansible.builtin.include_tasks: helpers.yml
|
|
|
|
|
|
vars:
|
|
|
|
|
|
helper_task: check_template
|
|
|
|
|
|
target_vm_id: "{{ vm_id }}"
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Ensure template conversion succeeded"
|
|
|
|
|
|
ansible.builtin.assert:
|
|
|
|
|
|
that:
|
|
|
|
|
|
- is_template | bool
|
|
|
|
|
|
fail_msg: "Failed to convert VM {{ vm_id }} to template"
|
2025-11-15 17:22:21 +01:00
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Display template conversion result"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.debug:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
msg: |
|
|
|
|
|
|
✓ VM {{ vm_id }} ({{ hostname }}) successfully converted to template
|
|
|
|
|
|
Template can now be cloned
|
|
|
|
|
|
|
|
|
|
|
|
rescue:
|
|
|
|
|
|
- name: "[TEMPLATE] Handle template conversion error"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.fail:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
msg: |
|
|
|
|
|
|
Failed to convert VM {{ vm_id }} to template:
|
|
|
|
|
|
{{ ansible_failed_result | default('Unknown error') }}
|
|
|
|
|
|
|
|
|
|
|
|
- name: "[TEMPLATE] Skip template conversion (already done)"
|
2025-11-15 21:49:12 +01:00
|
|
|
|
ansible.builtin.debug:
|
2025-11-15 17:22:21 +01:00
|
|
|
|
msg: "ℹ VM {{ vm_id }} is already a template, skipping conversion"
|
2025-11-16 08:42:26 +01:00
|
|
|
|
when: is_template
|