Skip to content

Config_management

Chapter 49: Configuration Management with Puppet/Chef

Section titled “Chapter 49: Configuration Management with Puppet/Chef”

This chapter covers Puppet and Chef configuration management tools.


Puppet Architecture
+------------------------------------------------------------------+
| |
| Puppet Master |
| +------------------------------------------------------------+ |
| | Catalog compilation | |
| | Manifests (.pp files) | |
| | Module distribution | |
| +------------------------------------------------------------+ |
| |
| Agent (Puppet Agent) |
| +------------------------------------------------------------+ |
| | Facter - System facts | |
| | Apply catalog | |
| | Report to master | |
| +------------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

/etc/puppetlabs/code/environments/production/manifests/site.pp
# Package resource
package { 'nginx':
ensure => installed,
}
# Service resource
service { 'nginx':
ensure => running,
enable => true,
requires => Package['nginx'],
}
# File resource
file { '/etc/nginx/nginx.conf':
ensure => file,
source => 'puppet:///modules/nginx/nginx.conf',
require => Package['nginx'],
notify => Service['nginx'],
}
Terminal window
# User
user { 'deploy':
ensure => present,
shell => '/bin/bash',
home => '/home/deploy',
managehome => true,
}
# Group
group { 'developers':
ensure => present,
}
# Cron
cron { 'backup':
command => '/usr/local/bin/backup.sh',
hour => '2',
minute => '0',
}

nginx/
# ├── manifests/
# │ ├── init.pp
# │ ├── install.pp
# │ ├── config.pp
# │ └── service.pp
# ├── files/
# │ └── nginx.conf
# └── templates/
# └── nginx.conf.erb
Terminal window
# Include class
include nginx
# Declare class with parameters
class { 'nginx':
version => '1.24.0',
}

Chef Architecture
+------------------------------------------------------------------+
| |
| +----------------+ +----------------+ +--------------+ |
| | Chef Server | <-- | Chef Client | --> | Cookbooks | |
| | (Central) | | (On node) | | (Recipes) | |
| +----------------+ +----------------+ +--------------+ |
| | | | |
| v v v |
| +----------------+ +----------------+ +--------------+ |
| | Workstation | | Ohai | | Attributes | |
| | (knife) | | (System facts) | | (Variables) | |
| +----------------+ +----------------+ +--------------+ |
| |
+------------------------------------------------------------------+

cookbooks/nginx/recipes/default.rb
# Install package
package 'nginx' do
action :install
end
# Start service
service 'nginx' do
action [:enable, :start]
end
# Template config
template '/etc/nginx/nginx.conf' do
source 'nginx.conf.erb'
owner 'root'
group 'root'
mode '0644'
notifies :restart, 'service[nginx]'
end
# File
cookbook_file '/etc/motd' do
source 'motd'
mode '0644'
end
# Execute
execute 'update-apt' do
command 'apt-get update'
not_if { ::File.exist?('/var/cache/apt/initialized') }
end
# Template
template '/etc/app.conf' do
variables(
app_name: 'myapp',
port: 8080
)
end

Terminal window
# Upload cookbook
knife cookbook upload nginx
# Bootstrap node
knife bootstrap node.example.com -x admin -P password
# Run chef-client
knife ssh 'role:web' 'sudo chef-client'
# List nodes
knife node list
knife node show node1

+------------------------------------------------------------------+
| Feature | Puppet | Chef | Ansible |
+----------------+---------------+--------------+----------------+
| Language | DSL (.pp) | Ruby (.rb) | YAML |
| Agent | Required | Required | Optional |
| Push/Pull | Pull | Pull | Push |
| Learning Curve | Moderate | Steep | Easy |
| Enterprise | Yes | Yes | Yes |
| Idempotent | Yes | Yes | Yes |
+------------------------------------------------------------------+

In this chapter, you learned:

  • ✅ Puppet architecture and manifests
  • ✅ Puppet resources and modules
  • ✅ Chef architecture and cookbooks
  • ✅ Chef recipes and resources
  • ✅ Tools comparison

Chapter 50: Infrastructure as Code


Last Updated: February 2026