qemu agent for vm template

This commit is contained in:
counterweight 2025-10-30 23:11:14 +01:00
parent e03c21e853
commit 102fad268c
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C

View file

@ -26,6 +26,9 @@
proxmox_sshkey_path: "/home/{{ new_user }}/.ssh/authorized_keys" # Path to pubkey file for cloud-init injection 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 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 # Storage to import disk into; use existing storage like local-lvm or your ZFS pool name
proxmox_image_storage: "{{ zfs_pool_name }}" proxmox_image_storage: "{{ zfs_pool_name }}"
@ -57,6 +60,34 @@
force: false force: false
when: not debian_image_stat.stat.exists 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 - name: Check if VMID already exists
command: qm config {{ proxmox_template_vmid }} command: qm config {{ proxmox_template_vmid }}
register: vmid_config_check register: vmid_config_check
@ -91,6 +122,21 @@
when: when:
- vmid_config_check.rc != 0 or not vm_already_template - 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) - name: Build consolidated qm set argument list (simplified)
set_fact: set_fact:
qm_set_args: >- qm_set_args: >-
@ -110,15 +156,16 @@
] ]
+ (proxmox_ci_upgrade | bool + (proxmox_ci_upgrade | bool
| ternary(['--ciupgrade 1'], [])) | ternary(['--ciupgrade 1'], []))
+ ['--cicustom user=local:snippets/' ~ qemu_agent_snippet_filename]
}} }}
when: 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 - name: Apply consolidated qm set
command: > command: >
qm set {{ proxmox_template_vmid }} {{ qm_set_args | join(' ') }} qm set {{ proxmox_template_vmid }} {{ qm_set_args | join(' ') }}
when: 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 - name: Resize primary disk to requested size
command: > command: >