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

91 lines
3.0 KiB
YAML

---
# Validation first, under always, so a bad input fails before any change whatever tags the run
# selects.
- name: Validate the role inputs
ansible.builtin.import_tasks: validate.yml
tags: always
# Every Proxmox API call of the role is inside this block, so the credentials are set once here
# instead of on each task; module_defaults set on a block reach tasks run through include_tasks.
# They are templated per task and no secret is stored as a fact. The uri call (detect_disk) takes
# host and header explicitly.
- name: Manage Proxmox VM lifecycle
module_defaults:
community.proxmox.proxmox_kvm:
api_user: "{{ pve_vm_api_user }}@{{ pve_vm_api_realm }}"
api_token_id: "{{ pve_vm_api_token_id }}"
api_token_secret: "{{ pve_vm_api_token_secret }}"
api_host: "{{ pve_vm_api_host }}"
validate_certs: false
community.proxmox.proxmox_disk:
api_user: "{{ pve_vm_api_user }}@{{ pve_vm_api_realm }}"
api_token_id: "{{ pve_vm_api_token_id }}"
api_token_secret: "{{ pve_vm_api_token_secret }}"
api_host: "{{ pve_vm_api_host }}"
validate_certs: false
ansible.builtin.uri:
validate_certs: false
block:
- name: Gather current VM information
ansible.builtin.include_tasks: detect.yml
- name: Remove VM if destruction is requested
ansible.builtin.include_tasks: delete.yml
when:
- pve_vm_state == "absent"
- pve_vm_allow_destroy | bool
- pve_vm_exists
- name: Stop processing for deleted VM
ansible.builtin.meta: end_host
when:
- pve_vm_state == "absent"
- pve_vm_allow_destroy | bool
- name: Create VM if it does not exist
ansible.builtin.include_tasks: create.yml
when:
- pve_vm_state != "absent"
- not pve_vm_exists
- not ansible_check_mode
- name: Preview VM creation
ansible.builtin.debug:
msg: >-
VM {{ pve_vm_vmid }} does not exist on {{ pve_vm_node }}: a real run clones it from template
{{ pve_vm_clone_vmid | default('(unset)') }} and then configures it. Nothing after the lookup runs for it
in this check.
changed_when: true
when:
- pve_vm_state != "absent"
- not pve_vm_exists
- ansible_check_mode
- name: Refresh VM information after changes
ansible.builtin.include_tasks: detect.yml
- name: Gather current VM disk information
ansible.builtin.include_tasks: detect_disk.yml
when:
- pve_vm_exists
- pve_vm_state != "absent"
- name: Update existing VM configuration
ansible.builtin.include_tasks: update.yml
when:
- pve_vm_state != "absent"
- pve_vm_exists
- name: Ensure VM power state
ansible.builtin.include_tasks: state.yml
when:
- pve_vm_exists
- pve_vm_state in ["started", "stopped"]
- name: Wait for VM to become ready
ansible.builtin.include_tasks: wait.yml
when:
- pve_vm_exists
- pve_vm_state == "started"
- pve_vm_wait_for_guest | bool