refactor ♻️: Refactored playbook to improve readability and maintainability

Simplified the playbook structure by removing unnecessary tasks and adding comments for clarity. Also, added a rescue block to handle potential errors during execution.
This commit is contained in:
2025-11-15 19:18:45 +01:00
parent c204a0a3b7
commit ec2b3b7edc

View File

@@ -8,88 +8,81 @@
# 5. Clone creation & deployment # 5. Clone creation & deployment
- name: "Create Debian VM template and deploy clones on Proxmox" - name: "Create Debian VM template and deploy clones on Proxmox"
hosts: localhost debug:
become: true msg: |
gather_facts: false ╔════════════════════════════════════════════════════════════╗
║ Proxmox VM Template & Clone Manager ║
║ Template VM: {{ hostname }} (ID: {{ vm_id }}) ║
║ Storage: {{ storage }} ║
║ CPU: {{ cores }} cores | RAM: {{ memory }}MB ║
╚════════════════════════════════════════════════════════════╝
pre_tasks: ##################################################################
- name: "Display playbook banner" # 1. PREFLIGHT CHECKS
debug: ##################################################################
msg: | - name: "STAGE 1: Run pre-flight environment checks"
╔════════════════════════════════════════════════════════════╗ include_tasks: preflight-checks.yml
║ Proxmox VM Template & Clone Manager ║ tags: [preflight, always]
║ Template VM: {{ hostname }} (ID: {{ vm_id }}) ║ run_once: true
║ Storage: {{ storage }} ║
║ CPU: {{ cores }} cores | RAM: {{ memory }}MB ║
╚════════════════════════════════════════════════════════════╝
tasks: ##################################################################
################################################################## # 2. DOWNLOAD IMAGE
# 1. PREFLIGHT CHECKS ##################################################################
################################################################## - name: "STAGE 2: Download and cache Debian GenericCloud image"
- name: "STAGE 1: Run pre-flight environment checks" include_tasks: download-image.yml
include_tasks: preflight-checks.yml tags: [image, always]
tags: [preflight, always]
################################################################## ##################################################################
# 2. DOWNLOAD IMAGE # 3. CREATE VM
################################################################## ##################################################################
- name: "STAGE 2: Download and cache Debian GenericCloud image" - name: "STAGE 3: Create base VM"
include_tasks: download-image.yml include_tasks: create-vm.yml
tags: [image, always] tags: [vm, create]
################################################################## ##################################################################
# 3. CREATE VM # 4. CONFIGURE VM (Disk, Cloud-Init, GPU, TPM, etc.)
################################################################## ##################################################################
- name: "STAGE 3: Create base VM" - name: "STAGE 4: Configure VM (disk, Cloud-Init, optional features)"
include_tasks: create-vm.yml include_tasks: configure-vm.yml
tags: [vm, create] tags: [vm, configure, cloudinit]
################################################################## ##################################################################
# 4. CONFIGURE VM (Disk, Cloud-Init, GPU, TPM, etc.) # 5. CREATE TEMPLATE
################################################################## ##################################################################
- name: "STAGE 4: Configure VM (disk, Cloud-Init, optional features)" - name: "STAGE 5: Convert VM to template"
include_tasks: configure-vm.yml include_tasks: create-template.yml
tags: [vm, configure, cloudinit] tags: [template, create]
when: make_template | default(false)
################################################################## ##################################################################
# 5. CREATE TEMPLATE # 6. CREATE CLONES
################################################################## ##################################################################
- name: "STAGE 5: Convert VM to template" - name: "STAGE 6: Create and configure clones"
include_tasks: create-template.yml include_tasks: create-clones.yml
tags: [template, create] tags: [clones, create]
when: make_template | default(false) when: create_clones | default(false)
################################################################## - name: "Display completion summary"
# 6. CREATE CLONES debug:
################################################################## msg: |
- name: "STAGE 6: Create and configure clones" ╔════════════════════════════════════════════════════════════╗
include_tasks: create-clones.yml ║ ✓ Playbook execution completed ║
tags: [clones, create] ║ ║
when: create_clones | default(false) ║ Template VM: {{ hostname }} (ID: {{ vm_id }}) ║
│ {% if make_template %}✓ Converted to template{% else %}✗ Template conversion disabled{% endif %}
│ {% if create_clones and clones %}✓ {{ clones | length }} clone(s) created{% else %}✗ Clone creation disabled{% endif %}
║ ║
║ Next steps: ║
║ - Verify VMs are running: qm list ║
║ - Connect to VM: ssh {{ ci_user }}@<vm-ip> ║
║ - Check Cloud-Init: cloud-init status ║
║ ║
╚════════════════════════════════════════════════════════════╝
post_tasks: # rescue:
- name: "Display completion summary" # - name: "Handle playbook errors"
debug: # debug:
msg: | # msg: |
╔════════════════════════════════════════════════════════════╗ # ✗ Playbook execution failed
║ ✓ Playbook execution completed ║ # Check the error messages above for details.
║ ║ # You may need to manually clean up partially created VMs.
║ Template VM: {{ hostname }} (ID: {{ vm_id }}) ║
│ {% if make_template %}✓ Converted to template{% else %}✗ Template conversion disabled{% endif %}
│ {% if create_clones and clones %}✓ {{ clones | length }} clone(s) created{% else %}✗ Clone creation disabled{% endif %}
║ ║
║ Next steps: ║
║ - Verify VMs are running: qm list ║
║ - Connect to VM: ssh {{ ci_user }}@<vm-ip> ║
║ - Check Cloud-Init: cloud-init status ║
║ ║
╚════════════════════════════════════════════════════════════╝
rescue:
- name: "Handle playbook errors"
debug:
msg: |
✗ Playbook execution failed
Check the error messages above for details.
You may need to manually clean up partially created VMs.