refactor(ansible): trim VM provisioning checks

This commit is contained in:
Hermes Agent committed 2026-11-09 14:09:35 +00:00
1 parent ef9b4454ff
commit 9b015b6987
2 files changed
+9 -64

No files matched your search

+8 -63
View File
@@ -21,29 +21,12 @@
api_token_secret: "{{ proxmox_api_token_secret }}"
validate_certs: true
tasks:
- name: Validate inputs before using the API
- name: Validate hostname and DNS mapping before using the API
ansible.builtin.assert:
that:
- proxmox_api_host is defined and proxmox_api_host | length > 0
- vm_template_id is defined and vm_template_id | string is match('^[0-9]+$')
- vm_hostname is defined and vm_hostname is match('^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$')
- vm_storage is defined and vm_storage | length > 0
- vm_vlan is defined and vm_vlan is match('^[a-zA-Z0-9_.-]+$')
- vm_vlan in vm_dns_domains
- vm_user is defined and vm_user | length > 0
- vm_ssh_public_key is defined and vm_ssh_public_key | trim | length > 0
- vm_cpu | string is match('^[0-9]+$') and vm_cpu | int > 0
- vm_ram_gib | string is match('^[0-9]+$') and vm_ram_gib | int > 0
- vm_disk_gib | string is match('^[0-9]+$') and vm_disk_gib | int > 0
fail_msg: Required VM inputs are invalid (short hostname, known VLAN/DNS suffix and public keys required); no VM was created.
- name: Validate API token inputs
ansible.builtin.assert:
that:
- proxmox_api_token_id is defined and proxmox_api_token_id is match('^[^!]+![^!]+$')
- proxmox_api_token_secret is defined and proxmox_api_token_secret | length > 0
fail_msg: Configure a Proxmox API token ID and secret in the Semaphore variable group secrets.
no_log: true
fail_msg: A short hostname and a known VLAN/DNS suffix are required; no VM was created.
- name: Look up requested hostname across the cluster
community.proxmox.proxmox_vm_info:
@@ -64,21 +47,16 @@
register: source_vm
no_log: true
- name: Require exactly one source template before reading its configuration
ansible.builtin.assert:
that:
- source_vm.proxmox_vms | length == 1
fail_msg: "Template {{ vm_template_id }} was missing or ambiguous in the cluster; no VM was created."
- name: Require a cloud-init-ready template on its current node
ansible.builtin.assert:
that:
- source_vm.proxmox_vms | length == 1
- source_vm.proxmox_vms[0].node is defined
- source_vm.proxmox_vms[0].template | bool
- source_vm.proxmox_vms[0].config.scsi0 is defined
- source_vm.proxmox_vms[0].config.scsi1 is defined
- "'cloudinit' in source_vm.proxmox_vms[0].config.scsi1"
fail_msg: "Template {{ vm_template_id }} must be a QEMU template with a node, scsi0 and a scsi1 cloud-init drive; no VM was created."
fail_msg: "Template {{ vm_template_id }} must resolve uniquely to a QEMU template with a node, scsi0 and a scsi1 cloud-init drive; no VM was created."
- name: Clone full VM onto requested storage
community.proxmox.proxmox_kvm:
@@ -97,8 +75,6 @@
that:
- cloned_vm.changed
- cloned_vm.vmid is defined
- cloned_vm.vmid | string is match('^[0-9]+$')
- cloned_vm.vmid | int >= 100
- cloned_vm.vmid | int != vm_template_id | int
fail_msg: >-
Clone did not return a valid new VMID (possibly allocated concurrently);
@@ -164,10 +140,6 @@
ansible_ssh_timeout: 10
provisioned_vmid: "{{ cloned_vm.vmid }}"
- name: Report guest check start
ansible.builtin.debug:
msg: "VMID {{ cloned_vm.vmid }} started; DNS and SSH port reachable for {{ vm_hostname }}.{{ vm_dns_domains[vm_vlan] }}. Checking guest SSH, cloud-init and Ansible next."
- name: Verify cloud-init and Ansible on the new guest
hosts: provisioned_vm
gather_facts: false
@@ -192,34 +164,17 @@
- "stdout: {{ cloudinit_wait.stdout | trim }}"
- "stderr: {{ cloudinit_wait.stderr | trim }}"
- name: Require cloud-init wait to complete
ansible.builtin.assert:
that: cloudinit_wait.rc in [0, 2]
fail_msg: "VMID {{ provisioned_vmid }} cloud-init wait failed (rc {{ cloudinit_wait.rc }}); VM left intact for inspection."
- name: Read final cloud-init status
ansible.builtin.raw: cloud-init status --long
register: cloudinit_status
changed_when: false
failed_when: false
- name: Show final cloud-init diagnostics
ansible.builtin.debug:
msg:
- "stdout: {{ cloudinit_status.stdout | trim }}"
- "stderr: {{ cloudinit_status.stderr | trim }}"
- name: Require completed cloud-init
ansible.builtin.assert:
that:
- cloudinit_status.rc in [0, 2]
- "cloudinit_status.stdout is search('(?m)^status: done\\s*$')"
fail_msg: "VMID {{ provisioned_vmid }} cloud-init status is not complete (rc {{ cloudinit_status.rc }}); VM left intact for inspection."
- cloudinit_wait.rc in [0, 2]
- "cloudinit_wait.stdout is search('(?m)^status: done\\s*$')"
fail_msg: "VMID {{ provisioned_vmid }} cloud-init status is not complete (rc {{ cloudinit_wait.rc }}); VM left intact for inspection."
- name: Warn about degraded cloud-init completion
ansible.builtin.debug:
msg: "VMID {{ provisioned_vmid }} cloud-init completed with degraded status (rc 2); individual cloud-init operations may not have succeeded. Inspect the diagnostics before relying on this guest."
when: cloudinit_wait.rc == 2 or cloudinit_status.rc == 2
when: cloudinit_wait.rc == 2
- name: Read guest timezone
ansible.builtin.command: timedatectl show --property=Timezone --value
@@ -232,16 +187,6 @@
become: true
when: guest_timezone_before.stdout | trim != vm_timezone
- name: Verify guest timezone
ansible.builtin.command: timedatectl show --property=Timezone --value
register: guest_timezone_after
changed_when: false
- name: Require configured guest timezone
ansible.builtin.assert:
that: guest_timezone_after.stdout | trim == vm_timezone
fail_msg: "VMID {{ provisioned_vmid }} guest timezone did not become {{ vm_timezone }}; VM left intact for inspection."
- name: Verify final Ansible ping
ansible.builtin.ping: