Create Template

Create a template using Ubuntu Cloud Image and customization script

Customization script for Ubuntu Cloud image

Docker Compose File (ubuntu.yml)

Once the snippets has been added to the local location you can cd to the /var/lib/snippets directory

code
cd /var/lib/vz/snippets/

Next using nano create the ubuntu.yaml file

code
nano ubuntu.yaml

copy and paste the following code

#cloud-config
runcmd:
    - apt update
    - apt install -y qemu-guest-agent
    - systemctl enable ssh
    - apt install -y apt-transport-https ca-certificates curl software-properties-common
    - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    - echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu noble stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
    - apt update
    - apt-cache policy docker-ce
    - apt install -y docker-ce
    - systemctl status docker
    - usermod -aG docker mbtech
    - echo "mbtech ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
    - mkdir -p ~/.docker/cli-plugins/
    - curl -SL https://github.com/docker/compose/releases/download/v2.36.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
    - chmod +x ~/.docker/cli-plugins/docker-compose
# Taken from https://forum.proxmox.com/threads/combining-custom-cloud-init-with-auto-generated.59008/page-3#post-428772

Next need to create the bash script to create the template

code
nano create-template.sh
#! /bin/bash

VMID=<Your ID you would Like to use>
STORAGE=<Your storage location>
USER=<your username>
set -x
rm -f noble-server-cloudimg-amd64.img
wget -q https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
qemu-img resize noble-server-cloudimg-amd64.img 8G
qm destroy $VMID
qm create $VMID --name "ubuntu-noble-template-5" --ostype l26 \
    --memory 1024 --balloon 0 \
    --agent 1 \
    --bios ovmf --machine q35 --efidisk0 $STORAGE:0,pre-enrolled-keys=0 \
    --cpu host --socket 1 --cores 1 \
    --vga serial0 --serial0 socket  \
    --net0 virtio,bridge=vmbr0
qm importdisk $VMID noble-server-cloudimg-amd64.img $STORAGE
qm set $VMID --scsihw virtio-scsi-pci --virtio0 $STORAGE:$VMID/vm-$VMID-disk-1.raw,discard=on
qm set $VMID --boot order=virtio0
qm set $VMID --scsi1 $STORAGE:cloudinit

cat << EOF | tee /var/lib/vz/snippets/ubuntu.yaml
#cloud-config
runcmd:
    - apt update
    - apt install -y qemu-guest-agent
    - systemctl enable ssh
    - apt install -y apt-transport-https ca-certificates curl software-properties-common
    - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    - echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu noble stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
    - apt update
    - apt-cache policy docker-ce
    - apt install -y docker-ce
    - systemctl status docker
    - usermod -aG docker mbtech
    - echo "mbtech ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
    - mkdir -p ~/.docker/cli-plugins/
    - curl -SL https://github.com/docker/compose/releases/download/v2.36.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
    - chmod +x ~/.docker/cli-plugins/docker-compose
# Taken from https://forum.proxmox.com/threads/combining-custom-cloud-init-with-auto-generated.59008/page-3#post-428772
EOF

qm set $VMID --cicustom "vendor=local:snippets/ubuntu.yaml"
qm set $VMID --tags ubuntu-template,noble,cloudinit
qm set $VMID --ciuser $USER
qm set $VMID --sshkeys ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIL7OapWuqeqK4mOF8LEJsBunfNUKJSpXmWkqACGCcQ4y mbtech@ubuntusemaphore
qm set $VMID --ipconfig0 ip=dhcp
qm template $VMID

Reminder you need to change the variables!!!

Youtube Video