From 102fad268c3c10d611491a602e5d191d7255001b Mon Sep 17 00:00:00 2001 From: counterweight Date: Thu, 30 Oct 2025 23:11:14 +0100 Subject: [PATCH] qemu agent for vm template --- .../33_proxmox_debian_cloud_template.yml | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/ansible/infra/nodito/33_proxmox_debian_cloud_template.yml b/ansible/infra/nodito/33_proxmox_debian_cloud_template.yml index ff69525..6a123d7 100644 --- a/ansible/infra/nodito/33_proxmox_debian_cloud_template.yml +++ b/ansible/infra/nodito/33_proxmox_debian_cloud_template.yml @@ -26,6 +26,9 @@ proxmox_sshkey_path: "/home/{{ new_user }}/.ssh/authorized_keys" # Path to pubkey file for cloud-init injection proxmox_ci_upgrade: true # If true, run package upgrade on first boot + # Auto-install qemu-guest-agent in clones via cloud-init snippet + qemu_agent_snippet_filename: "user-data-qemu-agent.yaml" + # Storage to import disk into; use existing storage like local-lvm or your ZFS pool name proxmox_image_storage: "{{ zfs_pool_name }}" @@ -57,6 +60,34 @@ force: false when: not debian_image_stat.stat.exists + - name: Ensure ZFS storage allows snippets content + command: > + pvesm set {{ proxmox_image_storage }} --content images,rootdir,snippets + + - name: Ensure local storage allows snippets content + command: > + pvesm set local --content images,iso,vztmpl,snippets + failed_when: false + + - name: Ensure snippets directory exists on storage mountpoint + file: + path: "{{ zfs_pool_mountpoint }}/snippets" + state: directory + mode: '0755' + + - name: Write cloud-init user-data snippet to install qemu-guest-agent + copy: + dest: "{{ zfs_pool_mountpoint }}/snippets/{{ qemu_agent_snippet_filename }}" + mode: '0644' + content: | + #cloud-config + package_update: false + package_upgrade: false + packages: + - qemu-guest-agent + runcmd: + - [ systemctl, enable, --now, qemu-guest-agent ] + - name: Check if VMID already exists command: qm config {{ proxmox_template_vmid }} register: vmid_config_check @@ -91,6 +122,21 @@ when: - vmid_config_check.rc != 0 or not vm_already_template + - name: Check if ide2 (cloudinit) drive exists + command: qm config {{ proxmox_template_vmid }} + register: vm_config_check + failed_when: false + changed_when: false + when: + - vmid_config_check.rc == 0 + + - name: Remove existing ide2 (cloudinit) drive if it exists for idempotency + command: > + qm set {{ proxmox_template_vmid }} --delete ide2 + when: + - vmid_config_check.rc == 0 + - "'ide2:' in vm_config_check.stdout" + - name: Build consolidated qm set argument list (simplified) set_fact: qm_set_args: >- @@ -110,15 +156,16 @@ ] + (proxmox_ci_upgrade | bool | ternary(['--ciupgrade 1'], [])) + + ['--cicustom user=local:snippets/' ~ qemu_agent_snippet_filename] }} when: - - vmid_config_check.rc != 0 or not vm_already_template + - vmid_config_check.rc != 0 or not vm_already_template | default(false) - name: Apply consolidated qm set command: > qm set {{ proxmox_template_vmid }} {{ qm_set_args | join(' ') }} when: - - vmid_config_check.rc != 0 or not vm_already_template + - vmid_config_check.rc != 0 or not vm_already_template | default(false) - name: Resize primary disk to requested size command: >