feat: Implement Debian VM template creation and cloning on Proxmox
- 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.
This commit is contained in:
212
tasks/main.yml
212
tasks/main.yml
@@ -1,167 +1,95 @@
|
||||
---
|
||||
- name: "Create a Debian VM template and optionally deploy clones"
|
||||
# main.yml - Orchestrate Debian VM template creation and cloning on Proxmox
|
||||
# This playbook handles:
|
||||
# 1. Pre-flight checks (environment validation)
|
||||
# 2. Image download & caching
|
||||
# 3. VM creation & configuration
|
||||
# 4. Template conversion
|
||||
# 5. Clone creation & deployment
|
||||
|
||||
- name: "Create Debian VM template and deploy clones on Proxmox"
|
||||
hosts: localhost
|
||||
become: true
|
||||
gather_facts: false
|
||||
|
||||
pre_tasks:
|
||||
- name: "Display playbook banner"
|
||||
debug:
|
||||
msg: |
|
||||
╔════════════════════════════════════════════════════════════╗
|
||||
║ Proxmox VM Template & Clone Manager ║
|
||||
║ Template VM: {{ hostname }} (ID: {{ vm_id }}) ║
|
||||
║ Storage: {{ storage }} ║
|
||||
║ CPU: {{ cores }} cores | RAM: {{ memory }}MB ║
|
||||
╚════════════════════════════════════════════════════════════╝
|
||||
|
||||
tasks:
|
||||
##################################################################
|
||||
# 1. PREFLIGHT CHECKS
|
||||
##################################################################
|
||||
- name: "STAGE 1: Run pre-flight environment checks"
|
||||
include_tasks: preflight-checks.yml
|
||||
tags: [preflight, always]
|
||||
|
||||
##################################################################
|
||||
# 1. Ensure Debian GenericCloud Image Exists
|
||||
# 2. DOWNLOAD IMAGE
|
||||
##################################################################
|
||||
- name: Check for Debian image
|
||||
stat:
|
||||
path: "/var/lib/vz/template/qemu/debian-genericcloud-amd64.qcow2"
|
||||
register: debian_img
|
||||
|
||||
- name: Download GenericCloud qcow2
|
||||
get_url:
|
||||
url: "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2"
|
||||
dest: "/var/lib/vz/template/qemu/debian-genericcloud-amd64.qcow2"
|
||||
mode: "0644"
|
||||
when: not debian_img.stat.exists
|
||||
- name: "STAGE 2: Download and cache Debian GenericCloud image"
|
||||
include_tasks: download-image.yml
|
||||
tags: [image, always]
|
||||
|
||||
##################################################################
|
||||
# 2. Create Base VM (if not exists)
|
||||
# 3. CREATE VM
|
||||
##################################################################
|
||||
- name: Check if VM exists
|
||||
stat:
|
||||
path: "/etc/pve/qemu-server/{{ vm_id }}.conf"
|
||||
register: vm_conf
|
||||
|
||||
- name: Create VM
|
||||
command: >
|
||||
qm create {{ vm_id }}
|
||||
--name {{ hostname }}
|
||||
--memory {{ memory }}
|
||||
--cores {{ cores }}
|
||||
--cpu {{ cpu_type }}
|
||||
--net0 virtio,bridge={{ bridge }},macaddr={{ mac_address }}
|
||||
--agent 1
|
||||
when: not vm_conf.stat.exists
|
||||
- name: "STAGE 3: Create base VM"
|
||||
include_tasks: create-vm.yml
|
||||
tags: [vm, create]
|
||||
|
||||
##################################################################
|
||||
# 3. Optional UEFI + Secure Boot + TPM
|
||||
# 4. CONFIGURE VM (Disk, Cloud-Init, GPU, TPM, etc.)
|
||||
##################################################################
|
||||
- name: Enable UEFI + TPM
|
||||
command: >
|
||||
qm set {{ vm_id }}
|
||||
--bios ovmf
|
||||
--efidisk0 {{ storage }}:0,pre-enrolled-keys=1
|
||||
--tpmstate0 {{ storage }}:1,size=4M,version=v2.0
|
||||
when: enable_tpm | default(false)
|
||||
- name: "STAGE 4: Configure VM (disk, Cloud-Init, optional features)"
|
||||
include_tasks: configure-vm.yml
|
||||
tags: [vm, configure, cloudinit]
|
||||
|
||||
##################################################################
|
||||
# 4. Disk Import & Attach
|
||||
# 5. CREATE TEMPLATE
|
||||
##################################################################
|
||||
- name: Check if disk already exists
|
||||
stat:
|
||||
path: "/var/lib/vz/images/{{ vm_id }}/vm-{{ vm_id }}-disk-0.qcow2"
|
||||
register: disk_exists
|
||||
|
||||
- name: Import qcow2 disk
|
||||
command: >
|
||||
qm importdisk {{ vm_id }}
|
||||
/var/lib/vz/template/qemu/debian-genericcloud-amd64.qcow2
|
||||
{{ storage }}
|
||||
when: not disk_exists.stat.exists
|
||||
|
||||
- name: Attach imported disk
|
||||
command: >
|
||||
qm set {{ vm_id }}
|
||||
--scsihw virtio-scsi-pci
|
||||
--scsi0 {{ storage }}:vm-{{ vm_id }}-disk-0
|
||||
when: not disk_exists.stat.exists
|
||||
|
||||
- name: Enable serial console + boot disk
|
||||
command: >
|
||||
qm set {{ vm_id }}
|
||||
--serial0 socket
|
||||
--boot order=scsi0
|
||||
|
||||
##################################################################
|
||||
# 5. Optional Disk Resize
|
||||
##################################################################
|
||||
- name: Resize disk
|
||||
command: qm resize {{ vm_id }} scsi0 {{ resize_size }}
|
||||
when: resize_disk | default(false)
|
||||
|
||||
##################################################################
|
||||
# 6. Optional GPU
|
||||
##################################################################
|
||||
- name: PCI GPU passthrough
|
||||
command: qm set {{ vm_id }} --hostpci0 {{ gpu_device }}
|
||||
when: gpu_passthrough | default(false)
|
||||
|
||||
- name: VirtIO GPU
|
||||
command: qm set {{ vm_id }} --vga virtio
|
||||
when: virtio_gpu | default(false)
|
||||
|
||||
##################################################################
|
||||
# 7. Cloud-Init Snippets
|
||||
##################################################################
|
||||
- name: Create Cloud-Init vendor-data
|
||||
template:
|
||||
src: cloudinit_vendor.yaml.j2
|
||||
dest: "/var/lib/vz/snippets/{{ vm_id }}-vendor.yaml"
|
||||
|
||||
- name: Create Cloud-Init user-data
|
||||
template:
|
||||
src: cloudinit_userdata.yaml.j2
|
||||
dest: "/var/lib/vz/snippets/{{ vm_id }}-user.yaml"
|
||||
|
||||
- name: Write SSH key snippet
|
||||
copy:
|
||||
content: "{{ lookup('file', ssh_key_path) }}"
|
||||
dest: "/var/lib/vz/snippets/{{ vm_id }}-sshkey.pub"
|
||||
|
||||
##################################################################
|
||||
# 8. Apply Cloud-Init
|
||||
##################################################################
|
||||
- name: Apply Cloud-Init config
|
||||
command: >
|
||||
qm set {{ vm_id }}
|
||||
--ciuser {{ ci_user }}
|
||||
--sshkeys local:snippets/{{ vm_id }}-sshkey.pub
|
||||
--hostname {{ hostname }}
|
||||
--citype nocloud
|
||||
--cicustom "user=local:snippets/{{ vm_id }}-user.yaml,vendor=local:snippets/{{ vm_id }}-vendor.yaml"
|
||||
--ipconfig0 {{ ipconfig0 }}
|
||||
|
||||
##################################################################
|
||||
# 9. Convert VM to Template
|
||||
##################################################################
|
||||
- name: Convert VM to template
|
||||
command: qm template {{ vm_id }}
|
||||
- name: "STAGE 5: Convert VM to template"
|
||||
include_tasks: create-template.yml
|
||||
tags: [template, create]
|
||||
when: make_template | default(false)
|
||||
args:
|
||||
creates: "/etc/pve/qemu-server/{{ vm_id }}.conf.lock"
|
||||
|
||||
##################################################################
|
||||
# 10. Create Clones (if enabled)
|
||||
# 6. CREATE CLONES
|
||||
##################################################################
|
||||
- name: Create clones from template
|
||||
- name: "STAGE 6: Create and configure clones"
|
||||
include_tasks: create-clones.yml
|
||||
tags: [clones, create]
|
||||
when: create_clones | default(false)
|
||||
loop: "{{ clones }}"
|
||||
loop_control:
|
||||
loop_var: clone
|
||||
|
||||
block:
|
||||
- name: Check if clone exists
|
||||
stat:
|
||||
path: "/etc/pve/qemu-server/{{ clone.id }}.conf"
|
||||
register: clone_conf
|
||||
post_tasks:
|
||||
- name: "Display completion summary"
|
||||
debug:
|
||||
msg: |
|
||||
╔════════════════════════════════════════════════════════════╗
|
||||
║ ✓ Playbook execution completed ║
|
||||
║ ║
|
||||
║ 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 ║
|
||||
║ ║
|
||||
╚════════════════════════════════════════════════════════════╝
|
||||
|
||||
- name: Clone VM from template
|
||||
command: >
|
||||
qm clone {{ vm_id }} {{ clone.id }} --name {{ clone.hostname }} --full {{ clone.full }}
|
||||
when: not clone_conf.stat.exists
|
||||
|
||||
- name: Apply Cloud-Init settings for clone
|
||||
command: >
|
||||
qm set {{ clone.id }}
|
||||
--hostname {{ clone.hostname }}
|
||||
--ipconfig0 ip={{ clone.ip }},gw={{ clone.gateway }}
|
||||
|
||||
- name: Start clone VM
|
||||
command: qm start {{ clone.id }}
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user