diff --git a/ansible/proxmox/README.md b/ansible/proxmox/README.md index 1326fb8..f39b1d0 100644 --- a/ansible/proxmox/README.md +++ b/ansible/proxmox/README.md @@ -16,4 +16,4 @@ In a Semaphore Variable Group, set fixed `proxmox_api_host` (hostname, not URL), Only `vlan66` maps to `lab.vezpi.com` in the playbook; unknown VLANs fail before API provisioning. `vm_hostname` must be a single short DNS label (no dots). DHCP/DNS must publish `.lab.vezpi.com` for the chosen VNet; a derived name is not proof of a current record. Stale/reused DNS can point at another host: verify record ownership and the new guest's SSH host key out of band before running. Never trust an unverified `ssh-keyscan` result or disable host-key checking. -Use a template with bootable `scsi0`, a `scsi1` cloud-init drive, Python 3, `cloud-init`, `timeout`, SSH and a reachable guest network. Select an SSH Key Store **user credential on the task inventory** whose private key matches one authorized public key for `vm_user`; the same Ansible invocation's inventory credential must be available to the dynamically added guest (verify this in the installed Semaphore version). The localhost API token is separate. Pre-enroll a verified guest host key in the runner's trusted `known_hosts` using a Captain-approved mechanism; no private key path or trust bypass is configured here. DNS, SSH port, Python-capable Ansible connection, cloud-init wait/status and final Ansible ping are bounded stages. The connection check retries transient transport failures within its timeout; Python 3 is required in the image. Completed cloud-init status accepts rc 0 or degraded rc 2, prints `--long` diagnostics and warns on rc 2; other codes, incomplete status and timeouts fail. A failed or timed-out VM remains for Captain to inspect, not delete. After accepted cloud-init completion, the guest timezone is verified as `Europe/Paris` (CET/CEST); RTC and NTP are untouched. The guest needs `timedatectl`, timezone data (`tzdata`), and authorized noninteractive sudo/become for `vm_user` when a timezone change is needed. If these are unavailable, readiness fails; no sudo configuration, package installation or reboot is added. +Use a template with bootable `scsi0`, a `scsi1` cloud-init drive, Python 3, `cloud-init`, `timeout`, SSH and a reachable guest network. Select an SSH Key Store **user credential on the task inventory** whose private key matches one authorized public key for `vm_user`; the same Ansible invocation's inventory credential must be available to the dynamically added guest (verify this in the installed Semaphore version). The localhost API token is separate. Pre-enroll a verified guest host key in the runner's trusted `known_hosts` using a Captain-approved mechanism; no private key path or trust bypass is configured here. DNS, SSH port, Python-capable Ansible connection, cloud-init wait/status and final Ansible ping are bounded stages. The connection check retries transient transport failures within its timeout; Python 3 is required in the image. Completed cloud-init status accepts rc 0 or degraded rc 2, prints `--long` diagnostics and warns on rc 2; other codes, incomplete status and timeouts fail. A failed or timed-out VM remains for Captain to inspect, not delete. After accepted cloud-init completion, the guest timezone is read and, if needed, set to `Europe/Paris` (CET/CEST); RTC and NTP are untouched. The guest needs `timedatectl`, timezone data (`tzdata`), and authorized noninteractive sudo/become for `vm_user` when a timezone change is needed. If these are unavailable, readiness fails; no sudo configuration, package installation or reboot is added. diff --git a/ansible/proxmox/provision_vm.yml b/ansible/proxmox/provision_vm.yml index d395cef..23d9ed3 100644 --- a/ansible/proxmox/provision_vm.yml +++ b/ansible/proxmox/provision_vm.yml @@ -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: