Why Check Code Automatically?
A typo in a manifest may only show up when an agent runs on production. Automatic checks find syntax errors, style problems and risky patterns in seconds, on your laptop and in CI, before anything is deployed.
Syntax Validation
bash
puppet parser validate manifests/site.pp
puppet parser validate $(find . -name '*.pp' -not -path './modules/*')
puppet epp validate templates/*.epp
erb -P -x -T '-' templates/app.conf.erb | ruby -c # ERB templates
ruby -c lib/facter/my_fact.rb
yamllint data/ hiera.yaml # install: pip install yamllint
python3 -c "import json,sys; json.load(open('metadata.json'))"
puppet parser validate checks only syntax. It does not know whether a class exists or whether your variables are valid. Unit tests catch those problems (next lessons).
puppet-lint
bash
gem install puppet-lint # or use: pdk validate
puppet-lint manifests/
puppet-lint --fix manifests/ # fix simple problems automatically
puppet-lint --no-140chars-check manifests/init.pp
puppet-lint --fail-on-warnings manifests/ # non-zero exit when warnings exist (good for CI)
text
manifests/init.pp - WARNING: double quoted string containing no variables on line 7
manifests/init.pp - WARNING: unquoted file mode on line 12
manifests/init.pp - WARNING: arrow should be on the right operand's line on line 15
manifests/init.pp - ERROR: tab character found on line 22
manifests/init.pp - WARNING: top-scope variable being used without an explicit namespace on line 30
| Common warning | Fix |
double quoted string containing no variables | Use single quotes: 'text' |
unquoted file mode | Write mode => '0644' |
arrow alignment | Align => arrows in each resource block |
variable not enclosed in {} | In strings use "${name}" |
class not documented | Add @summary and @param comments |
ensure should be the first attribute | Put ensure first in a resource |
top-scope variable | Use $facts['x'] or $::x carefully; prefer $facts |
legacy facts | Use $facts['os']['family'] instead of $osfamily |
trailing whitespace, tab character | Clean spaces and use 2-space indentation |
140chars | Break long lines |
Configuration File
text
# ~/.puppet-lint.rc or .puppet-lint.rc in the project
--fail-on-warnings
--no-140chars-check
--no-documentation-check
--relative
--ignore-paths=modules/*,vendor/**/*.pp,spec/fixtures/**/*.pp
puppet
# Disable a check for one line, with a reason
$x = "$a" # lint:ignore:only_variable_string (legacy reasons)
# lint:ignore:80chars
$long_line = 'a very long string ... '
# lint:endignore
Style Guide Summary
| Topic | Rule |
| Indentation | 2 spaces, never tabs |
| Strings | Single quotes unless interpolating |
| Resource titles | Always quote them |
| Attribute arrows | Align => within a resource |
ensure | First attribute |
| Modes | Quoted four-digit strings: '0644' |
| Class names | Lowercase, :: separated, matching the file path |
| Variables | Lowercase snake_case, with $facts[...] instead of top-scope |
| Parameters | Typed, with defaults where possible, documented |
| Conditionals | Always include a default or else branch |
| Comments | Explain why, not what |
| Line length | Prefer 140 characters or fewer |
puppet
# Before
class ntp($servers=["a","b"]){
package{"chrony": ensure=>installed, }
file { "/etc/chrony.conf": mode=>644, content=>template("ntp/chrony.erb") }
}
# After
# @summary Manage time synchronization
# @param servers List of time servers
class ntp (
Array[String] $servers = ['a', 'b'],
) {
package { 'chrony':
ensure => installed,
}
file { '/etc/chrony.conf':
ensure => file,
mode => '0644',
content => epp('ntp/chrony.conf.epp', { 'servers' => $servers }),
}
}
Editor Integration
- VS Code: the Puppet extension runs lint on save and shows problems inline
- Vim: syntastic or ALE with
puppet-lint and puppet parser validate
- IntelliJ: Puppet plugin
- Make 'format on save' and 'trim whitespace' part of the shared editor configuration (
.editorconfig)
ini
# .editorconfig
root = true
[*]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
charset = utf-8
Pre-commit Hook
yaml
# .pre-commit-config.yaml (pip install pre-commit; pre-commit install)
repos:
- repo: local
hooks:
- id: puppet-parser-validate
name: puppet parser validate
entry: puppet parser validate
language: system
files: \.pp$
- id: puppet-lint
name: puppet-lint
entry: puppet-lint --fail-on-warnings
language: system
files: \.pp$
- id: yamllint
name: yamllint
entry: yamllint
language: system
files: \.ya?ml$
CI Job
yaml
# GitHub Actions
name: lint
on: [pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with: { ruby-version: '3.2', bundler-cache: true }
- run: gem install puppet-lint
- run: puppet-lint --fail-on-warnings site-modules manifests
- run: pip install yamllint && yamllint data hiera.yaml
Practice Tasks
- Run
puppet parser validate and puppet-lint on one of your earlier manifests and fix every warning
- Run
puppet-lint --fix and review the diff before committing
- Create a
.puppet-lint.rc that fits your team
- Set up a pre-commit hook and try to commit a file with a syntax error
- Add a lint job to your CI pipeline
Conclusion
Run puppet parser validate, puppet epp validate, puppet-lint and yamllint on every change, first in your editor, then in a pre-commit hook, then in CI. Follow the style guide so code stays easy to read.
Quick Summary: parser validate = syntax only; puppet-lint style; --fix; pre-commit; CI; typed documented parameters.