- Added default configuration for VM creation in defaults/main.yml. - Created tasks for configuring the VM with UEFI, TPM, disks, GPU, and Cloud-Init in tasks/configure-vm.yml. - Implemented clone creation and configuration logic in tasks/create-clones.yml. - Added template conversion functionality in tasks/create-template.yml. - Developed base VM creation logic in tasks/create-vm.yml. - Included image download and caching tasks in tasks/download-image.yml. - Introduced utility tasks for common operations in tasks/helpers.yml. - Organized main orchestration logic in tasks/main.yml, with clear stages for each operation. - Added pre-flight checks to validate the environment before execution in tasks/preflight-checks.yml.
4.9 KiB
4.9 KiB
Quick Reference Guide
Key Improvements at a Glance
Error Handling
# All major operations now have try-catch blocks
block:
- name: "Try operation"
command: ...
rescue:
- name: "Handle error with context"
fail:
msg: "Clear error message"
Idempotency
# All operations check before acting
- stat: path="/path/to/resource"
register: resource
- command: "create resource"
when: not resource.stat.exists
Pre-flight Validation
ansible-playbook tasks/main.yml --tags preflight
# Validates: Proxmox, storage, SSH keys, IP addresses, permissions
Run Commands
| Command | Purpose |
|---|---|
ansible-playbook tasks/main.yml |
Full deployment |
ansible-playbook tasks/main.yml --tags preflight |
Validate only |
ansible-playbook tasks/main.yml --tags image,vm |
VM creation only |
ansible-playbook tasks/main.yml --tags clones |
Clone deployment only |
ansible-playbook tasks/main.yml --check |
Dry run (no changes) |
ansible-playbook tasks/main.yml -vvv |
Verbose debug output |
Task Stages
- STAGE 1:
preflight-checks.yml- Validate environment - STAGE 2:
download-image.yml- Cache Debian image - STAGE 3:
create-vm.yml- Create base VM - STAGE 4:
configure-vm.yml- Configure disk, networking, Cloud-Init - STAGE 5:
create-template.yml- Convert to template (idempotent) - STAGE 6:
create-clones.yml- Deploy clones
File Changes Summary
| File | Status | Key Changes |
|---|---|---|
tasks/main.yml |
Refactored | Now orchestrates subtasks |
tasks/preflight-checks.yml |
New | Environment validation |
tasks/download-image.yml |
Improved | Retry logic, validation |
tasks/create-vm.yml |
Improved | Error handling, idempotency |
tasks/configure-vm.yml |
Improved | Disk, Cloud-Init, TPM, GPU |
tasks/create-template.yml |
Improved | Idempotent template conversion |
tasks/create-clones.yml |
Improved | Per-clone error handling |
tasks/helpers.yml |
New | Utility functions |
defaults/main.yml |
Improved | Better docs, new options |
IMPROVEMENTS.md |
New | Complete guide |
Before vs After Examples
Idempotent Template Conversion
Before ❌
- name: Convert VM to template
command: qm template {{ vm_id }}
args:
creates: "/etc/pve/qemu-server/{{ vm_id }}.conf.lock"
# .lock doesn't exist → always runs → fails on re-run
After ✅
- name: "[TEMPLATE] Check if VM is already a template"
shell: "qm config {{ vm_id }} | grep -q 'template: 1'"
register: is_template
failed_when: false
- name: "[TEMPLATE] Convert VM to template"
command: "qm template {{ vm_id }}"
when: is_template.rc != 0
# Checks actual template status → safe to re-run
Error Handling
Before ❌
- name: Import disk
command: qm importdisk {{ vm_id }} {{ image_path }} {{ storage }}
# Fails with generic error, no recovery
After ✅
- name: "[CONFIG] Import qcow2 disk"
command: qm importdisk ...
register: disk_import
retries: 3 # Try 3 times
delay: 5 # Wait 5 seconds between tries
until: disk_import is succeeded
- rescue:
- name: "[CONFIG] Handle disk configuration error"
fail:
msg: "Failed to configure disk for VM {{ vm_id }}: ..."
# Clear context, automatic retries
Validation
Before ❌
# No checks, script fails mysteriously
After ✅
# Pre-flight checks:
[PREFLIGHT] Check if running on Proxmox host
[PREFLIGHT] Verify qm command is available
[PREFLIGHT] Check if user can run qm commands
[PREFLIGHT] Verify storage pool exists
[PREFLIGHT] Check SSH key file exists
[PREFLIGHT] Validate VM ID is unique
[PREFLIGHT] Validate clone IDs are unique
[PREFLIGHT] Validate IP address format
# All failing fast with context
Security Notes
-
Passwords: Use Ansible Vault for
ci_passwordansible-vault create group_vars/proxmox/vault.yml -
SSH Keys: Automatically validated before use
-
Permissions: Warns if user can't run
qmcommands
Performance Tips
- Use linked clones (
full: 0) for faster deployments - Tag-based execution to skip unnecessary stages
- Caching of Debian image to avoid re-downloads
- Parallel cloning (multiple --tags clones invocations)
Troubleshooting Commands
# Check Proxmox version
qm version
# List all VMs
qm list
# Check specific VM
qm config 150
# Check storage
pvesm status local-lvm
# Check Cloud-Init status (inside VM)
cloud-init status
cloud-init logs -f
Got Issues?
- Check
IMPROVEMENTS.mdfor detailed explanation - Run
--tags preflight -vvvto see exact validation errors - Check inline comments in each task file
- Review Proxmox logs:
journalctl -u pveproxy -f
Version: 2.0 (Improved with error handling & idempotency) Last Updated: 2025-11-15