2025-11-15 09:51:38 +01:00
|
|
|
---
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: "Create a Debian VM template and optionally deploy clones"
|
2025-11-15 09:51:38 +01:00
|
|
|
hosts: localhost
|
|
|
|
|
become: true
|
2025-11-15 12:59:20 +01:00
|
|
|
gather_facts: false
|
2025-11-15 09:51:38 +01:00
|
|
|
|
|
|
|
|
tasks:
|
|
|
|
|
|
|
|
|
|
##################################################################
|
|
|
|
|
# 1. Ensure Debian GenericCloud Image Exists
|
|
|
|
|
##################################################################
|
|
|
|
|
- name: Check for Debian image
|
2025-11-15 12:59:20 +01:00
|
|
|
stat:
|
2025-11-15 09:51:38 +01:00
|
|
|
path: "/var/lib/vz/template/qemu/debian-genericcloud-amd64.qcow2"
|
|
|
|
|
register: debian_img
|
|
|
|
|
|
|
|
|
|
- name: Download GenericCloud qcow2
|
2025-11-15 12:59:20 +01:00
|
|
|
get_url:
|
2025-11-15 09:51:38 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
# 2. Create Base VM (if not exists)
|
2025-11-15 09:51:38 +01:00
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: Check if VM exists
|
|
|
|
|
stat:
|
|
|
|
|
path: "/etc/pve/qemu-server/{{ vm_id }}.conf"
|
|
|
|
|
register: vm_conf
|
|
|
|
|
|
2025-11-15 09:51:38 +01:00
|
|
|
- name: Create VM
|
2025-11-15 12:59:20 +01:00
|
|
|
command: >
|
2025-11-15 09:51:38 +01:00
|
|
|
qm create {{ vm_id }}
|
|
|
|
|
--name {{ hostname }}
|
|
|
|
|
--memory {{ memory }}
|
|
|
|
|
--cores {{ cores }}
|
|
|
|
|
--cpu {{ cpu_type }}
|
|
|
|
|
--net0 virtio,bridge={{ bridge }},macaddr={{ mac_address }}
|
2025-11-15 12:59:20 +01:00
|
|
|
--agent 1
|
|
|
|
|
when: not vm_conf.stat.exists
|
2025-11-15 09:51:38 +01:00
|
|
|
|
|
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
# 3. Optional UEFI + Secure Boot + TPM
|
2025-11-15 09:51:38 +01:00
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: Enable UEFI + TPM
|
|
|
|
|
command: >
|
2025-11-15 09:51:38 +01:00
|
|
|
qm set {{ vm_id }}
|
|
|
|
|
--bios ovmf
|
2025-11-15 12:59:20 +01:00
|
|
|
--efidisk0 {{ storage }}:0,pre-enrolled-keys=1
|
2025-11-15 09:51:38 +01:00
|
|
|
--tpmstate0 {{ storage }}:1,size=4M,version=v2.0
|
|
|
|
|
when: enable_tpm | default(false)
|
|
|
|
|
|
|
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
# 4. Disk Import & Attach
|
2025-11-15 09:51:38 +01:00
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: Check if disk already exists
|
|
|
|
|
stat:
|
|
|
|
|
path: "/var/lib/vz/images/{{ vm_id }}/vm-{{ vm_id }}-disk-0.qcow2"
|
|
|
|
|
register: disk_exists
|
|
|
|
|
|
2025-11-15 09:51:38 +01:00
|
|
|
- name: Import qcow2 disk
|
2025-11-15 12:59:20 +01:00
|
|
|
command: >
|
2025-11-15 09:51:38 +01:00
|
|
|
qm importdisk {{ vm_id }}
|
|
|
|
|
/var/lib/vz/template/qemu/debian-genericcloud-amd64.qcow2
|
|
|
|
|
{{ storage }}
|
2025-11-15 12:59:20 +01:00
|
|
|
when: not disk_exists.stat.exists
|
2025-11-15 09:51:38 +01:00
|
|
|
|
|
|
|
|
- name: Attach imported disk
|
2025-11-15 12:59:20 +01:00
|
|
|
command: >
|
2025-11-15 09:51:38 +01:00
|
|
|
qm set {{ vm_id }}
|
|
|
|
|
--scsihw virtio-scsi-pci
|
|
|
|
|
--scsi0 {{ storage }}:vm-{{ vm_id }}-disk-0
|
2025-11-15 12:59:20 +01:00
|
|
|
when: not disk_exists.stat.exists
|
|
|
|
|
|
|
|
|
|
- name: Enable serial console + boot disk
|
|
|
|
|
command: >
|
|
|
|
|
qm set {{ vm_id }}
|
|
|
|
|
--serial0 socket
|
|
|
|
|
--boot order=scsi0
|
2025-11-15 09:51:38 +01:00
|
|
|
|
|
|
|
|
##################################################################
|
|
|
|
|
# 5. Optional Disk Resize
|
|
|
|
|
##################################################################
|
|
|
|
|
- name: Resize disk
|
2025-11-15 12:59:20 +01:00
|
|
|
command: qm resize {{ vm_id }} scsi0 {{ resize_size }}
|
2025-11-15 09:51:38 +01:00
|
|
|
when: resize_disk | default(false)
|
|
|
|
|
|
|
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
# 6. Optional GPU
|
2025-11-15 09:51:38 +01:00
|
|
|
##################################################################
|
|
|
|
|
- name: PCI GPU passthrough
|
2025-11-15 12:59:20 +01:00
|
|
|
command: qm set {{ vm_id }} --hostpci0 {{ gpu_device }}
|
2025-11-15 09:51:38 +01:00
|
|
|
when: gpu_passthrough | default(false)
|
|
|
|
|
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: VirtIO GPU
|
|
|
|
|
command: qm set {{ vm_id }} --vga virtio
|
2025-11-15 09:51:38 +01:00
|
|
|
when: virtio_gpu | default(false)
|
|
|
|
|
|
|
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
# 7. Cloud-Init Snippets
|
2025-11-15 09:51:38 +01:00
|
|
|
##################################################################
|
|
|
|
|
- name: Create Cloud-Init vendor-data
|
2025-11-15 12:59:20 +01:00
|
|
|
template:
|
2025-11-15 09:51:38 +01:00
|
|
|
src: cloudinit_vendor.yaml.j2
|
|
|
|
|
dest: "/var/lib/vz/snippets/{{ vm_id }}-vendor.yaml"
|
|
|
|
|
|
|
|
|
|
- name: Create Cloud-Init user-data
|
2025-11-15 12:59:20 +01:00
|
|
|
template:
|
2025-11-15 09:51:38 +01:00
|
|
|
src: cloudinit_userdata.yaml.j2
|
|
|
|
|
dest: "/var/lib/vz/snippets/{{ vm_id }}-user.yaml"
|
|
|
|
|
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: Write SSH key snippet
|
|
|
|
|
copy:
|
|
|
|
|
content: "{{ lookup('file', ssh_key_path) }}"
|
|
|
|
|
dest: "/var/lib/vz/snippets/{{ vm_id }}-sshkey.pub"
|
|
|
|
|
|
2025-11-15 09:51:38 +01:00
|
|
|
##################################################################
|
|
|
|
|
# 8. Apply Cloud-Init
|
|
|
|
|
##################################################################
|
|
|
|
|
- name: Apply Cloud-Init config
|
2025-11-15 12:59:20 +01:00
|
|
|
command: >
|
2025-11-15 09:51:38 +01:00
|
|
|
qm set {{ vm_id }}
|
|
|
|
|
--ciuser {{ ci_user }}
|
2025-11-15 12:59:20 +01:00
|
|
|
--sshkeys local:snippets/{{ vm_id }}-sshkey.pub
|
2025-11-15 09:51:38 +01:00
|
|
|
--hostname {{ hostname }}
|
|
|
|
|
--citype nocloud
|
|
|
|
|
--cicustom "user=local:snippets/{{ vm_id }}-user.yaml,vendor=local:snippets/{{ vm_id }}-vendor.yaml"
|
|
|
|
|
--ipconfig0 {{ ipconfig0 }}
|
|
|
|
|
|
|
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
# 9. Convert VM to Template
|
2025-11-15 09:51:38 +01:00
|
|
|
##################################################################
|
|
|
|
|
- name: Convert VM to template
|
2025-11-15 12:59:20 +01:00
|
|
|
command: qm template {{ vm_id }}
|
2025-11-15 09:51:38 +01:00
|
|
|
when: make_template | default(false)
|
2025-11-15 12:59:20 +01:00
|
|
|
args:
|
|
|
|
|
creates: "/etc/pve/qemu-server/{{ vm_id }}.conf.lock"
|
2025-11-15 09:51:38 +01:00
|
|
|
|
|
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
# 10. Create Clones (if enabled)
|
2025-11-15 09:51:38 +01:00
|
|
|
##################################################################
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: Create clones from template
|
2025-11-15 09:51:38 +01:00
|
|
|
when: create_clones | default(false)
|
|
|
|
|
loop: "{{ clones }}"
|
|
|
|
|
loop_control:
|
|
|
|
|
loop_var: clone
|
|
|
|
|
|
|
|
|
|
block:
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: Check if clone exists
|
|
|
|
|
stat:
|
|
|
|
|
path: "/etc/pve/qemu-server/{{ clone.id }}.conf"
|
|
|
|
|
register: clone_conf
|
2025-11-15 09:51:38 +01:00
|
|
|
|
|
|
|
|
- name: Clone VM from template
|
2025-11-15 12:59:20 +01:00
|
|
|
command: >
|
|
|
|
|
qm clone {{ vm_id }} {{ clone.id }} --name {{ clone.hostname }} --full {{ clone.full }}
|
|
|
|
|
when: not clone_conf.stat.exists
|
2025-11-15 09:51:38 +01:00
|
|
|
|
2025-11-15 12:59:20 +01:00
|
|
|
- name: Apply Cloud-Init settings for clone
|
|
|
|
|
command: >
|
2025-11-15 09:51:38 +01:00
|
|
|
qm set {{ clone.id }}
|
|
|
|
|
--hostname {{ clone.hostname }}
|
|
|
|
|
--ipconfig0 ip={{ clone.ip }},gw={{ clone.gateway }}
|
|
|
|
|
|
|
|
|
|
- name: Start clone VM
|
2025-11-15 12:59:20 +01:00
|
|
|
command: qm start {{ clone.id }}
|