Skip to content

Recipes

Short, complete patterns that build on the verified core. Unless a recipe says otherwise, its parts are assembled from commands and blocks the lab ran; anything that needs a real builder is marked documented-only, like chapter 9.

One template, a file per environment

Keep image.pkr.hcl free of values that change, and keep one var-file per environment beside it — dev.pkrvars.hcl, release.pkrvars.hcl — each nothing but assignments:

packer build -var-file=dev.pkrvars.hcl -var stage_dir=stage-dev image.pkr.hcl
packer build -var-file=release.pkrvars.hcl -var stage_dir=stage-release image.pkr.hcl

Chapter 7 ran exactly this with release.pkrvars.hcl. Distinct stage_dir values are not decoration: chapter 6's rule says a second build into a lived-in folder nests instead of replacing.

Date the image, not just the release

Add a variable with no default but a habit — or compute the name in the build environment and pass it in:

packer build -var image_name="basebox-$(date +%Y%m%d)" image.pkr.hcl

Because -var beats every other source (chapter 7), the date lands in the manifest's custom_data and in the artifact itself. Fleets that date their images can answer "which pattern is this machine from?" months later, from the machine's own build-info.

Save the receipt with the build

The manifest and the downloaded build-info describe only the most recent build (chapter 8). If builds matter, archive them as a unit immediately after each build:

mkdir -p receipts/3.0.0
cp packer-manifest.json collected/build-info.txt receipts/3.0.0/

Nothing here is Packer magic — it is the filing discipline that turns chapter 8's checks from a spot check into an audit trail.

Checksum the image (documented-only)

With a builder that produces an image file, the checksum post-processor writes its hashes beside it:

post-processor "checksum" {
  checksum_types = ["sha256"]
  output         = "checksums/{{.ChecksumType}}.txt"
}

The lab's null builder exports no file to checksum, so this block is shown from the documentation, not from a transcript. Verify either end the way chapter 8 did: sha256sum and a human comparing two lines.

Templates in version control

A template is code and earns code's habits: commit it with its var-files and payload folder; let packer fmt -check and packer validate run in CI (exit codes 3 and 1 respectively — transcripts t03 and t08 — are made for failing a pipeline); and never commit credentials or the lab's SSH key. The one thing to keep out of the repository is anything a build secretes locally — staged folders, manifests, collected downloads belong in a .gitignore, regenerated by the next build.

Grow the payload with a real setup script

When inline shell lines outgrow the template, move them to a file and hand it to the shell provisioner as script = "setup.sh", uploaded and run the same way. The upgrade path from there is a real configuration tool as provisioner — the Ansible guide in this series is the natural next read — with the template shrinking back to what Packer is for: starting the machine and saving it.