Manage a Proxmox VE QEMU VM through the Proxmox API: clone from a template, cloud-init user, SSH keys and network, update only on detected drift, grow the boot disk, apply the power state, wait for SSH and cloud-init, and delete behind pve_vm_allow_destroy. The role never sets a cloud-init password and deletes a stored one with no_log. Inputs are validated first under the always tag: the API credentials, and pve_vm_disk_size while the disk is managed; the clone asserts pve_vm_clone_vmid. Only pve_vm_exists is public, every other fact and register is __pve_vm_. The molecule scenario runs offline on noble and resolute: validation with positive and refusal cases, drift detection over seeded VM configs, and static checks over the role's task files that no proxmox_kvm task or module_defaults entry carries cipassword and that the one task deleting a stored cipassword is no_log. The API lifecycle and the cloud-init wait are not exercised. README Roadmap: the pve_vm wave 2 line and the opnsense and proxmox_firewall line are removed; wave 3 stays. Closes #482 Assisted-by: Claude Code:claude-opus-5-5
98 lines
5.2 KiB
YAML
98 lines
5.2 KiB
YAML
---
|
|
# community.proxmox.proxmox_kvm with `update: true` reports changed on every run: it does not diff
|
|
# against the live config. Compare the config already fetched in detect_disk.yml with the desired
|
|
# values and only call the module on real drift or while cloud-init is still unconfigured (first
|
|
# run after a clone, when ciuser and sshkeys have to be applied once). Proxmox stores sshkeys
|
|
# URL-encoded, so a change is not diffed: pve_vm_force_update pushes it to an existing VM.
|
|
- name: Normalise current QEMU guest agent setting
|
|
ansible.builtin.set_fact:
|
|
# PVE returns "1", "enabled=1" or e.g. "fstrim_cloned_disks=1,enabled=1"; enabled=1 may sit
|
|
# in any position.
|
|
__pve_vm_agent_current: >-
|
|
{{
|
|
'1' if ((__pve_vm_config.agent | default('0') | string).split(',')
|
|
| select('in', ['1', 'enabled=1']) | list | length > 0)
|
|
else '0'
|
|
}}
|
|
|
|
- name: Normalise desired and current values for drift comparison
|
|
ansible.builtin.set_fact:
|
|
# ciuser and sshkeys are only expected in the current config when the role sets them.
|
|
__pve_vm_required_config_keys: >-
|
|
{{
|
|
(['ciuser'] if (pve_vm_ci_user | string | length > 0) else [])
|
|
+ (['sshkeys'] if (pve_vm_sshkeys | default('') | string | length > 0) else [])
|
|
}}
|
|
# PVE returns nameserver space-separated; accept a list or a comma-separated string as well.
|
|
__pve_vm_nameservers_desired: >-
|
|
{{
|
|
((pve_vm_nameservers | join(' '))
|
|
if (pve_vm_nameservers | default('') is not string and pve_vm_nameservers | default('') is iterable)
|
|
else (pve_vm_nameservers | default('') | string))
|
|
| replace(',', ' ') | split | join(' ')
|
|
}}
|
|
# ipconfig0 tokens can come back from PVE in a different order.
|
|
__pve_vm_ipconfig0_current: "{{ (__pve_vm_config.ipconfig0 | default('') | string).split(',') | sort }}"
|
|
__pve_vm_ipconfig0_desired: "{{ (pve_vm_ipconfig.ipconfig0 | default('') | string).split(',') | sort }}"
|
|
__pve_vm_net0_current: "{{ (__pve_vm_config.net0 | default('') | string).split(',') }}"
|
|
|
|
# autostart is not compared: PVE has no such VM config key (the config API does not return it).
|
|
# One fact per drift dimension, combined in __pve_vm_needs_update below.
|
|
- name: Determine drift per configuration dimension
|
|
ansible.builtin.set_fact:
|
|
__pve_vm_drift_forced: >-
|
|
{{
|
|
(pve_vm_force_update | bool)
|
|
or (__pve_vm_created | default(false) | bool)
|
|
}}
|
|
__pve_vm_drift_missing_keys: "{{ __pve_vm_required_config_keys | reject('in', __pve_vm_config) | list | length > 0 }}"
|
|
__pve_vm_drift_name: "{{ (__pve_vm_config.name | default('')) != (inventory_hostname | lower | replace('_', '-')) }}"
|
|
# PVE may return "host" or "cputype=host" (optionally followed by flags).
|
|
__pve_vm_drift_cpu: >-
|
|
{{
|
|
(__pve_vm_config.cpu | default('') | string).split(',')[0] | regex_replace('^cputype=', '') != 'host'
|
|
}}
|
|
__pve_vm_drift_resources: >-
|
|
{{
|
|
(pve_vm_memory is defined and (__pve_vm_config.memory | default(0) | int) != (pve_vm_memory | int))
|
|
or (pve_vm_cores is defined and (__pve_vm_config.cores | default(1) | int) != (pve_vm_cores | int))
|
|
or (pve_vm_sockets is defined and (__pve_vm_config.sockets | default(1) | int) != (pve_vm_sockets | int))
|
|
}}
|
|
__pve_vm_drift_ciuser: "{{ (pve_vm_ci_user | string | length > 0) and __pve_vm_config.ciuser | default('') != pve_vm_ci_user }}"
|
|
__pve_vm_drift_ipconfig: "{{ pve_vm_ipconfig.ipconfig0 is defined and __pve_vm_ipconfig0_current != __pve_vm_ipconfig0_desired }}"
|
|
__pve_vm_drift_nameservers: >-
|
|
{{ pve_vm_nameservers is defined and (__pve_vm_config.nameserver | default('')) != __pve_vm_nameservers_desired }}
|
|
__pve_vm_drift_onboot: "{{ (__pve_vm_config.onboot | default(0) | int) != (pve_vm_onboot | bool | int) }}"
|
|
__pve_vm_drift_agent: "{{ __pve_vm_agent_current != ((pve_vm_agent | bool | int) | string) }}"
|
|
__pve_vm_drift_network: >-
|
|
{{
|
|
pve_vm_network.net0 is defined and (
|
|
not (__pve_vm_config.net0 | default('')).startswith(pve_vm_network.net0.model | default('virtio') ~ '=')
|
|
or (pve_vm_network.net0.bridge is defined
|
|
and ('bridge=' ~ pve_vm_network.net0.bridge) not in __pve_vm_net0_current)
|
|
or (pve_vm_network.net0.tag is defined
|
|
and ('tag=' ~ pve_vm_network.net0.tag) not in __pve_vm_net0_current)
|
|
or (pve_vm_network.net0.tag is not defined
|
|
and (__pve_vm_net0_current | select('match', '^tag=') | list | length > 0))
|
|
or (pve_vm_network.net0.firewall is defined
|
|
and (('firewall=1' in __pve_vm_net0_current) != (pve_vm_network.net0.firewall | bool)))
|
|
)
|
|
}}
|
|
|
|
- name: Determine if VM configuration needs updating
|
|
ansible.builtin.set_fact:
|
|
__pve_vm_needs_update: >-
|
|
{{
|
|
__pve_vm_drift_forced | bool
|
|
or __pve_vm_drift_missing_keys | bool
|
|
or __pve_vm_drift_name | bool
|
|
or __pve_vm_drift_cpu | bool
|
|
or __pve_vm_drift_resources | bool
|
|
or __pve_vm_drift_ciuser | bool
|
|
or __pve_vm_drift_ipconfig | bool
|
|
or __pve_vm_drift_nameservers | bool
|
|
or __pve_vm_drift_onboot | bool
|
|
or __pve_vm_drift_agent | bool
|
|
or __pve_vm_drift_network | bool
|
|
}}
|