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