--- - 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