Cloud-init enabled Ubuntu 20.04 template for Proxmox to facilitate automatic instance deploy by Terraform

Cloud-init enabled Ubuntu 20.04 template for Proxmox to facilitate automatic instance deploy by Terraform

Create Proxmox Template (Ubuntu 20.04 LTS)

Prepare Proxmox node for Template creation process

Working on Proxmox (RnD) IP: 172.16.7.41

Update/Upgrade all packages. And install libguestfs-tools package

sudo apt update
sudo apt upgrade -y

sudo apt install libguestfs-tools -y

Add current user to kvm group to be able to use the virt-edit command

sudo usermod -aG kvm <username>

Logout and login again and add an environment variable EDITOR which is used by virt-edit

export EDITOR=nano
printenv | grep EDITOR

Download and Prepare Ubuntu 20.04 LTS cloud image

Download Ubuntu 20.04 LTS cloud image

wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img -O ubuntu20.qcow2

Customize sshd_conf file for ssh configuration

virt-edit -a ubuntu20.qcow2 /etc/ssh/sshd_config

After nano opens up the sshd_config file, do the following changes, save the file and exit

  • UnComment PubkeyAuthentication yes
  • Change PasswordAuthentication yes

Prepare Cloud image before starting to create a new VM from it

Setup Time-Zone, Run update for all packages, Install additional packages

virt-customize -a ubuntu20.qcow2 --timezone "Asia/Dhaka"
virt-customize -a ubuntu20.qcow2 --update
virt-customize -a ubuntu20.qcow2 --install qemu-guest-agent
virt-customize -a ubuntu20.qcow2 --run-command 'sudo systemctl enable qemu-guest-agent'

Set password for root

read -sp "Password for root: " pwd && virt-customize -a ubuntu20.qcow2 --root-password password:$pwd

Create Proxmox Template from prepared cloud image

Create a VM with minimum configuration

sudo qm create 9202 --name "Ubuntu20-CI-Template" --memory 1024 --cores 1 --net0 virtio,bridge=vmbr0
sudo qm importdisk 9202 ubuntu20.qcow2 pve-block

Additional settings for the new VM

sudo qm set 9202 --scsihw virtio-scsi-pci --scsi0 pve-rbd:vm-9202-disk-0
sudo qm set 9202 --boot c --bootdisk scsi0
sudo qm set 9202 --ide2 pve-rbd:cloudinit
sudo qm set 9202 --sshkey .ssh/myKey.pub
sudo qm set 9202 --agent 1

Set cloud-init settings for the VM

sudo qm set 9202 --ciuser ubuntu --citype nocloud --nameserver 172.16.4.1 --ipconfig0 ip=dhcp --searchdomain cs.net
read -sp 'Password for ubuntu: ' pwd && sudo qm set 9202 --cipassword $pwd

Convert the new VM to template

sudo qm template 9202