ascia.tech

Packer and Ubuntu Cloud Init

· C.M. Hobbs

On a recent project, I needed to create a bespoke VM disk image. Normally I would either use something like Alpine if it needed to be lightweight, or an RPM based distro for something heavier. In this case, the project called for Ubuntu for compatibility on various levels.

While doing some research on configuration, I bumped into Ubuntu cloud images. I had used these before on AWS but never really dove into the cloud-init configuration. These images come with cloud-init preconfigured and when they boot up for the first time, they’ll try to set themselves up with custom configuration.

Given that Packer spins up a VM to build images, I thought I might try using cloud-init to configure it. This is likely a known pattern but it was all new to me. It proved to be a pretty simple process.

In short, I set my iso_url to one of the cloud images and then created a user-data file to set up all the stuff I wanted to configure. One neat option was the disk_image reference to let Packer know that the image was directly bootable, instead of just being an ISO.

Here’s a very loose example of the Packer setup:

source "qemu" "ubuntu-example" {
  iso_url      = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
  iso_checksum = "file:https://cloud-images.ubuntu.com/noble/current/SHA256SUMS"
  disk_image = true

  cd_files = ["./user-data", "./meta-data"]

  ...
}

And an example for “user-data”:

users:
  - name: bob
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
      - ssh-rsa DEADC0FFEE...

packages:
  - cowsay
  - sl

And sample meta-data:

instance-id: 42
local-hostname: gabecube

Overall, this was pretty seamless and cloud-init feels a lot like Ansible shorthand. I don’t often think about using Ubuntu when building infrastructure but I may have to tinker with it some more in the future. I found it to be really convenient.

#100daystooffload #linux #hcl #packer #ubuntu #devops

Reply to this post by email ↪