infra/ansible/create-vm-template.yml

107 lines
No EOL
3.5 KiB
YAML

---
# Create a cloud-init template on Proxmox for K8s nodes
# This playbook runs commands directly on the Proxmox host
- name: Create cloud-init template on Proxmox
hosts: proxmox[0]
become: yes
vars:
template_vmid: 9000
template_name: debian12-cloud-init
debian_image_url: https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2
storage: ceph
tasks:
- name: Check if template already exists
shell: |
qm status {{ template_vmid }} >/dev/null 2>&1 && echo "exists" || echo "missing"
register: template_check
changed_when: false
- name: Display template status
debug:
msg: "Template VM {{ template_vmid }} already exists. Skipping creation."
when: template_check.stdout == "exists"
- name: Install required packages
apt:
name:
- wget
- libguestfs-tools
state: present
update_cache: yes
when: template_check.stdout == "missing"
- name: Download Debian cloud image
get_url:
url: "{{ debian_image_url }}"
dest: /var/lib/vz/template/iso/debian-12-genericcloud-amd64.qcow2
mode: '0644'
when: template_check.stdout == "missing"
- name: Create VM for template
shell: |
qm create {{ template_vmid }} --name {{ template_name }} --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0
args:
creates: /etc/pve/qemu-server/{{ template_vmid }}.conf
when: template_check.stdout == "missing"
- name: Import disk image to VM
shell: |
qm importdisk {{ template_vmid }} /var/lib/vz/template/iso/debian-12-genericcloud-amd64.qcow2 {{ storage }}
register: import_result
changed_when: "'successfully imported' in import_result.stdout"
when: template_check.stdout == "missing"
- name: Get imported disk name
shell: |
pvesm list {{ storage }} | grep "vm-{{ template_vmid }}-disk-" | awk '{print $1}' | head -1
register: disk_name
when: template_check.stdout == "missing"
- name: Attach imported disk to VM
shell: |
qm set {{ template_vmid }} --scsihw virtio-scsi-pci --scsi0 {{ disk_name.stdout }}
when:
- template_check.stdout == "missing"
- disk_name.stdout != ""
- name: Check if cloud-init drive exists
shell: |
qm config {{ template_vmid }} | grep -q 'ide2:.*cloudinit' && echo "exists" || echo "missing"
register: cloudinit_check
changed_when: false
when: template_check.stdout == "missing"
- name: Add cloud-init drive
shell: |
qm set {{ template_vmid }} --ide2 {{ storage }}:cloudinit
when:
- template_check.stdout == "missing"
- cloudinit_check.stdout == "missing"
- name: Set boot disk
shell: |
qm set {{ template_vmid }} --boot c --bootdisk scsi0
when: template_check.stdout == "missing"
- name: Add serial console
shell: |
qm set {{ template_vmid }} --serial0 socket --vga serial0
when: template_check.stdout == "missing"
- name: Enable QEMU guest agent
shell: |
qm set {{ template_vmid }} --agent enabled=1
when: template_check.stdout == "missing"
- name: Convert VM to template
shell: |
qm template {{ template_vmid }}
when: template_check.stdout == "missing"
- name: Clean up downloaded image
file:
path: /var/lib/vz/template/iso/debian-12-genericcloud-amd64.qcow2
state: absent
when: template_check.stdout == "missing"