Files
Kamino/roles/pve_vm/tasks/detect.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

38 lines
1.8 KiB
YAML

---
# no_log hides the whole failure of the lookup below, so the operator would only see "output hidden
# due to no_log". The rescue re-raises just the module's msg, which is safe to show: the module
# masks the token secret in everything it returns (its invocation echo is not used here).
- name: Look up the VM
block:
- name: Check if VM exists
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: current
register: __pve_vm_info
# proxmox_kvm declares no check mode support, so --check would skip this lookup, leave
# pve_vm_exists false and skip everything after it. state=current only reads (GET of the VM
# and its status), so it runs for real under --check as well.
check_mode: false
# An absent VM is an expected answer here: for state=current the module fails with
# "VM with vmid <id> does not exist in cluster". Any other failure (API, auth, network) is
# real and must stop the run instead of being read as "VM absent", which would send
# create.yml after an existing VM.
failed_when:
- __pve_vm_info.failed | default(false)
- "'does not exist in cluster' not in (__pve_vm_info.msg | default(''))"
changed_when: false
rescue:
- name: Fail with the module message
ansible.builtin.fail:
msg: >-
Looking up VM {{ pve_vm_vmid }} on {{ pve_vm_node }} failed:
{{ ansible_failed_result.msg | default('no message') }}
- name: Set VM existence fact
ansible.builtin.set_fact:
pve_vm_exists: >-
{{ __pve_vm_info.vmid is defined or __pve_vm_info.status is defined }}