Create VM templates on Proxmox with Packer

Payam Saderi
3 min readAug 1, 2023

--

I have several servers and have been using Proxmox for virtualization on them. The best and fastest way to create a VM is to use a template. I need to have similar VM templates on all of my Proxmox servers and I hate to do same job over and over, so I started to see how I can escape from this.

Before writing this blog post, I didn’t know anything about Packer, I was just looking for a tool that I could use to easily create my own VM by setting a few variables, I only knew a few things:

  1. Proxmox API: I know that Proxmox has a API and I can manage everything on Proxmox with API
  2. Cloud-init: I used cloud-init to make some settings in VMs

3. Terraform: Is an open-source infrastructure-as-code software tool, I haven’t used much yet, but I’m getting more familiar with it

My first goal was to do this with Terrafrom, but in my searches I came across Packer the right tool for my problem. The Packer is also a product of Hashicorp.
After reading the document on the Hashicorp website, I was looking for a quick tutorial and found this video:

Well, it’s over, you can go and watch this video :). I am just kidding, keep reading.

The video and codes in the boilerplates repository that Christian made are great, but I encountered some problems that I will explain here and tell what I did to solve these problems.

When you run the packer build command, Packer creates a temporary web server on your system that Ubuntu’s autoinstall can use, but like many, I work remotely, so the VM on Proxmox will not access the web server that Packer is running on my system.

The first solution that came to my mind was to install the Packer in the VM in the network where the server is located and run the Packer commands through ssh. But I’m a fan of automation, so instead of using VM and ssh I preferred to use gitlab-ci and runner.

So I create this repo, In this repo, I tried to solve the problems I was facing.

I solved the problem of the autoinstall’s web server by setting the ip through a variable, I changed this part in boot_commnad :

http://{{ .HTTPIP }}:{{ .HTTPPort }}

to this:

http://${var.runner_ip_address}:{{ .HTTPPort }}

So I can define the ip through a variable.

The second problem was installing ubuntu 22.04, at the time the latest available version was 22.04.2, which caused problems during installation. I used version 22.04.1 to fix this problem and also disabled updates during installation. Check the user-data file in the ubuntu-server-jammy folder.

Well, if everything goes well, you can create your template and then use Terrafrom to have your VMs from it. Christia also has a video about this:

In the future, I may publish the Github Action version of this repo.

--

--

Payam Saderi

Now I am a DevOps engineer! I started to work as a front-end developer in 2000, aslo I have more than 16 years of experiences as a Linux system administrator