packer fmt: Usage & Common CI Errors
Canonical formatting for your Packer HCL2 templates.
packer fmt rewrites .pkr.hcl and .pkrvars.hcl files to the canonical HCL2 style. As a CI check it keeps template diffs clean and reviews focused on substance.
What it does
packer fmt applies standard HCL2 formatting - consistent indentation and alignment - to Packer template files. By default it edits files in place; with -check it only reports whether files are already formatted and exits nonzero if not.
Common usage
# Format files in place
packer fmt .
# Recurse into subdirectories
packer fmt -recursive .
# CI gate: fail if anything is unformatted, show the diff
packer fmt -check -diff -recursive .Common error in CI: fmt -check fails the build
A formatting gate exits nonzero and lists files that "would be formatted", but applies no change - -check never writes. Fix: run packer fmt -recursive . locally, commit the result, and push. To auto-fix in a pre-commit hook instead of failing CI, drop -check and let fmt write in place.
Key options
| Option | Purpose |
|---|---|
| -check | Exit nonzero if not formatted (no writes) |
| -diff | Show the formatting diff |
| -recursive | Include subdirectories |
| -write=false | Do not modify files |