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
11 KiB
pve_vm
Clones a Proxmox VE QEMU VM from a template through the Proxmox API and keeps its configuration, cloud-init settings, boot disk size and power state in line with the inputs. The configuration is updated only when drift is detected. Variables and their types are defined in meta/argument_specs.yml, which is the source of truth.
Supported platforms
Ubuntu 24.04 (noble) and 26.04 (resolute) guests, the platforms the molecule scenario runs on. The play runs for the VM's inventory host: the API calls run on the controller (pve_vm_delegate, default localhost), and only the cloud-init wait runs on the guest itself. The template the VM is cloned from must be a cloud-init image of one of these releases.
The guest-side cloud-init wait (cloud-init status --wait) is untested on both releases: the test images do not ship cloud-init.
The role targets the guests, not the Proxmox node, so it does not declare Debian trixie: the node is reached only through its API.
Requirements and run order
- Use it by its fully qualified name,
mortis.kamino.pve_vm, in a play over the VM hosts withgather_facts: false: the VM may not exist yet when the play starts. - The controller needs the
community.proxmoxcollection and the Python librariesproxmoxer2.3 or later andrequests. - Role dependencies: none.
pve_vm_nodeandpve_vm_vmidare required.pve_vm_api_userandpve_vm_api_token_secrethave no usable default and are asserted non-empty before any change;pve_vm_disk_sizetoo whilepve_vm_manage_diskis true, unlesspve_vm_stateisabsent.pve_vm_api_hostdefaults to theansible_hostofpve_vm_nodefrom hostvars, so the node must then be an inventory host even when only the VM hosts are targeted. Setpve_vm_api_hostexplicitly when the node is not in the inventory.- The VM's own
ansible_hostis the target of the SSH wait, andinventory_hostname, lowercased with underscores replaced by dashes, is its name in Proxmox. - Run it before any role that configures the inside of the VM. Gate those on
pve_vm_exists(below).
API privileges
The API token needs, on the VM (or a pool or path above it), the template and the storage:
VM.Allocate,VM.Clone(on the template),VM.AuditandVM.Config.*(VM.Config.CDROM,VM.Config.CPU,VM.Config.Cloudinit,VM.Config.Disk,VM.Config.HWType,VM.Config.Memory,VM.Config.Network,VM.Config.Options), andVM.PowerMgmtfor the power state and restarts;Datastore.AllocateSpaceon the storage that holds the disks;SDN.Useon the bridge or zone whenpve_vm_networksets a network.
A token with privilege separation (the Proxmox default) gets the intersection of its own ACL and its user's: grant the role on both the user and the token.
Role variables
| Variable | Default | Description |
|---|---|---|
pve_vm_node |
none (required) | Name of the Proxmox node that owns the VM. |
pve_vm_vmid |
none (required) | VM id; for a clone, the new id. |
pve_vm_state |
present |
present, started, stopped or absent. present leaves the power state alone. |
pve_vm_allow_destroy |
false |
Required before absent deletes the VM. |
pve_vm_clone_vmid |
none | Template id, read and asserted set only when the VM does not exist and is cloned. |
pve_vm_full_clone |
true |
Full clone instead of a linked clone. |
pve_vm_delegate |
localhost |
Host that runs the API module tasks. |
pve_vm_wait_for_guest |
true |
Wait for SSH and cloud-init when started. |
pve_vm_agent |
true |
QEMU guest agent. |
pve_vm_onboot |
true |
Start on node boot. |
pve_vm_autostart |
true |
Sent as autostart on update, never compared. |
pve_vm_memory, pve_vm_cores, pve_vm_sockets |
none | Sizing, omitted when undefined. |
pve_vm_network, pve_vm_ipconfig |
none | Mappings for net and ipconfig, see the spec. Omitted when undefined. |
pve_vm_nameservers |
none | List, or space or comma separated string. Omitted when undefined. |
pve_vm_sshkeys |
none | Cloud-init public keys, one per line. Omitted when undefined. |
pve_vm_ci_user |
"" |
Cloud-init user (ciuser). Left out of the update and of the drift check when empty. |
pve_vm_force_update |
false |
Push the configuration and cloud-init settings without drift. |
pve_vm_manage_disk |
true |
Grow the boot disk to pve_vm_disk_size. |
pve_vm_disk_size |
none (asserted) | Boot disk size, for example 32G; only ever grown. |
pve_vm_boot_disk |
scsi0 |
Disk key whose size is read and resized. |
pve_vm_reboot_if_required |
true |
Restart the VM after its boot disk was grown. |
pve_vm_api_user |
"" (asserted) |
API user without realm; @<pve_vm_api_realm> is appended. |
pve_vm_api_realm |
pam |
Realm of the API user. |
pve_vm_api_token_id |
<pve_vm_api_user>-token |
API token id. |
pve_vm_api_token_secret |
none (asserted) | API token secret, normally vaulted. No default. no_log. |
pve_vm_api_host |
ansible_host of pve_vm_node, empty without it |
Proxmox API address (port 8006). |
The pve_vm_api_* inputs are applied through module_defaults inside the block that holds every API call of the role, and the REST lookup of the VM config sends the header PVEAPIToken=<user>@<realm>!<token_id>=<secret>, derived in vars/main.yml.
Variables set by the role
pve_vm_exists: the VM exists on the node.
Tags
The input validation (tasks/validate.yml) carries always, so it runs whatever tags are selected. No other task is tagged, so only a play-level tag applies to them.
Example play
- name: Provision VMs
hosts: vms
gather_facts: false
roles:
- role: mortis.kamino.pve_vm
vars:
pve_vm_state: started
pve_vm_node: pve1
pve_vm_vmid: 210
pve_vm_clone_vmid: 9000
pve_vm_memory: 4096
pve_vm_cores: 2
pve_vm_disk_size: 32G
pve_vm_network:
net0:
model: virtio
bridge: vmbr0
tag: 20
pve_vm_ipconfig:
ipconfig0: "ip={{ ansible_host }}/24,gw=192.0.2.1"
pve_vm_ci_user: ubuntu
pve_vm_sshkeys: "{{ lookup('ansible.builtin.file', 'files/admin.pub') }}"
pve_vm_api_user: ansible
pve_vm_api_token_secret: "{{ vault_pve_api_token_secret }}"
- name: Configure the VMs
hosts: vms
tasks:
- name: Configure the guest
ansible.builtin.include_role:
name: mortis.kamino.user_management
when: pve_vm_exists | default(false)
Tests
The default molecule scenario runs in CI on Ubuntu 24.04 (noble) and 26.04 (resolute), the molecule-ubuntu2404 and molecule-ubuntu2604 images, which stand in for the guest. It runs check before the converge (a fresh host) and after it. The converge includes the internal task files validate and detect_drift, with a seeded VM config in Proxmox's own forms standing in for the REST lookup.
- Positive:
validateaccepts the scenario inputs, an unmanaged disk without a size, andabsentwithout a size. - Negative: through
main, a state outside the spec, a VM id that is not a number, a missing API user and a missing token secret; throughvalidate, a missing token secret withabsentand a managed disk without a size; throughcreate, a clone withoutpve_vm_clone_vmid. Each asserts the expected message. - Drift:
detect_driftover seeded configs: a matching config; the cloud-init user (empty input with a different or missing liveciuser, a changed user, a missingciuser); the SSH keys (missing, rotated, empty input); a forced update; Proxmox's other value forms; every compared dimension at once; a dropped VLAN tag. - Static, over every task file under
tasks/(subdirectories and nestedblock,rescueandalwaysincluded): no task callingproxmox_kvm(ascommunity.proxmox.proxmox_kvm,community.general.proxmox_kvm,ansible.legacy.proxmox_kvmorproxmox_kvm, by its key or throughactionorlocal_action, with itsargsmerged) sendscipassword; nomodule_defaultsentry of any task or block setscipassword; exactly one such task deletes a storedcipassword, and it isno_log.
Not exercised: the API lifecycle (lookup, clone, update, the cipassword delete, resize, start, stop, restart and delete), as there is no Proxmox API in the scenario; and the guest-side cloud-init wait, as the test images do not ship cloud-init.
Notes
- Idempotent: yes, as far as drift detection goes, which the scenario checks: the update runs only on drift. The API calls themselves rely on
community.proxmox, which is not tested; the update and thecipassworddelete report changed whenever they run. - Check mode:
--checkis the supported preview; a play-levelcheck_mode: trueis not supported.proxmox_kvmandproxmox_diskhave no check mode support, so under--checkthe two reads (theproxmox_kvmstate: currentlookup and the REST GET of the VM config) run for real withcheck_mode: false, and every write is skipped and previewed instead:Preview VM creationfor a missing VM (nothing else runs for it),Report configuration driftnames the drifted dimensions and a pending disk resize (changed when the update or resize would run; it also reports on a real run),Preview deletion of the stored cloud-init passwordandPreview VM power state. Delete and restart are skipped. Roles gated onpve_vm_existstherefore run in check mode against existing VMs. - Drift is checked for name, CPU type (always
host), memory, cores, sockets, cloud-init user,ipconfig0, nameservers,onboot, agent andnet0. A missingciuserorsshkeyskey in the live config also counts, and so does the first run after a clone. - Cloud-init
sshkeysare stored URL-encoded by Proxmox and cannot be diffed. A rotatedpve_vm_sshkeysreaches an existing VM only through other detected drift, a fresh clone orpve_vm_force_update: true. - The role never sets a cloud-init password (
cipassword): the first login is by SSH key. Acipasswordstored on a VM is deleted on the next real run, by ano_logtask. - Disk: the boot disk only grows. Only the digits of
pve_vm_disk_sizeare compared with the current size in GB. After a resize the VM is restarted whenpve_vm_reboot_if_requiredis true. - With
pve_vm_state: absentandpve_vm_allow_destroytrue the VM is stopped and deleted and the play ends for that host. Withoutpve_vm_allow_destroythe role only looks the VM up and changes nothing. - A VM that does not exist is detected by the module's "does not exist in cluster" message. Any other lookup failure stops the run, re-raised with only the module message.
- TLS certificates of the API are not verified.
no_logis set on every task that carries the API token.