cheat-sheets

Puppet Cheat-Sheet

Puppet is a declarative configuration management tool: you describe the desired state of a machine in manifests and an agent makes the machine match. Use puppet apply for a single host or testing, and an agent talking to a Puppet server for a fleet.

Documentation: Puppet docs · man puppet-apply Project: Puppet

Verification: options checked against the Puppet 8 man pages (Ubuntu 24.04 package, December 2023 pages). The manifest, EPP template and the apply, resource, facts, config, lookup, parser and epp commands were run in a throwaway container with puppet apply; no Puppet server, agent-to-server run, Hiera hierarchy or Forge module was tested.

Related sheets: Ansible, Ansible-Playbook, Terraform, Salt, Vagrant.

Run Puppet #

COMMANDDESCRIPTION
puppet apply <MANIFEST>.ppApply a manifest to this machine
puppet apply --noop <MANIFEST>.ppDry run: show what would change
puppet apply -e 'notify { "hi": }'Apply a one-line manifest
puppet apply --modulepath <DIR> <MANIFEST>.ppUse modules from a directory
puppet apply --detailed-exitcodes <MANIFEST>.ppExit codes: 0 no changes, 1 failed, 2 changes applied, 4 some resources failed, 6 changes and failures
puppet apply --test <MANIFEST>.ppVerbose, detailed exit codes and diffs
puppet apply --debug --trace <MANIFEST>.ppDebug output and stack traces
puppet agent -tOne verbose agent run against the server (--test)
puppet agent -t --noopDry run against the server
puppet agent --onetime --no-daemonizeOne run, no background daemon
puppet agent --disable "<REASON>" / puppet agent --enablePause or resume agent runs on this node
puppet agent --fingerprintShow the certificate fingerprint
puppet agent --certname <NAME> --waitforcert 60Set the identity and wait for the cert to be signed
puppet parser validate <FILE>.ppCheck manifest syntax
puppet epp validate <FILE>.eppCheck an EPP template’s syntax
puppet --versionShow the version

--detailed-exitcodes uses 2 for “changes applied”, so a clean second run returning 0 is the idempotence check.

Inspect and Change Resources #

COMMANDDESCRIPTION
puppet resource <TYPE>List every resource of a type (for example service, package, user)
puppet resource <TYPE> <NAME>Show one resource as Puppet code
puppet resource <TYPE> <NAME> <ATTR>=<VALUE>Change a resource right now
puppet resource --typesList the available resource types
puppet describe <TYPE>Documentation for a type (--list shows all types, --short a brief form)
puppet factsPrint this node’s facts as JSON
puppet config print <SETTING>Show a setting (all for everything)
puppet config set <SETTING> <VALUE> --section mainWrite a setting to puppet.conf
puppet lookup <KEY>Look a value up through Hiera
puppet lookup <KEY> --explainShow how it was resolved
puppet lookup <KEY> --node <NAME> --facts <FILE>Look up for another node, with overridden facts
puppet lookup <KEY> --merge deepMerge strategies: first, unique, hash, deep
puppet lookup <KEY> --default <VALUE>Fall back if nothing is found
puppet filebucketBack up and restore file contents Puppet has changed

Manifest Basics #

Resources have a type, a title and attributes. ensure, require, notify and subscribe order and trigger them.

puppet
class motd_demo (
  String  $message = 'Managed by Puppet',
  Boolean $enable  = true,
) {
  file { '/tmp/motd-demo':
    ensure  => file,
    mode    => '0644',
    content => epp('motd_demo/motd.epp', { 'message' => $message }),
  }

  exec { 'say-hello':
    command     => '/bin/echo changed',
    refreshonly => true,
    subscribe   => File['/tmp/motd-demo'],
  }
}

node default {
  class { 'motd_demo':
    message => "Hello from ${facts['networking']['hostname']}",
  }
}

The EPP template modules/motd_demo/templates/motd.epp:

text
<%- | String $message | -%>
<%= $message %>

Run it with puppet apply --modulepath modules site.pp. A second run changes nothing and exits 0 with --detailed-exitcodes.

Common resource types:

TYPEMANAGES
packageSoftware (ensure => installed, latest, absent)
fileFiles, directories, links (ensure, source, content, owner, group, mode)
serviceServices (ensure => running, enable => true)
user / groupAccounts
execCommands; add creates, onlyif, unless or refreshonly so they stay idempotent
cronCron jobs
notifyMessages in the run log

Modules and Data #

COMMANDDESCRIPTION
puppet module install <AUTHOR>-<MODULE>Install a module from the Forge (--version, --target-dir)
puppet module listList installed modules (--tree for dependencies)
puppet module upgrade <AUTHOR>-<MODULE>Upgrade
puppet module uninstall <AUTHOR>-<MODULE>Remove
puppet module changes <PATH>Show files changed since installation
puppet generate typesGenerate resource type metadata for environments
puppet catalog compile <CERTNAME>Compile a catalog (needs a server or the right terminus)

A module is a directory with manifests/, templates/, files/, lib/ and data/; manifests/init.pp defines the class named after the module.

Server and Certificates #

COMMANDDESCRIPTION
puppetserver ca listPending certificate requests (run on the server)
puppetserver ca sign --certname <NAME>Sign a request
puppetserver ca clean --certname <NAME>Revoke and remove a node’s certificate
puppet ssl bootstrapBootstrap an agent’s certificates (puppet ssl --help for other actions)
puppet node clean <NAME>Remove a node’s data from the server

The puppetserver ca commands were not checked against a server here.

Troubleshooting #

SYMPTOMFIX
Agent run says the certificate is not signedSign it on the server, or check --certname and the server name
Run changes the same thing every timeThe resource is not idempotent: add creates or unless to exec, or manage the attribute properly
Could not find classCheck --modulepath and that manifests/init.pp defines a class with the module’s name
Dependency cycle errorLook for require/before/notify/subscribe that loop back on each other
Need to see what would changepuppet agent -t --noop or puppet apply --noop

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.