diff --git a/ansible/proxmox/README.md b/ansible/proxmox/README.md index 897f13c..a31aab3 100644 --- a/ansible/proxmox/README.md +++ b/ansible/proxmox/README.md @@ -1,10 +1,10 @@ # Provision one Proxmox VM with Semaphore -Create an Ansible task template for `ansible/proxmox/provision_vm.yml` with a localhost inventory (`localhost ansible_connection=local`). It clones a cloud-init QEMU template, configures the VM and starts it. +Create an Ansible task template for `ansible/proxmox/provision_vm.yml` with a localhost inventory (`localhost ansible_connection=local`). It discovers the template's current node via the cluster API, clones once, configures before starting, then checks the guest by FQDN. The Semaphore server gets `proxmoxer>=2.3,<3` and `requests>=2.31,<3` from `docker/semaphore/requirements.txt` (mounted from `/appli/docker/semaphore/requirements.txt` to `/etc/semaphore/requirements.txt`); the image installs these on container startup, not merely on mount. Semaphore automatically installs the adjacent `ansible/proxmox/requirements.yml` (`community.proxmox:2.0.0`) unless **Skip Galaxy install** is enabled. -In a Semaphore Variable Group, set fixed `proxmox_api_host` (hostname, not URL), `proxmox_node`, `vm_template_id`, `vm_storage`, `vm_user`, and `vm_ssh_public_key` (public key only). Set `proxmox_api_token_id` (full `user@realm!token`) and `proxmox_api_token_secret` in the group's **Secrets** tab, not the survey. Optional fixed values: `proxmox_api_port` (default 8006) and `vm_ipconfig0` (default `ip=dhcp`). Trust the Proxmox CA in the runner; TLS verification stays enabled. +In a Semaphore Variable Group, set fixed `proxmox_api_host` (hostname, not URL), `vm_template_id`, `vm_storage`, `vm_user`, and `vm_ssh_public_key` (public keys only). One key works as before; multiple keys are newline-delimited, one complete public key per line (for example, `ssh-ed25519 ` followed by `ssh-ed25519 ` on the next line). Include the public key corresponding to the guest SSH credential selected for Semaphore. Set `proxmox_api_token_id` (full `user@realm!token`) and `proxmox_api_token_secret` in the group's **Secrets** tab, not the survey. Optional fixed values: `proxmox_api_port` (default 8006) and `vm_ipconfig0` (default `ip=dhcp`). Trust the Proxmox CA in the runner; TLS verification stays enabled. | Survey variable | Default | | --- | --- | @@ -14,4 +14,6 @@ In a Semaphore Variable Group, set fixed `proxmox_api_host` (hostname, not URL), | `vm_disk_gib` | `4` GiB, absolute disk size | | `vm_vlan` | `vlan66` bridge/VNet name | -Use a template with bootable `scsi0` and a `scsi1` cloud-init drive; partial failures are not automatically deleted, and starting the VM does not guarantee guest cloud-init readiness. +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. Nonzero cloud-init status fails; a failed or timed-out VM remains for Captain to inspect, not delete. No guest agent, package installation, sudo changes or reboot are added. diff --git a/ansible/proxmox/provision_vm.yml b/ansible/proxmox/provision_vm.yml index 5a4ee10..63385f9 100644 --- a/ansible/proxmox/provision_vm.yml +++ b/ansible/proxmox/provision_vm.yml @@ -10,6 +10,8 @@ vm_disk_gib: 4 vm_vlan: vlan66 vm_ipconfig0: ip=dhcp + vm_dns_domains: + vlan66: lab.vezpi.com module_defaults: group/community.proxmox.proxmox: api_host: "{{ proxmox_api_host }}" @@ -23,17 +25,17 @@ ansible.builtin.assert: that: - proxmox_api_host is defined and proxmox_api_host | length > 0 - - proxmox_node is defined and proxmox_node | 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.-]*$') + - 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 | 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 missing or invalid; no VM was created. + 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: @@ -62,23 +64,28 @@ register: source_vm no_log: true - - name: Require a cloud-init-ready template on the selected node + - name: Require exactly one source template before reading its configuration ansible.builtin.assert: that: - source_vm.proxmox_vms | length == 1 - - source_vm.proxmox_vms[0].node == proxmox_node + 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[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 on {{ proxmox_node }} with scsi0 and a scsi1 cloud-init drive; no VM was created." + 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." - name: Clone full VM onto requested storage community.proxmox.proxmox_kvm: clone: source-template vmid: "{{ vm_template_id }}" name: "{{ vm_hostname }}" - node: "{{ proxmox_node }}" + node: "{{ source_vm.proxmox_vms[0].node }}" storage: "{{ vm_storage }}" full: true state: present @@ -100,7 +107,7 @@ - name: Configure VM before first boot community.proxmox.proxmox_kvm: vmid: "{{ cloned_vm.vmid }}" - node: "{{ proxmox_node }}" + node: "{{ source_vm.proxmox_vms[0].node }}" update: true update_unsafe: true # Needed to replace net0 on this newly cloned VM. cores: "{{ vm_cpu }}" @@ -128,6 +135,76 @@ state: started no_log: true - - name: Report submitted VMID + - name: Wait for guest DNS publication + ansible.builtin.command: + argv: + - getent + - ahosts + - "{{ vm_hostname }}.{{ vm_dns_domains[vm_vlan] }}" + register: guest_dns + changed_when: false + until: guest_dns.rc == 0 and guest_dns.stdout | length > 0 + retries: 30 + delay: 5 + + - name: Wait for guest SSH port after DNS resolution + ansible.builtin.wait_for: + host: "{{ vm_hostname }}.{{ vm_dns_domains[vm_vlan] }}" + port: 22 + connect_timeout: 5 + timeout: 180 + sleep: 5 + + - name: Add new guest to ephemeral inventory + ansible.builtin.add_host: + name: "{{ vm_hostname }}.{{ vm_dns_domains[vm_vlan] }}" + groups: provisioned_vm + ansible_connection: ssh + ansible_user: "{{ vm_user }}" + ansible_ssh_timeout: 10 + provisioned_vmid: "{{ cloned_vm.vmid }}" + + - name: Report guest check start ansible.builtin.debug: - msg: "VMID {{ cloned_vm.vmid }} start requested; cloud-init completion is not checked." + 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 + tasks: + - name: Wait for Ansible connection with Python + ansible.builtin.wait_for_connection: + connect_timeout: 10 + sleep: 5 + timeout: 180 + + - name: Wait for cloud-init with an external bound + ansible.builtin.raw: timeout 300 cloud-init status --wait + register: cloudinit_wait + changed_when: false + failed_when: false + + - name: Require cloud-init wait to succeed + ansible.builtin.assert: + that: cloudinit_wait.rc == 0 + 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 + register: cloudinit_status + changed_when: false + failed_when: false + + - name: Require successful completed cloud-init + ansible.builtin.assert: + that: + - cloudinit_status.rc == 0 + - "cloudinit_status.stdout is search('(?m)^status: done\\s*$')" + fail_msg: "VMID {{ provisioned_vmid }} cloud-init status is not successful (rc {{ cloudinit_status.rc }}); VM left intact for inspection." + + - name: Verify final Ansible ping + ansible.builtin.ping: + + - name: Report verified guest readiness + ansible.builtin.debug: + msg: "VMID {{ provisioned_vmid }} at {{ inventory_hostname }} completed cloud-init and returned Ansible pong."