Puppet
Declarative configuration management, manifests, modules, and agent runs
๐ Introduction
Puppet is an enterprise-grade configuration management tool that automates the provisioning, configuration, and compliance of infrastructure across multi-cloud and on-premises environments.
Puppet uses a declarative domain-specific language (DSL) based on Ruby to model system state. It can be deployed in an agent-server (primary/agent) architecture or run standalone using Puppet Apply or Puppet Bolt (agentless task runner).
๐ง Key Concepts
| Term | Description |
|---|---|
| Manifest | A file containing Puppet DSL code with the .pp file extension. |
| Resource | The basic unit of configuration representing a system element (package, file, service, user). |
| Class | A named, reusable block of Puppet code representing a singleton configuration component. |
| Defined Type | Reusable block that can be instantiated multiple times with unique titles. |
| Module | A structured package containing manifests, templates, files, and metadata. |
| Facts | System metadata automatically gathered from managed nodes using Facter. |
| Catalog | Compiled dependency graph representing the desired state for a specific node. |
| Hiera | Key-value hierarchical data lookup tool separating code from configuration data. |
๐งพ Puppet Commands Quick Reference
๐ข Beginner Commands (Click to Expand)
๐น Check Version & Environment
puppet --version
facter --version๐น Apply Manifest Locally
puppet apply manifest.pp
puppet apply --noop manifest.pp # Dry-run mode (no changes applied)๐น Syntax Validation & Linting
puppet parser validate manifest.pp
puppet-lint manifest.pp๐น Inspect & Query System Resources
puppet resource user root
puppet resource service sshd
puppet resource package nginx๐น View System Facts
facter
facter os.family
facter networking.ip
facter memory.system.available๐ก Intermediate Commands (Click to Expand)
๐น PDK (Puppet Development Kit)
pdk new module my_module
pdk new class my_class
pdk validate # Syntax, linting, metadata check
pdk test unit # Run RSpec unit tests๐น Manage Modules
puppet module list
puppet module install puppetlabs-apt
puppet module install puppetlabs-docker
puppet module upgrade puppetlabs-apt๐น Trigger Agent Run Manually
# Run agent once and print verbose output
puppet agent -t
# Dry-run agent execution
puppet agent -t --noop
# Enable debug logging
puppet agent -t --debug๐ด Advanced Commands (Click to Expand)
๐น Certificate Authority Management (puppetserver ca)
# List all pending certificate signing requests (CSRs)
puppetserver ca list
# Sign an agent certificate
puppetserver ca sign --certname node1.example.com
# Sign all pending certificates
puppetserver ca sign --all
# Revoke and clean a decommissioned node certificate
puppetserver ca revoke --certname node1.example.com
puppetserver ca clean --certname node1.example.com๐น PuppetDB & PQL Queries
# Query nodes by operating system
puppet query 'inventory[certname] { facts.os.family = "Debian" }'
# Query nodes where a specific package is installed
puppet query 'resources[certname] { type = "Package" and title = "openssl" }'๐น Hiera CLI Lookup
puppet lookup my_module::port --node web1.example.com --explain๐น Puppet Bolt (Agentless Task Execution)
bolt command run "uptime" --targets linux_servers
bolt task run package action=install name=curl --targets web1.example.com
bolt plan run myplan::deploy --targets all๐ข Beginner Level: Syntax & Resources
๐น Basic Resource Declaration
# Manage file
file { '/etc/motd':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
content => "Authorized Access Only - Managed by Puppet\n",
}
# Manage package
package { 'nginx':
ensure => installed,
}
# Manage service
service { 'nginx':
ensure => running,
enable => true,
}๐ Resource Relationships & Chaining
In Puppet, resources do not execute in linear top-to-bottom order by default. You must declare explicit dependencies using metaparameters or chaining arrows.
๐น Relationship Metaparameters
| Metaparameter | Behavior |
|---|---|
require | Current resource applies after specified resource |
before | Current resource applies before specified resource |
notify | Current resource applies before and sends a refresh trigger if changed |
subscribe | Current resource applies after and refreshes if target changed |
package { 'nginx':
ensure => installed,
}
file { '/etc/nginx/nginx.conf':
ensure => file,
source => 'puppet:///modules/nginx/nginx.conf',
require => Package['nginx'],
notify => Service['nginx'],
}
service { 'nginx':
ensure => running,
enable => true,
}๐น Chaining Arrows
Use -> (ordering) and ~> (ordering with notification):
Package['nginx'] -> File['/etc/nginx/nginx.conf'] ~> Service['nginx']๐ก Intermediate Level: Classes, Defined Types & Modules
๐ธ Classes vs Defined Types
- Class: Singleton. Can only be declared once per node.
- Defined Type: Reusable prototype. Can be instantiated multiple times with different titles.
Class Example:
class nginx (
Integer $port = 80,
String $package_name = 'nginx',
) {
package { $package_name:
ensure => installed,
}
file { '/etc/nginx/sites-available/default':
ensure => file,
content => epp('nginx/vhost.epp', { 'port' => $port }),
notify => Service['nginx'],
}
service { 'nginx':
ensure => running,
enable => true,
}
}Defined Resource Type Example:
define nginx::vhost (
Integer $port,
String $docroot,
) {
file { "/etc/nginx/conf.d/${title}.conf":
ensure => file,
content => epp('nginx/vhost_snippet.epp', {
'server_name' => $title,
'port' => $port,
'docroot' => $docroot,
}),
notify => Service['nginx'],
}
}
# Invoking multiple instances:
nginx::vhost { 'blog.example.com':
port => 80,
docroot => '/var/www/blog',
}
nginx::vhost { 'shop.example.com':
port => 8080,
docroot => '/var/www/shop',
}๐ธ Templates: EPP (Embedded Puppet) vs ERB
Modern Puppet favors EPP (.epp) templates using native Puppet DSL over legacy ERB.
File: templates/vhost.epp:
<%- | String $server_name, Integer $port, String $docroot | -%>
server {
listen <%= $port %>;
server_name <%= $server_name %>;
root <%= $docroot %>;
}Usage in manifest:
file { '/etc/nginx/conf.d/site.conf':
ensure => file,
content => epp('nginx/vhost.epp', {
'server_name' => 'example.com',
'port' => 80,
'docroot' => '/var/www/html',
}),
}๐ด Advanced Level: Hiera & Directory Environments
๐น Modern Hiera 5 Configuration (hiera.yaml)
Hiera separates code logic from site-specific data:
version: 5
defaults:
datadir: data
data_hash: yaml_data
hierarchy:
- name: "Per-node secrets (eyaml)"
lookup_key: eyaml_lookup_key
path: "nodes/%{trusted.certname}.eyaml"
- name: "Per-node data"
path: "nodes/%{trusted.certname}.yaml"
- name: "Environment data"
path: "environments/%{server_facts.environment}.yaml"
- name: "Operating System Family"
path: "os/%{facts.os.family}.yaml"
- name: "Common data"
path: "common.yaml"Lookup data directly in Hiera YAML:
# data/common.yaml
nginx::port: 80
app::db_user: 'app_admin'๐น Encrypted Secrets with Hiera-eYAML
Encrypt passwords directly in your git repository:
# Encrypt a secret string
eyaml encrypt -s 'SuperSecretPassword123'Output stored safely in data/common.eyaml:
app::db_password: >
ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw...==]๐ Puppet Best Practices
- Use PDK for Module Authoring: Standardize code style, unit tests, and validation.
- Always Declare Explicit Dependencies: Use
require,notify, or chaining arrows (->,~>) rather than relying on evaluation order. - Prefer EPP over ERB: EPP ensures type validation and syntax consistency with Puppet DSL.
- Use Trusted Facts: Rely on
$trusted['certname']for node identification to prevent fact spoofing. - Keep Manifests Generic: Store all environment-specific values in Hiera data files, not hardcoded in
.ppfiles.
