Files
ansible_proxmox_VM/tasks/configure-vm.yml
Jose f62750fe2f 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.
2025-11-15 17:22:21 +01:00

170 lines
5.1 KiB
YAML

---
# configure-vm.yml - Configure VM with UEFI, TPM, disks, GPU, and Cloud-Init
- name: "[CONFIG] Configure UEFI + Secure Boot + TPM (if enabled)"
block:
- name: "[CONFIG] Enable UEFI and TPM"
command: >
qm set {{ vm_id }}
--bios ovmf
--efidisk0 {{ storage }}:0,pre-enrolled-keys=1
--tpmstate0 {{ storage }}:1,size=4M,version=v2.0
register: tpm_config
changed_when: tpm_config.rc == 0
- name: "[CONFIG] Verify TPM configuration"
debug:
msg: "✓ UEFI + TPM configured for VM {{ vm_id }}"
when: enable_tpm | default(false)
- name: "[CONFIG] Import and attach disk"
block:
- name: "[CONFIG] Check if disk already exists"
stat:
path: "/var/lib/vz/images/{{ vm_id }}/vm-{{ vm_id }}-disk-0.qcow2"
register: disk_exists
changed_when: false
- name: "[CONFIG] Import qcow2 disk"
command: >
qm importdisk {{ vm_id }}
{{ debian_image_path }}
{{ storage }}
register: disk_import
retries: 3
delay: 5
until: disk_import is succeeded
when: not disk_exists.stat.exists
- name: "[CONFIG] Verify disk import"
fail:
msg: "Disk import failed for VM {{ vm_id }}"
when:
- not disk_exists.stat.exists
- disk_import is failed
- name: "[CONFIG] Attach imported disk"
command: >
qm set {{ vm_id }}
--scsihw virtio-scsi-pci
--scsi0 {{ storage }}:vm-{{ vm_id }}-disk-0
register: disk_attach
when: not disk_exists.stat.exists
changed_when: disk_attach.rc == 0
- name: "[CONFIG] Enable serial console and set boot order"
command: >
qm set {{ vm_id }}
--serial0 socket
--boot order=scsi0
register: serial_config
changed_when: serial_config.rc == 0
- name: "[CONFIG] Display disk configuration"
debug:
msg: "✓ Disk configured and attached to VM {{ vm_id }}"
rescue:
- name: "[CONFIG] Handle disk configuration error"
fail:
msg: |
Failed to configure disk for VM {{ vm_id }}:
{{ ansible_failed_result | default('Unknown error') }}
- name: "[CONFIG] Resize disk (if enabled)"
block:
- name: "[CONFIG] Resize disk"
command: "qm resize {{ vm_id }} scsi0 {{ resize_size }}"
register: disk_resize
changed_when: disk_resize.rc == 0
- name: "[CONFIG] Display disk resize result"
debug:
msg: "✓ Disk resized to {{ resize_size }}"
when: resize_disk | default(false)
- name: "[CONFIG] Configure GPU passthrough (if enabled)"
block:
- name: "[CONFIG] Enable PCI GPU passthrough"
command: "qm set {{ vm_id }} --hostpci0 {{ gpu_device }}"
register: gpu_config
changed_when: gpu_config.rc == 0
- name: "[CONFIG] Display GPU configuration"
debug:
msg: "✓ GPU passthrough configured: {{ gpu_device }}"
when: gpu_passthrough | default(false)
- name: "[CONFIG] Configure VirtIO GPU (if enabled)"
block:
- name: "[CONFIG] Enable VirtIO GPU"
command: "qm set {{ vm_id }} --vga virtio"
register: virtio_gpu_config
changed_when: virtio_gpu_config.rc == 0
- name: "[CONFIG] Display VirtIO GPU configuration"
debug:
msg: "✓ VirtIO GPU configured"
when: virtio_gpu | default(false)
- name: "[CONFIG] Create and apply Cloud-Init snippets"
block:
- name: "[CONFIG] Create Cloud-Init vendor-data snippet"
template:
src: cloudinit_vendor.yaml.j2
dest: "/var/lib/vz/snippets/{{ vm_id }}-vendor.yaml"
mode: "0644"
register: vendor_snippet
- name: "[CONFIG] Create Cloud-Init user-data snippet"
template:
src: cloudinit_userdata.yaml.j2
dest: "/var/lib/vz/snippets/{{ vm_id }}-user.yaml"
mode: "0644"
register: user_snippet
- name: "[CONFIG] Verify SSH key is readable"
stat:
path: "{{ ssh_key_path | expanduser }}"
register: ssh_key_stat
failed_when: not ssh_key_stat.stat.readable
- name: "[CONFIG] Copy SSH public key to snippets"
copy:
src: "{{ ssh_key_path | expanduser }}"
dest: "/var/lib/vz/snippets/{{ vm_id }}-sshkey.pub"
mode: "0644"
register: ssh_snippet
- name: "[CONFIG] Apply Cloud-Init configuration"
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 }}
register: cloudinit_apply
changed_when: cloudinit_apply.rc == 0
- name: "[CONFIG] Display Cloud-Init configuration"
debug:
msg: |
✓ Cloud-Init configured
- User: {{ ci_user }}
- Hostname: {{ hostname }}
- IP Config: {{ ipconfig0 }}
- Timezone: {{ timezone }}
rescue:
- name: "[CONFIG] Handle Cloud-Init configuration error"
fail:
msg: |
Failed to configure Cloud-Init for VM {{ vm_id }}:
{{ ansible_failed_result | default('Unknown error') }}