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
324 lines
13 KiB
YAML
324 lines
13 KiB
YAML
---
|
|
# The refusal cases run first: each fails in the input validation, before any API call.
|
|
- name: Refuse invalid input
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Refuse a state outside the argument spec choices
|
|
ansible.builtin.include_tasks: tasks/failed_run.yml
|
|
vars:
|
|
pve_vm_state: running
|
|
pve_vm_test_case:
|
|
name: state-choice
|
|
tasks_from: main
|
|
expect: pve_vm_state must be one of
|
|
|
|
- name: Refuse a run without an API user
|
|
ansible.builtin.include_tasks: tasks/failed_run.yml
|
|
vars:
|
|
pve_vm_api_user: ""
|
|
pve_vm_test_case:
|
|
name: no-api-user
|
|
tasks_from: main
|
|
expect: The Proxmox API calls need pve_vm_api_user
|
|
|
|
- name: Refuse a run without an API token secret
|
|
ansible.builtin.include_tasks: tasks/failed_run.yml
|
|
vars:
|
|
pve_vm_api_token_secret: ""
|
|
pve_vm_test_case:
|
|
name: no-token-secret
|
|
tasks_from: main
|
|
expect: The Proxmox API calls need pve_vm_api_user
|
|
|
|
- name: Refuse a deletion without an API token secret
|
|
ansible.builtin.include_tasks: tasks/failed_run.yml
|
|
vars:
|
|
pve_vm_state: absent
|
|
pve_vm_api_token_secret: ""
|
|
pve_vm_test_case:
|
|
name: absent-no-token-secret
|
|
tasks_from: validate
|
|
expect: The Proxmox API calls need pve_vm_api_user
|
|
|
|
- name: Refuse a managed disk without a size
|
|
ansible.builtin.include_tasks: tasks/failed_run.yml
|
|
vars:
|
|
pve_vm_test_case:
|
|
name: no-disk-size
|
|
tasks_from: validate
|
|
expect: pve_vm_disk_size (for example 32G) must be set
|
|
|
|
- name: Refuse a VM id that is not a number
|
|
ansible.builtin.include_tasks: tasks/failed_run.yml
|
|
vars:
|
|
pve_vm_vmid: not-a-number
|
|
pve_vm_test_case:
|
|
name: vmid-type
|
|
tasks_from: main
|
|
expect: pve_vm_vmid
|
|
|
|
# create.yml is reached only after the API lookup found no VM; its assert precedes the clone.
|
|
- name: Refuse a clone without a template id
|
|
ansible.builtin.include_tasks: tasks/failed_run.yml
|
|
vars:
|
|
pve_vm_test_case:
|
|
name: no-clone-vmid
|
|
tasks_from: create
|
|
expect: which needs pve_vm_clone_vmid
|
|
|
|
- name: Accept valid input
|
|
hosts: all
|
|
gather_facts: false
|
|
tasks:
|
|
- name: Accept an unmanaged disk without a size
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.pve_vm
|
|
tasks_from: validate
|
|
vars:
|
|
pve_vm_manage_disk: false
|
|
|
|
- name: Accept a deletion without a disk size
|
|
ansible.builtin.include_role:
|
|
name: mortis.kamino.pve_vm
|
|
tasks_from: validate
|
|
vars:
|
|
pve_vm_state: absent
|
|
|
|
- name: Detect drift on a seeded VM config
|
|
hosts: all
|
|
gather_facts: false
|
|
vars:
|
|
pve_vm_test_no_drift:
|
|
forced: false
|
|
missing_keys: false
|
|
name: false
|
|
cpu: false
|
|
resources: false
|
|
ciuser: false
|
|
ipconfig: false
|
|
nameservers: false
|
|
onboot: false
|
|
agent: false
|
|
network: false
|
|
needs_update: false
|
|
tasks:
|
|
- name: Report no drift when the live config matches
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_test_case:
|
|
name: match
|
|
config: "{{ pve_vm_test_config }}"
|
|
expect: "{{ pve_vm_test_no_drift }}"
|
|
|
|
- name: Ignore the live ciuser while pve_vm_ci_user is empty
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_ci_user: ""
|
|
pve_vm_test_case:
|
|
name: ciuser-empty
|
|
config: "{{ pve_vm_test_config | combine({'ciuser': 'someone-else'}) }}"
|
|
expect: "{{ pve_vm_test_no_drift }}"
|
|
|
|
- name: Ignore a missing live ciuser while pve_vm_ci_user is empty
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_ci_user: ""
|
|
pve_vm_test_case:
|
|
name: ciuser-empty-unset
|
|
config: "{{ pve_vm_test_config | dict2items | rejectattr('key', 'eq', 'ciuser') | items2dict }}"
|
|
expect: "{{ pve_vm_test_no_drift }}"
|
|
|
|
- name: Report a live ciuser other than pve_vm_ci_user
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_ci_user: admin
|
|
pve_vm_test_case:
|
|
name: ciuser-changed
|
|
config: "{{ pve_vm_test_config }}"
|
|
expect: "{{ pve_vm_test_no_drift | combine({'ciuser': true, 'needs_update': true}) }}"
|
|
|
|
- name: Report a live config without ciuser
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_test_case:
|
|
name: ciuser-missing
|
|
config: "{{ pve_vm_test_config | dict2items | rejectattr('key', 'eq', 'ciuser') | items2dict }}"
|
|
expect: "{{ pve_vm_test_no_drift | combine({'missing_keys': true, 'ciuser': true, 'needs_update': true}) }}"
|
|
|
|
- name: Report a live config without sshkeys
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_test_case:
|
|
name: sshkeys-missing
|
|
config: "{{ pve_vm_test_config | dict2items | rejectattr('key', 'eq', 'sshkeys') | items2dict }}"
|
|
expect: "{{ pve_vm_test_no_drift | combine({'missing_keys': true, 'needs_update': true}) }}"
|
|
|
|
# Proxmox stores sshkeys URL-encoded, so a rotated key is not drift; pve_vm_force_update pushes it.
|
|
- name: Report no drift for rotated sshkeys
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_sshkeys: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIRotatedTestKeyNotARealKey molecule
|
|
pve_vm_test_case:
|
|
name: sshkeys-rotated
|
|
config: "{{ pve_vm_test_config }}"
|
|
expect: "{{ pve_vm_test_no_drift }}"
|
|
|
|
- name: Expect no sshkeys in the live config while pve_vm_sshkeys is empty
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_sshkeys: ""
|
|
pve_vm_test_case:
|
|
name: sshkeys-empty
|
|
config: "{{ pve_vm_test_config | dict2items | rejectattr('key', 'eq', 'sshkeys') | items2dict }}"
|
|
expect: "{{ pve_vm_test_no_drift }}"
|
|
|
|
- name: Report forced drift with pve_vm_force_update
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_force_update: true
|
|
pve_vm_test_case:
|
|
name: forced
|
|
config: "{{ pve_vm_test_config }}"
|
|
expect: "{{ pve_vm_test_no_drift | combine({'forced': true, 'needs_update': true}) }}"
|
|
|
|
- name: Read Proxmox's other forms as no drift
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_nameservers: 192.0.2.53,198.51.100.53
|
|
pve_vm_test_case:
|
|
name: other-forms
|
|
config: "{{ pve_vm_test_config | combine({'cpu': 'cputype=host,flags=+aes', 'agent': '1'}) }}"
|
|
expect: "{{ pve_vm_test_no_drift }}"
|
|
|
|
- name: Report drift in each compared dimension
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_test_case:
|
|
name: all-dimensions
|
|
config: >-
|
|
{{ pve_vm_test_config | combine({'name': 'other-name', 'cpu': 'kvm64', 'memory': 1024,
|
|
'ipconfig0': 'ip=192.0.2.31/24,gw=192.0.2.1', 'nameserver': '192.0.2.53', 'onboot': 0,
|
|
'agent': 'enabled=0', 'net0': 'virtio=BC:24:11:00:00:01,bridge=vmbr1,tag=20'}) }}
|
|
expect:
|
|
forced: false
|
|
missing_keys: false
|
|
name: true
|
|
cpu: true
|
|
resources: true
|
|
ciuser: false
|
|
ipconfig: true
|
|
nameservers: true
|
|
onboot: true
|
|
agent: true
|
|
network: true
|
|
needs_update: true
|
|
|
|
- name: Report a live VLAN tag that the inputs drop
|
|
ansible.builtin.include_tasks: tasks/drift_case.yml
|
|
vars:
|
|
pve_vm_network:
|
|
net0:
|
|
model: virtio
|
|
bridge: vmbr0
|
|
firewall: true
|
|
pve_vm_test_case:
|
|
name: tag-dropped
|
|
config: "{{ pve_vm_test_config }}"
|
|
expect: "{{ pve_vm_test_no_drift | combine({'network': true, 'needs_update': true}) }}"
|
|
|
|
# Static: the role must never send a cloud-init password, and its delete of a stored one must not
|
|
# log the task, so these read the role's own task files on the controller.
|
|
- name: Verify the role's cloud-init password handling
|
|
hosts: localhost
|
|
gather_facts: false
|
|
vars:
|
|
pve_vm_test_task_dir: "{{ (playbook_dir ~ '/../../tasks') | realpath }}"
|
|
# Every name a task can call the module by.
|
|
pve_vm_test_kvm_names:
|
|
- community.proxmox.proxmox_kvm
|
|
- community.general.proxmox_kvm
|
|
- ansible.legacy.proxmox_kvm
|
|
- proxmox_kvm
|
|
tasks:
|
|
- name: Find the role's task files, subdirectories included
|
|
ansible.builtin.find:
|
|
paths: "{{ pve_vm_test_task_dir }}"
|
|
patterns: ["*.yml", "*.yaml"]
|
|
recurse: true
|
|
register: pve_vm_test_files
|
|
|
|
- name: Read the role's task files
|
|
ansible.builtin.set_fact:
|
|
pve_vm_test_pending: >-
|
|
{{ [pve_vm_test_pending | default([]),
|
|
lookup('ansible.builtin.file', item) | from_yaml] | flatten(levels=1) }}
|
|
loop: "{{ pve_vm_test_files.files | map(attribute='path') | sort }}"
|
|
|
|
- name: Start the flattened task list
|
|
ansible.builtin.set_fact:
|
|
pve_vm_test_tasks: []
|
|
|
|
# Recurses through block, rescue and always to any depth.
|
|
- name: Flatten the blocks
|
|
ansible.builtin.include_tasks: tasks/flatten_tasks.yml
|
|
|
|
# A task calls the module by its key, or through action or local_action, either a mapping
|
|
# with module or a string that starts with the module name. Its args are merged in.
|
|
- name: Summarise each proxmox_kvm task
|
|
ansible.builtin.set_fact:
|
|
pve_vm_test_kvm: >-
|
|
{{ pve_vm_test_kvm | default([]) + [{
|
|
'name': item.name | default('(unnamed)'),
|
|
'sends': ('cipassword' in pve_vm_test_args)
|
|
or (pve_vm_test_raw is string and 'cipassword=' in pve_vm_test_raw),
|
|
'deletes': ('cipassword' in (pve_vm_test_args.delete | default('') | string).split(','))
|
|
or (pve_vm_test_raw is string and pve_vm_test_raw is search('delete=\\S*cipassword')),
|
|
'no_log': item.no_log | default(false) | bool,
|
|
}] }}
|
|
vars:
|
|
pve_vm_test_key: "{{ pve_vm_test_kvm_names | select('in', item) | first | default('') }}"
|
|
pve_vm_test_action: "{{ item.action | default(item.local_action | default('')) }}"
|
|
pve_vm_test_action_kvm: >-
|
|
{{ (pve_vm_test_action is mapping and pve_vm_test_action.module | default('') in pve_vm_test_kvm_names)
|
|
or (pve_vm_test_action is string and (pve_vm_test_action.split() | first | default('')) in pve_vm_test_kvm_names) }}
|
|
pve_vm_test_raw: >-
|
|
{{ item[pve_vm_test_key] if pve_vm_test_key
|
|
else (pve_vm_test_action | dict2items | rejectattr('key', 'eq', 'module') | items2dict
|
|
if pve_vm_test_action is mapping else pve_vm_test_action) }}
|
|
pve_vm_test_args: >-
|
|
{{ (pve_vm_test_raw if pve_vm_test_raw is mapping else {}) | combine(item.args | default({})) }}
|
|
loop: "{{ pve_vm_test_tasks }}"
|
|
loop_control:
|
|
label: "{{ item.name | default('(unnamed)') }}"
|
|
when: pve_vm_test_key | length > 0 or pve_vm_test_action_kvm | bool
|
|
|
|
- name: Assert no proxmox_kvm task sends cipassword
|
|
ansible.builtin.assert:
|
|
that:
|
|
- pve_vm_test_kvm | default([]) | length > 0
|
|
- pve_vm_test_kvm | selectattr('sends') | list | length == 0
|
|
fail_msg: "proxmox_kvm tasks sending cipassword: {{ pve_vm_test_kvm | default([]) | selectattr('sends') | map(attribute='name') | list }}"
|
|
quiet: true
|
|
|
|
# Any module, any spelling or group: no default the role sets may carry cipassword.
|
|
- name: Assert no module_defaults sets cipassword
|
|
ansible.builtin.assert:
|
|
that:
|
|
- >-
|
|
[item.module_defaults] | flatten(levels=1) | map('dict2items') | flatten(levels=1)
|
|
| map(attribute='value') | select('contains', 'cipassword') | list | length == 0
|
|
fail_msg: "module_defaults with cipassword on '{{ item.name | default('(unnamed)') }}'"
|
|
quiet: true
|
|
loop: "{{ pve_vm_test_tasks | selectattr('module_defaults', 'defined') | list }}"
|
|
loop_control:
|
|
label: "{{ item.name | default('(unnamed)') }}"
|
|
|
|
- name: Assert the stored cipassword is deleted by one no_log task
|
|
ansible.builtin.assert:
|
|
that:
|
|
- pve_vm_test_kvm | selectattr('deletes') | list | length == 1
|
|
- pve_vm_test_kvm | selectattr('deletes') | selectattr('no_log') | list | length == 1
|
|
fail_msg: "Tasks deleting cipassword: {{ pve_vm_test_kvm | selectattr('deletes') | list }}"
|
|
quiet: true
|