Files
Homelab/ansible/proxmox/provision_vm.yml
T

251 lines
9.5 KiB
YAML

---
- name: Clone and start one cloud-init VM
hosts: localhost
connection: local
gather_facts: false
vars:
ansible_python_interpreter: "{{ ansible_playbook_python }}"
vm_cpu: 1
vm_ram_gib: 2
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 }}"
api_port: "{{ proxmox_api_port | default(8006) }}"
api_user: "{{ proxmox_api_token_id.split('!')[0] }}"
api_token_id: "{{ proxmox_api_token_id.split('!')[1] }}"
api_token_secret: "{{ proxmox_api_token_secret }}"
validate_certs: true
tasks:
- name: Validate inputs 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
- name: Look up requested hostname across the cluster
community.proxmox.proxmox_vm_info:
name: "{{ vm_hostname }}"
register: named_vm
no_log: true
- name: Reject an already used hostname
ansible.builtin.assert:
that: named_vm.proxmox_vms | length == 0
fail_msg: "Hostname {{ vm_hostname }} already exists; no VM was modified. Inspect it before retrying."
- name: Look up source template and its current configuration
community.proxmox.proxmox_vm_info:
vmid: "{{ vm_template_id }}"
type: qemu
config: current
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[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."
- name: Clone full VM onto requested storage
community.proxmox.proxmox_kvm:
clone: source-template
vmid: "{{ vm_template_id }}"
name: "{{ vm_hostname }}"
node: "{{ source_vm.proxmox_vms[0].node }}"
storage: "{{ vm_storage }}"
full: true
state: present
register: cloned_vm
no_log: true
- name: Stop if cloning did not allocate a new VMID
ansible.builtin.assert:
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);
no configuration was attempted. Inspect the cluster before retrying.
- name: Configure VM before first boot
community.proxmox.proxmox_kvm:
vmid: "{{ cloned_vm.vmid }}"
node: "{{ source_vm.proxmox_vms[0].node }}"
update: true
update_unsafe: true # Needed to replace net0 on this newly cloned VM.
cores: "{{ vm_cpu }}"
memory: "{{ vm_ram_gib | int * 1024 }}"
net:
net0: "virtio,bridge={{ vm_vlan }}"
ipconfig:
ipconfig0: "{{ vm_ipconfig0 }}"
ciuser: "{{ vm_user }}"
sshkeys: "{{ vm_ssh_public_key }}"
state: present
no_log: true
- name: Grow cloned scsi0 disk to requested size
community.proxmox.proxmox_disk:
vmid: "{{ cloned_vm.vmid }}"
disk: scsi0
size: "{{ vm_disk_gib }}G"
state: resized
no_log: true
- name: Start configured VM
community.proxmox.proxmox_kvm:
vmid: "{{ cloned_vm.vmid }}"
state: started
no_log: true
- 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 }} 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
vars:
vm_timezone: Europe/Paris
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 --long
register: cloudinit_wait
changed_when: false
failed_when: false
- name: Show cloud-init wait diagnostics
ansible.builtin.debug:
msg:
- "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."
- 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
- name: Read guest timezone
ansible.builtin.command: timedatectl show --property=Timezone --value
register: guest_timezone_before
changed_when: false
- name: Set guest display timezone
ansible.builtin.command:
argv: [timedatectl, set-timezone, "{{ vm_timezone }}"]
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:
- name: Report verified guest readiness
ansible.builtin.debug:
msg: "VMID {{ provisioned_vmid }} at {{ inventory_hostname }} completed cloud-init and returned Ansible pong."