Skip to content

Latest commit

 

History

History
443 lines (308 loc) · 7.75 KB

File metadata and controls

443 lines (308 loc) · 7.75 KB

🤖 Puppet Cheat Sheet

puppet-cheat.png

📘 Introduction

Puppet is an open-source configuration management tool that automates infrastructure provisioning, configuration, and management. It uses a declarative language to describe the desired state of your systems.

Puppet supports both agent-master and agentless (bolt) architectures, making it powerful for large-scale environments.


🧠 Key Concepts

Term Description
Manifest A file written in Puppet DSL (.pp) that describes desired system state.
Module A collection of manifests, templates, files, etc., organized in structure.
Class Reusable block of Puppet code.
Resource Basic unit that describes something (like a package or service).
Facts System information gathered by Facter.
Catalog Compiled version of the manifests specific to a node.
Node A client machine being managed.

🧾 Puppet Commands

🟢 Beginner Commands (Click to Expand)

🔹 Check Version

puppet --version

🔹 Apply Manifest Locally

puppet apply example.pp

🔹 Validate Syntax of Manifest

puppet parser validate example.pp

🔹 Format Manifests (Linting)

puppet parser validate example.pp
puppet-lint example.pp

🔹 List Available Facts

facter
facter os

🔹 View Help

puppet help
puppet help apply

🟡 Intermediate Commands (Click to Expand)

🔹 Puppet Resource (Inspect or Manage)

puppet resource <type>
puppet resource user root
puppet resource service ssh

🔹 Generate New Module Skeleton

puppet module generate yourname-modulename

🔹 Install a Module

puppet module install puppetlabs-apache

🔹 List Installed Modules

puppet module list

🔹 Check Current Puppet Config

puppet config print
puppet config print all

🔴 Advanced Commands (Click to Expand)

🔹 Agent Commands

puppet agent -t
puppet agent -t --debug

🔹 Manage Certificates

puppetserver ca list
puppetserver ca sign --certname node.example.com
puppetserver ca revoke --certname node.example.com
puppetserver ca clean --certname node.example.com

🔹 PuppetDB Query

puppet query 'inventory[certname] { facts.os.name = "Ubuntu" }'

🔹 Run Task with Bolt

bolt command run "uptime" --targets localhost
bolt plan run myplan

🔹 Testing & Debugging

puppet apply --noop file.pp
puppet apply --debug file.pp
puppet lookup varname
puppet describe <type>

🔹 System & Config

puppet config print <setting>
puppet facts show
puppet module search apache
puppet doc <module>
puppet resource --to_yaml

🟢 Beginner Level

🔹 Installing Puppet (Agent/Master)

# Install Puppet (Debian/Ubuntu)
sudo apt install puppet

# Check version
puppet --version

🔹 First Manifest Example

# hello.pp
file { '/tmp/hello.txt':
  ensure  => present,
  content => "Hello from Puppet!",
}

Run it:

puppet apply hello.pp

🔹 Resource Types

Type Example
file Manage files, directories, symlinks
package Install, remove software
service Ensure a service is running/stopped
user Manage system users
# Install nginx and ensure it runs
package { 'nginx':
  ensure => installed,
}

service { 'nginx':
  ensure => running,
  enable => true,
}

🔹 Variables

$greeting = "Hello, World"
notice($greeting)

🔹 Conditionals

if $osfamily == 'Debian' {
  notice("Debian-based system")
} else {
  notice("Other OS")
}

🟡 Intermediate Level

🔸 Facts and Facter

View system facts:

facter
facter os

Use in manifests:

if $facts['os']['family'] == 'RedHat' {
  package { 'httpd': ensure => installed }
}

🔸 Classes

class apache {
  package { 'apache2': ensure => installed }
  service { 'apache2': ensure => running }
}

Include it:

include apache

🔸 Modules

puppet module generate yourname-apache
puppet module install puppetlabs-apache

Structure:

apache/
├── manifests/
│   └── init.pp
├── files/
├── templates/

Use:

class { 'apache': }

🔸 Templates (ERB)

File: templates/vhost.erb

<VirtualHost *:80>
  ServerName <%= @servername %>
</VirtualHost>

Manifest:

file { '/etc/httpd/conf.d/vhost.conf':
  content => template('apache/vhost.erb'),
}

🔸 Puppet Apply vs Agent

Mode Usage
Apply Local apply of manifests
Agent Connects to master and applies catalog

🔴 Advanced Level

🔹 Puppet Master-Agent Setup

  • Puppet Server: Central server managing infrastructure.
  • Agent: Node that pulls configuration from the server.
# On agent
puppet agent -t

Sign certs:

puppetserver ca list
puppetserver ca sign --certname <agent-fqdn>

🔹 Environments

Used to separate dev, staging, prod configs.

Directory structure:

/etc/puppetlabs/code/environments/
├── production/
│   └── manifests/
├── development/

🔹 Hiera (Hierarchical Data Lookup)

Configure external data in YAML:

# hiera.yaml
version: 5
defaults:
  datadir: data
  data_hash: yaml_data

# data/common.yaml
apache::port: 80

Access in Puppet:

$port = lookup('apache::port')

🔹 PuppetDB

Central storage for catalog, fact, and report data.

Query:

query_nodes(['=', 'catalog_environment', 'production'])

🔹 Bolt (Agentless Task Runner)

bolt command run 'uptime' --targets localhost
bolt plan run myplan

Write plans in YAML or Puppet DSL.


📌 Useful Puppet CLI Commands

Command Description
puppet apply <file.pp> Apply a manifest locally
puppet agent -t Trigger agent run
puppet resource <type> <name> View current resource state
puppet module install <name> Install a module
puppet config print all Print all config settings
puppet parser validate file.pp Validate syntax of manifest
facter Show system facts
puppet doc <module> Generate module documentation

📚 Learning Resources