Chuyển đến nội dung

Bài 27 · Testing and Code Management

Puppet Lint and Parser Validate

Catch Puppet mistakes before they reach a node: puppet parser validate, puppet-lint checks and plugins, epp validate, YAML checks, noop runs and a pre-commit pipeline.

Infrastructure code deserves the same safety net as application code. A typo in a manifest can stop catalog compilation for every node that uses it, and a wrong value can reconfigure a thousand machines within half an hour. The cheapest defense is static checking: tools that read your files, without running anything on a node, and report syntax errors, style problems and risky patterns.

This lesson introduces the checks you should run on every commit: the parser validator, puppet-lint, template and data validation, and the noop mode. They are command-line tools, so the examples are read-only; the runnable blocks are stand-ins that reproduce the idea of a lint rule on plain text.

puppet parser validate

The Puppet language has a parser that can check syntax without compiling anything:

Terminal
puppet parser validate manifests/site.pp
puppet parser validate modules/profile/manifests/*.pp

No output and exit code 0 means the syntax is valid. An error prints the file, line and a message. It does not know whether apache::vhost exists or whether a variable is defined; it only checks that the code parses. That is exactly why it is fast enough for a pre-commit hook.

Related validators:

Terminal
puppet epp validate templates/nginx.conf.epp      # EPP syntax
ruby -rerb -e 'ERB.new(File.read(ARGV[0])).src' templates/old.erb   # ERB syntax
puppet lookup --node test.example.com some::key   # data sanity

YAML and JSON files (Hiera data, metadata.json) should also be parsed in CI, because a single bad indent in a data file breaks lookups for every node that reads it.

puppet-lint

puppet-lint is a style checker based on the official Puppet style guide. It reports problems with a check name so that you can fix or disable each one:

Terminal
puppet-lint manifests/
puppet-lint --fix manifests/webserver.pp
puppet-lint --no-140chars-check manifests/

Typical checks include:

Check Meaning
arrow_alignment The => arrows in a resource should line up
double_quoted_strings Use single quotes when nothing is interpolated
variable_scope Variables in a class should be fully qualified or defined locally
trailing_whitespace, hard_tabs Whitespace hygiene
documentation Classes and defines should have a comment
top_scope_facts Use $facts['x'], not bare legacy variables
ensure_first_param ensure should come first in a resource
unquoted_file_mode File modes need quotes, for example '0644'
selector_inside_resource Do not hide a selector in an attribute value

The --fix flag repairs many problems automatically (alignment, quotes, whitespace). Always review the diff afterwards.

Why bother with style rules? Because uniform code makes real bugs visible in reviews. The unquoted_file_mode check, for instance, prevents a real production incident: YAML or the parser may read an unquoted 0644 as a number, which is not the mode you meant.

Configure the tool with a .puppet-lint.rc file in the repository so every developer and the CI system run the same rules:

TEXT
--fail-on-warnings
--no-140chars-check
--relative
--ignore-paths=vendor/**/*.pp,spec/fixtures/**/*.pp

Plugins add checks, for example for trailing commas or for unsafe class parameters. Install them as gems and they are picked up automatically.

Noop: a dry run on a node

Static checks cannot know what a real node would do. No-operation mode compiles the catalog and compares it with the node, but changes nothing:

Terminal
puppet agent --test --noop
puppet apply --noop manifests/site.pp

The report shows Notice: ... current_value 'absent', should be 'present' (noop) lines for every resource that would change. Use it before a risky roll-out. A resource can also be protected from change with noop => true, a metaparameter useful for a slow, safe introduction of a new rule.

Make noop a routine: run a noop on a canary node, read the list of changes, and only then promote the code to the rest of the fleet. If the list is surprising, you saved yourself an incident.

A pre-commit and CI pipeline

A reasonable minimum, in the order of cost:

  1. YAML/JSON syntax check of data and metadata.
  2. puppet parser validate and puppet epp validate.
  3. puppet-lint with --fail-on-warnings.
  4. Unit tests (RSpec-puppet).
  5. Acceptance tests on real or containerized machines (Litmus).
flowchart LR
  C["git commit"] --> V["parser validate / epp validate"]
  V --> L["puppet-lint"]
  L --> U["rspec-puppet"]
  U --> A["Litmus acceptance"]
  A --> D["merge and deploy"]

Cheap checks first, expensive checks last: fail fast.

Stand-ins: two tiny lint rules

The code below is a JavaScript stand-in with two of the real lint ideas: unquoted file modes and arrow alignment. The real tool uses a proper Puppet lexer; this one uses regular expressions on lines, enough to show the mechanism.

JS Stand-in: unquoted file modes and double quotes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: arrow alignment check

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Using the results well

Treat lint output as a conversation, not a punishment. When a rule is wrong for your team, disable it in the config file, with a comment explaining why, instead of scattering # lint:ignore markers across the code. When a rule exposes a genuine pattern (like missing documentation), fix the cause. And never run --fix on code you have not committed: you want the cleanup in its own commit.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does puppet parser validate check?

    It checks syntax only: it does not evaluate resources or data.
  2. 2. Why does puppet-lint warn about an unquoted file mode?

    Modes should be strings such as '0644' to avoid numeric interpretation.
  3. 3. What does puppet agent --test --noop do?

    Noop compiles and compares but does not change the node.
  4. 4. In a pipeline, which check should run first?

    Fail fast: cheap static checks go first, slow tests last.

Summary

  • puppet parser validate and puppet epp validate check syntax only, and are cheap enough for every commit.
  • puppet-lint enforces the style guide; keep its rules in .puppet-lint.rc and use --fix carefully.
  • Validate YAML and JSON data too.
  • Noop runs show the changes a catalog would make; use them on a canary first.
  • Order checks from cheapest to most expensive.

Next: RSpec-puppet Unit Tests

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.