Files
homelab/plays/create-template.yml
T

80 lines
2.3 KiB
YAML

---
- name: Create Ubuntu 24.04 Template
hosts: saturn
become: true
vars:
template_id: 605
template_name: ubuntu-24.04-template
image_url: https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
image_path: /tmp/noble-server-cloudimg-amd64.img
tasks:
- name: Install libguestfs-tools
apt:
name: libguestfs-tools
state: present
- name: Download Ubuntu 24.04 Cloud Image
get_url:
url: "{{ image_url }}"
dest: "{{ image_path }}"
mode: '0644'
force: true
- name: Customize Image (Install Agent & Update)
command: >
virt-customize -a {{ image_path }}
--install qemu-guest-agent
--update
register: virt_customize
changed_when: true
- name: Destroy existing template if exists
command: "qm destroy {{ template_id }}"
ignore_errors: true
changed_when: false
- name: Create VM
command: >
qm create {{ template_id }}
--name {{ template_name }}
--memory 2048
--cores 2
--net0 virtio,bridge=v40srv
changed_when: true
- name: Import Disk
command: "qm importdisk {{ template_id }} {{ image_path }} cpool"
changed_when: true
- name: Create temp ssh key file
copy:
content: "{{ common_ssh_public_keys }}"
dest: /tmp/sshkeys.pub
- name: Configure VM Hardware
command: "{{ item }}"
loop:
- "qm set {{ template_id }} --scsihw virtio-scsi-pci --scsi0 cpool:vm-{{ template_id }}-disk-0"
- "qm set {{ template_id }} --boot c --bootdisk scsi0"
- "qm set {{ template_id }} --ide2 cpool:cloudinit"
- "qm set {{ template_id }} --serial0 socket --vga serial0"
- "qm set {{ template_id }} --agent enabled=1"
- "qm set {{ template_id }} --ipconfig0 ip=dhcp"
- "qm set {{ template_id }} --ciuser ladmin"
- "qm set {{ template_id }} --sshkeys /tmp/sshkeys.pub"
changed_when: true
- name: Resize Disk
command: "qm resize {{ template_id }} scsi0 32G"
changed_when: true
- name: Convert to Template
command: "qm template {{ template_id }}"
changed_when: true
- name: Cleanup Image
file:
path: "{{ image_path }}"
state: absent