# Create Template

## Create a template using Ubuntu Cloud Image and customization script

### Customization script (`ubuntu.yml`)

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

```
cd /var/lib/vz/snippets/
```
Next using nano create the ubuntu.yaml file

```
nano ubuntu.yaml
```

copy and paste the following code

```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=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 "<username> 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
```
::: info
# Reminder you need to change the user name for sudoers file:
```
echo "<username> ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
```
:::


Next need to create the bash script to create the template

```
cd ~
```
```
nano create-template.sh
```

```bash
#! /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
```
::: info
# Reminder you need to change the variables:
```
VMID=<Your ID you would Like to use>
STORAGE=<Your storage location>
USER=<your username>
```
:::

Make sure that the bash script is executable
```
chmod 755 create-template.sh
```
```
./create-template.sh
```

## Youtube Video

<iframe width="560" height="315" src="https://www.youtube.com/embed/DU195xLjQdA?si=3_uwn7UTtyqxTX2V" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9