Files
Kamino/roles/pve_vm
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
..
2026-11-29 20:10:33 +00:00
2026-11-29 20:10:33 +00:00
2026-11-29 20:10:33 +00:00
2026-11-29 20:10:33 +00:00

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 with gather_facts: false: the VM may not exist yet when the play starts.
  • The controller needs the community.proxmox collection and the Python libraries proxmoxer 2.3 or later and requests.
  • Role dependencies: none.
  • pve_vm_node and pve_vm_vmid are required. pve_vm_api_user and pve_vm_api_token_secret have no usable default and are asserted non-empty before any change; pve_vm_disk_size too while pve_vm_manage_disk is true, unless pve_vm_state is absent.
  • pve_vm_api_host defaults to the ansible_host of pve_vm_node from hostvars, so the node must then be an inventory host even when only the VM hosts are targeted. Set pve_vm_api_host explicitly when the node is not in the inventory.
  • The VM's own ansible_host is the target of the SSH wait, and inventory_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.Audit and VM.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), and VM.PowerMgmt for the power state and restarts;
  • Datastore.AllocateSpace on the storage that holds the disks;
  • SDN.Use on the bridge or zone when pve_vm_network sets 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: validate accepts the scenario inputs, an unmanaged disk without a size, and absent without 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; through validate, a missing token secret with absent and a managed disk without a size; through create, a clone without pve_vm_clone_vmid. Each asserts the expected message.
  • Drift: detect_drift over seeded configs: a matching config; the cloud-init user (empty input with a different or missing live ciuser, a changed user, a missing ciuser); 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 nested block, rescue and always included): no task calling proxmox_kvm (as community.proxmox.proxmox_kvm, community.general.proxmox_kvm, ansible.legacy.proxmox_kvm or proxmox_kvm, by its key or through action or local_action, with its args merged) sends cipassword; no module_defaults entry of any task or block sets cipassword; exactly one such task deletes a stored cipassword, and it is no_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 the cipassword delete report changed whenever they run.
  • Check mode: --check is the supported preview; a play-level check_mode: true is not supported. proxmox_kvm and proxmox_disk have no check mode support, so under --check the two reads (the proxmox_kvm state: current lookup and the REST GET of the VM config) run for real with check_mode: false, and every write is skipped and previewed instead: Preview VM creation for a missing VM (nothing else runs for it), Report configuration drift names 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 password and Preview VM power state. Delete and restart are skipped. Roles gated on pve_vm_exists therefore 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 and net0. A missing ciuser or sshkeys key in the live config also counts, and so does the first run after a clone.
  • Cloud-init sshkeys are stored URL-encoded by Proxmox and cannot be diffed. A rotated pve_vm_sshkeys reaches an existing VM only through other detected drift, a fresh clone or pve_vm_force_update: true.
  • The role never sets a cloud-init password (cipassword): the first login is by SSH key. A cipassword stored on a VM is deleted on the next real run, by a no_log task.
  • Disk: the boot disk only grows. Only the digits of pve_vm_disk_size are compared with the current size in GB. After a resize the VM is restarted when pve_vm_reboot_if_required is true.
  • With pve_vm_state: absent and pve_vm_allow_destroy true the VM is stopped and deleted and the play ends for that host. Without pve_vm_allow_destroy the 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_log is set on every task that carries the API token.