Puppet Declarative config management

Version 8.22.0 · source

Start here

puppet parser validate <file.pp>
Check syntax
puppet apply --noop <file.pp>
Dry run locally
puppet apply <file.pp>
Apply for real
puppet agent -t
One run against server
puppet resource <type> <name>
Inspect a resource
facter <fact>
Read a system fact
puppet lookup <key>
Query Hiera data
puppet module install <name>
Get a Forge module

Concepts

  1. Facts facter on the node
  2. Catalog compiled from manifests + Hiera
  3. Agent compares to real state
  4. Report changes and failures

Apply and agent

puppet apply <file.pp>
Apply a manifest locally
puppet apply --noop <file.pp>
Dry run, show changes only
puppet apply -e '<code>'
Run a one-line snippet
puppet apply --test <file.pp>
Verbose, diffs, detailed exit codes
puppet apply --detailed-exitcodes <file.pp>
Exit code reports changes
0 no change, 1 failed, 2 changed, 4 resources failed, 6 changed and failed. Plain apply exits 0 even when it changed things.
puppet apply --modulepath <dir> <file.pp>
Use modules from a directory
puppet apply --tags <tag> <file.pp>
Only resources with this tag
puppet apply --debug <file.pp>
Full debug output
puppet apply --catalog <file.json>
Apply a compiled catalog
puppet agent -t
One foreground run, verbose
puppet agent -t --noop
Preview what the server would change
puppet agent --onetime --no-daemonize
Single run, stay in terminal
puppet agent -t --environment <env>
Run against another environment
puppet agent --server <host> -t
Point at a specific server
puppet agent --disable "<reason>"
Pause runs on this node
puppet agent --enable
Resume runs

Inspect and change

puppet resource <type>
List all resources of a type
puppet resource <type> <name>
Show one resource as code
puppet resource <type> <name> <attr>=<value>
Change a resource now
puppet resource service <name> ensure=running
Start a service
puppet resource package <name> ensure=present
Install a package
puppet resource --types
List known resource types
puppet describe <type>
Docs for a resource type
puppet describe <type> -s
Short attribute list
puppet help <subcommand>
Help for a subcommand
puppet --version
Print Puppet version

Facts and config

facter
Print all facts
facter <fact>
One fact, e.g. os.family
facter --json
All facts as JSON
facter -p
Include facts from Puppet plugins
facter <fact> --debug
Debug a fact resolving wrong
puppet facts
Show facts as the server sees them
puppet config print
All settings and values
puppet config print <setting>
One setting, e.g. modulepath
puppet config set <setting> <value>
Write to puppet.conf

Hiera and lookup

puppet lookup <key>
Look up a data value
puppet lookup <key> --explain
Show where the value came from
puppet lookup <key> --node <node>
Look up as another node
puppet lookup <key> --facts <file.yaml>
Override facts for the lookup
puppet lookup <key> --merge deep
Merge hashes and arrays
puppet lookup <key> --default <value>
Fallback if not found

Modules

puppet module install <author-name>
Install from the Forge
puppet module install <author-name> --version <ver>
Pin a version
puppet module list
Installed modules
puppet module list --tree
Show dependency tree
puppet module upgrade <author-name>
Update a module
puppet module uninstall <author-name>
Remove a module
puppet module install <name> --target-dir <dir>
Install elsewhere
puppet config print modulepath
Where modules are searched

Check and lint

puppet parser validate <file.pp>
Syntax check only
puppet parser validate
Check the site manifest
puppet-lint <file.pp>
Style problems
puppet-lint --fix <file.pp>
Auto-fix what it can
puppet-lint --fail-on-warnings <path>
Non-zero exit for CI
puppet-lint --no-<check>-check <path>
Skip one check

Manifest syntax

Resource
package { 'nginx':  ensure => installed,}
Service and file
service { 'nginx':  ensure => running,  enable => true,}file { '/etc/motd':  ensure  => file,  content => "Managed by Puppet\n",}
Class
class web (  String $port = '80',) {  package { 'nginx': ensure => installed }}include web
Ordering
package { 'nginx': ensure => installed }-> service { 'nginx': ensure => running }# or: require => Package['nginx']
Notify on change
file { '/etc/nginx/nginx.conf':  source => 'puppet:///modules/web/nginx.conf',  notify => Service['nginx'],}
Conditionals
if $facts['os']['family'] == 'Debian' {  $pkg = 'apache2'} else {  $pkg = 'httpd'}
Exec guarded
exec { 'reload-units':  command => '/bin/systemctl daemon-reload',  refreshonly => true,}
Template
file { '/etc/app.conf':  content => epp('app/app.conf.epp', { 'port' => 8080 }),}
Loop over a list
['git', 'curl'].each |$p| {  package { $p: ensure => installed }}

Recipes

Safe local change
puppet parser validate site.pppuppet-lint site.pppuppet apply --noop site.pppuppet apply site.pp
Preview a node run
puppet agent -t --noop# review, thenpuppet agent -t
Debug a Hiera value
puppet lookup web::port --node web01 --explainpuppet lookup web::port --facts test.yaml --explain
Test a module quickly
puppet module install puppetlabs-stdlib --target-dir ./modulespuppet apply --modulepath ./modules -e 'include stdlib'