Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Practice Exercises and Cheat Sheet

Test what you learned with graded Puppet exercises on facts, templates, Hiera, roles and profiles, testing and operations, plus a command and language cheat sheet and review questions.

Reading about Puppet gets you halfway; the rest comes from building things and breaking them. This final lesson gives you graded exercises for the second half of the course (facts and templates, roles and profiles, testing, code management and operations), a compact cheat sheet and a set of review questions of the kind asked in interviews.

You need a lab: two virtual machines or containers (one for the server, one agent) or just puppet apply on a single machine. Nothing here runs on this site, so the exercises are written for your own lab, and the runnable blocks are stand-ins for checking your answers. Try each exercise before looking at the hints.

Exercises

Level 1: warm-up

  1. Fact tour. Run facter and list the facts for OS family, release, memory in bytes, processor count and primary IP. Write a manifest that prints them with notice() and check it with puppet apply.
  2. First template. Create an EPP template with typed parameters String $owner and Integer $retention = 14 that produces a short backup.conf. Render it with puppet epp render before using it in a file resource.
  3. Custom fact. Write an external fact (a text file in facts.d) named acme_tier and read it in a manifest. Observe that it appears only on the run after a Puppet-managed file creates it.

Level 2: design

  1. Two OS families. Write a class that installs the right web server package for Debian and RedHat families, and calls fail() for anything else. Add a unit test for all three cases (RSpec-puppet).
  2. Roles and profiles. Design three profiles (base, web, monitoring) and two roles (web_server, monitoring_server). Show on paper which component modules each uses and write down why a machine must not have both roles at once.
  3. Hiera hierarchy. Define a hierarchy with node, environment, OS family and common levels. Choose, for five settings (NTP servers, web port, admin users, Debian-only package name, a debug flag), the level where each belongs and the merge strategy it needs.
  4. Anti-pattern review. Take the code below and list every violation of the anti-patterns, then refactor it.
PUPPET
class role::api {
  package { 'git': ensure => installed }
  if $facts['networking']['hostname'] == 'api1' {
    class { 'profile::debug': level => 9 }
  }
  class { 'apache': default_vhost => false }
  $db = '10.0.4.17'
}

Level 3: operations

  1. Pipeline. Write a CI script that runs, in order, YAML checks, puppet parser validate, epp validate, puppet-lint and pdk test unit. Justify the order.
  2. Control repo. Create a control repository with a pinned Puppetfile and two branches. Deploy with r10k, point one agent at the feature branch, and show a noop diff.
  3. Capacity plan. A fleet has 5,000 nodes, a 30-minute interval and a 6-second compile on 8-core machines. How many compile servers do you need with a safety factor of 2? What would you optimize first?
  4. Incident drill. Break something on purpose (a duplicate resource, a bad Hiera type, a wrong server name, a dependency cycle) and write down the error message and the fix for each. This becomes your first runbook (Troubleshooting).
  5. Bolt rollout. Write a plan that restarts a service one node at a time with a health check between nodes and stops on the first failure (Bolt).

Check your own answers

Exercise 10 is arithmetic, so a stand-in can verify it. The first block calculates the plan; the second tests your hierarchy decisions from exercise 6 against a data table.

JS Stand-in: capacity plan for exercise 10

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

No output.

The comparison makes the answer to the second question: faster compiles are worth more than extra servers.

JS Stand-in: where does each setting belong?

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

No output.

There are several defensible answers; the point of the exercise is that you can explain why a value belongs at a level.

Cheat sheet

Facts and data

Terminal
facter                        facter os.family            facter -p
puppet lookup KEY --node N --explain
puppet query 'inventory[certname] { facts.os.family = "Debian" }'
PUPPET
$facts['os']['family']       $trusted['certname']        $server_facts['environment']
lookup('key', String, 'first', 'default')
pick($a, $b, 'default')      fail('message')             notice("x ${y}")

Templates

TEXT
<%= $var %>   <% code %>   <%# comment %>   <%- | Type $param | -%>   -%> trims newline
epp('module/file.epp', { 'k' => $v })      inline_epp('text <%= $x %>', {})
puppet epp validate FILE      puppet epp render --values '{ x => 1 }' FILE

Roles and profiles: role = business purpose, includes only profiles; profile = technology stack, takes data through parameters; one role per node.

Testing and code

Terminal
puppet parser validate FILE     puppet-lint --fix DIR      pdk validate     pdk test unit
r10k deploy environment -p      puppet agent --test --noop --environment NAME

Operations

Terminal
puppet agent --test [--noop|--debug|--trace|--evaltrace]
puppetserver ca list|sign|clean --certname NAME
bolt command run 'cmd' --targets GROUP      bolt plan run ns::plan targets=web

Exit codes with --detailed-exitcodes: 0 no change, 2 changes, 4 failures, 6 both.

Review questions

Answer aloud, in two or three sentences each:

  1. Why are facts untrusted, and what do you use instead for security decisions?
  2. Why does EPP take typed parameters, and why is that better than ERB's instance variables?
  3. A node must be "a web server and a database". How do roles and profiles model that?
  4. What is the difference between first, unique, hash and deep lookups?
  5. What do compile.with_all_deps and catch_changes each prove?
  6. Why pin modules in a Puppetfile?
  7. Where is the first place you look when a node reports certificate verify failed?
  8. When do you choose Bolt over a Puppet run?

Where next

You now have the full path from facts to fleet operations. To go deeper, return to the track overview, read the module and Hiera lessons again with your lab open, and contribute a small fix to a public module: reading and testing other people's code is the fastest way to become fluent.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A manifest uses $facts['role'] from a facts.d file to hand out database passwords. What is the problem?

    Facts come from the node and are untrusted; classification for secrets needs certname, trusted extensions or an ENC.
  2. 2. 5,000 nodes, 30 minute interval, 6 s compile. Average busy instances?

    5000/30 = 166.7 compiles per minute, times 0.1 minute each = 16.7 busy instances.
  3. 3. Which pair gives the strongest guarantee that code is safe to promote?

    Unit tests, a noop review and idempotence together cover compile, intent and stability.
  4. 4. Which tool pushes an ordered, health-checked restart over SSH without agents?

    Bolt plans orchestrate steps across targets with logic and error handling.

Summary

  • Practice with a lab: facts, templates, Hiera, roles and profiles, tests, r10k, noop and Bolt.
  • Check designs with arithmetic (capacity) and checklists (anti-patterns, baselines).
  • Keep the cheat sheet next to your terminal until the commands are muscle memory.
  • Explain every design decision in your own words: that is the real test of understanding.

Next: Back to the Puppet track