Files
Kamino/roles/pve_vm/tasks/update.yml
T
Defame1297 f3cab37c7b
Build / build (pull_request) Successful in 46s
Lint / lint (pull_request) Successful in 2m57s
Molecule / molecule (pull_request) Successful in 30m40s
feat(pve_vm): add the pve_vm role
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
2026-11-29 20:10:33 +00:00

127 lines
4.7 KiB
YAML

---
- name: Detect VM configuration drift
ansible.builtin.include_tasks: detect_drift.yml
# Names only, no values: the drift facts are booleans, and sshkeys are never compared.
# Under --check this is the preview of the update and resize below (changed when either would run).
- name: Report configuration drift
ansible.builtin.debug:
msg: >-
{{
'drift: ' ~ (__pve_vm_drift_dimensions | join(', ')) if __pve_vm_drift_dimensions | length > 0
else 'no drift'
}}{{
'; boot disk resize: ' ~ __pve_vm_current_disk ~ 'G -> ' ~ pve_vm_disk_size
if (pve_vm_manage_disk and __pve_vm_resize_disk | default(false) | bool) else ''
}}
vars:
__pve_vm_drift_dimensions: >-
{{
[
'forced' if __pve_vm_drift_forced | bool else '',
'missing_keys' if __pve_vm_drift_missing_keys | bool else '',
'name' if __pve_vm_drift_name | bool else '',
'cpu' if __pve_vm_drift_cpu | bool else '',
'resources' if __pve_vm_drift_resources | bool else '',
'ciuser' if __pve_vm_drift_ciuser | bool else '',
'ipconfig' if __pve_vm_drift_ipconfig | bool else '',
'nameservers' if __pve_vm_drift_nameservers | bool else '',
'onboot' if __pve_vm_drift_onboot | bool else '',
'agent' if __pve_vm_drift_agent | bool else '',
'network' if __pve_vm_drift_network | bool else '',
] | select | list
}}
changed_when: >-
ansible_check_mode and (__pve_vm_needs_update | bool
or (pve_vm_manage_disk and __pve_vm_resize_disk | default(false) | bool))
- name: Update VM configuration
no_log: true # carries the API token secret
delegate_to: "{{ pve_vm_delegate }}"
community.proxmox.proxmox_kvm:
node: "{{ pve_vm_node }}"
vmid: "{{ pve_vm_vmid }}"
name: "{{ inventory_hostname | lower | replace('_', '-') }}"
memory: "{{ pve_vm_memory | default(omit) }}"
cores: "{{ pve_vm_cores | default(omit) }}"
sockets: "{{ pve_vm_sockets | default(omit) }}"
cpu: host
net: "{{ pve_vm_network | default(omit) }}"
ciuser: "{{ pve_vm_ci_user if (pve_vm_ci_user | length > 0) else omit }}"
ipconfig: "{{ pve_vm_ipconfig | default(omit) }}"
sshkeys: "{{ pve_vm_sshkeys | default(omit) }}"
nameservers: "{{ pve_vm_nameservers | default(omit) }}"
agent: "{{ pve_vm_agent }}"
onboot: "{{ pve_vm_onboot }}"
autostart: "{{ pve_vm_autostart }}"
update: true
state: present
# Explicit: the module has no check mode support, so --check skips it anyway; the report above is the preview.
when:
- __pve_vm_needs_update | bool
- not ansible_check_mode
# The role never sets a cloud-init password; a VM configured by other means may still have one
# stored. The module cannot combine delete with update, has no check mode and always reports
# changed, hence a separate task, run only when the key is present, with the debug as its --check
# preview.
- name: Preview deletion of the stored cloud-init password
ansible.builtin.debug:
msg: "cipassword stored on VM {{ pve_vm_vmid }}: a real run deletes it"
changed_when: true
when:
- "'cipassword' in __pve_vm_config"
- ansible_check_mode
- name: Delete the stored cloud-init password
no_log: true # carries the API token secret
delegate_to: "{{ pve_vm_delegate }}"
community.proxmox.proxmox_kvm:
node: "{{ pve_vm_node }}"
vmid: "{{ pve_vm_vmid }}"
delete: cipassword
when:
- "'cipassword' in __pve_vm_config"
- not ansible_check_mode
# The marker has been consumed by drift detection and the update above; reset it so another
# pve_vm run for this host in the same play does not force a second update.
- name: Clear the just-cloned marker
ansible.builtin.set_fact:
__pve_vm_created: false
- name: Resize boot drive
no_log: true # defence in depth: the module already masks the API token secret
delegate_to: "{{ pve_vm_delegate }}"
community.proxmox.proxmox_disk:
vmid: "{{ pve_vm_vmid }}"
disk: "{{ pve_vm_boot_disk }}"
size: "{{ pve_vm_disk_size }}"
state: resized
when:
- pve_vm_manage_disk | bool
- __pve_vm_resize_disk
- not ansible_check_mode
register: __pve_vm_disk_resize
- name: Track disk resize result
ansible.builtin.set_fact:
__pve_vm_disk_resized: "{{ __pve_vm_disk_resize.changed | default(false) }}"
- name: Restart VM if disk was resized
no_log: true # defence in depth: the module already masks the API token secret
delegate_to: "{{ pve_vm_delegate }}"
community.proxmox.proxmox_kvm:
node: "{{ pve_vm_node }}"
vmid: "{{ pve_vm_vmid }}"
state: restarted
when:
- __pve_vm_disk_resized
- pve_vm_reboot_if_required | bool
- not ansible_check_mode