Project 1: Build a Complete Web Server Stack with Roles, Profiles, Hiera and Tests
Project Goal
Build a control repository that configures web servers and a base layer for all servers. When you are finished, one command applies the full configuration to a new VM, and a second run changes nothing.
| Requirement | How |
|---|---|
| Every server gets a base setup | profile::base: time sync, packages, admin users, SSH settings, motd |
| Web servers run Nginx with a site | profile::nginx + role::webserver |
| Only needed ports are open | profile::firewall |
| Data per environment and node | Hiera: common.yaml, role/, nodes/ |
| Code is tested | puppet-lint, rspec-puppet |
| No manual steps on servers | Everything in Git |
Lab Setup
- One Linux VM (Rocky Linux 9 or Ubuntu 24.04) with 2 GB RAM, called
web01 - Puppet agent installed (Puppet Core 8 or OpenVox), no server needed. We use
puppet apply - Git and an editor on your laptop. A second VM for a Puppet server is optional (later)
bash
mkdir -p ~/control-repo/{manifests,data/{role,nodes},site-modules/{role,profile}/manifests,site-modules/profile/{templates,files}}
cd ~/control-repo && git init
Files: environment.conf and hiera.yaml
ini
# environment.conf
modulepath = site-modules:modules:$basemodulepath
yaml
# hiera.yaml
---
version: 5
defaults:
datadir: data
data_hash: yaml_data
hierarchy:
- name: 'Per node'
path: 'nodes/%{trusted.certname}.yaml'
- name: 'Per role'
path: 'role/%{facts.pp_role}.yaml' # custom/external fact for the lab (use trusted extension in production)
- name: 'Per OS family'
path: 'os/%{facts.os.family}.yaml'
- name: 'Common'
path: 'common.yaml'
Hiera Data
yaml
# data/common.yaml
---
profile::base::admin_users:
alice:
uid: 2001
groups: [wheel]
ssh_key: 'AAAAC3NzaC1lZDI1NTE5AAAAIExampleReplaceWithRealPublicKey'
ssh_key_type: ssh-ed25519
profile::base::packages: [vim, curl, htop, git, unzip]
profile::base::time_servers: [0.pool.ntp.org, 1.pool.ntp.org]
profile::firewall::open_tcp_ports: [22]
# data/os/RedHat.yaml
---
profile::base::admin_group: wheel
profile::nginx::user: nginx
profile::nginx::conf_dir: /etc/nginx/conf.d
# data/os/Debian.yaml
---
profile::base::admin_group: sudo
profile::nginx::user: www-data
profile::nginx::conf_dir: /etc/nginx/conf.d
# data/role/webserver.yaml
---
profile::firewall::open_tcp_ports: [22, 80, 443]
profile::nginx::sites:
demo.example.com:
port: 80
docroot: /var/www/demo
title: 'Hello from Puppet'
# data/nodes/web01.example.com.yaml
---
profile::nginx::worker_processes: 2
profile::base
puppet
# site-modules/profile/manifests/base.pp
class profile::base (
Hash $admin_users,
Array[String] $packages,
Array[String] $time_servers,
String $admin_group,
) {
ensure_packages($packages)
# Time sync
$chrony_pkg = 'chrony'
$chrony_svc = $facts['os']['family'] ? { 'RedHat' => 'chronyd', default => 'chrony' }
$chrony_conf = $facts['os']['family'] ? { 'RedHat' => '/etc/chrony.conf', default => '/etc/chrony/chrony.conf' }
package { $chrony_pkg: ensure => installed }
file { $chrony_conf:
ensure => file,
mode => '0644',
content => epp('profile/chrony.conf.epp', { 'servers' => $time_servers }),
require => Package[$chrony_pkg],
notify => Service[$chrony_svc],
}
service { $chrony_svc:
ensure => running,
enable => true,
}
# Admin users
$admin_users.each |$name, $cfg| {
user { $name:
ensure => present,
uid => $cfg['uid'],
groups => $cfg['groups'],
shell => '/bin/bash',
managehome => true,
}
ssh_authorized_key { "${name}@admin":
ensure => present,
user => $name,
type => $cfg['ssh_key_type'],
key => $cfg['ssh_key'],
}
}
file { '/etc/motd':
ensure => file,
mode => '0644',
content => "Managed by Puppet. Host: ${facts['networking']['fqdn']}\nUnauthorised access is prohibited.\n",
}
}
text
<%# site-modules/profile/templates/chrony.conf.epp %>
<%- | Array[String] $servers | -%>
# Managed by Puppet
<% $servers.each |$s| { -%>
pool <%= $s %> iburst
<% } -%>
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
profile::nginx
puppet
# site-modules/profile/manifests/nginx.pp
class profile::nginx (
String $user = 'nginx',
String $conf_dir = '/etc/nginx/conf.d',
Integer $worker_processes = 1,
Hash $sites = {},
) {
package { 'nginx':
ensure => installed,
}
file { '/etc/nginx/nginx.conf':
ensure => file,
mode => '0644',
content => epp('profile/nginx.conf.epp', {
'user' => $user,
'worker_processes' => $worker_processes,
'conf_dir' => $conf_dir,
}),
require => Package['nginx'],
notify => Service['nginx'],
}
$sites.each |$name, $cfg| {
file { $cfg['docroot']:
ensure => directory,
owner => $user,
mode => '0755',
}
file { "${cfg['docroot']}/index.html":
ensure => file,
owner => $user,
mode => '0644',
content => "<h1>${cfg['title']}</h1><p>Served by ${facts['networking']['hostname']}</p>\n",
}
file { "${conf_dir}/${name}.conf":
ensure => file,
mode => '0644',
content => epp('profile/site.conf.epp', {
'server_name' => $name,
'port' => $cfg['port'],
'docroot' => $cfg['docroot'],
}),
require => Package['nginx'],
notify => Service['nginx'],
}
}
service { 'nginx':
ensure => running,
enable => true,
}
}
text
<%# templates/nginx.conf.epp %>
<%- | String $user, Integer $worker_processes, String $conf_dir | -%>
# Managed by Puppet
user <%= $user %>;
worker_processes <%= $worker_processes %>;
pid /run/nginx.pid;
events { worker_connections 1024; }
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include <%= $conf_dir %>/*.conf;
}
text
<%# templates/site.conf.epp %>
<%- | String $server_name, Integer[1,65535] $port, String $docroot | -%>
server {
listen <%= $port %>;
server_name <%= $server_name %>;
root <%= $docroot %>;
index index.html;
location / { try_files $uri $uri/ =404; }
}
Default site: On some distributions the nginx package ships a default site that also listens on port 80. If both answer, remove the default file, for example
file { '/etc/nginx/conf.d/default.conf': ensure => absent } on RHEL family, or remove sites-enabled/default on Debian family. Add it to the profile after checking your system.
profile::firewall
puppet
# site-modules/profile/manifests/firewall.pp (RHEL family with firewalld; adapt for Ubuntu with ufw/nftables)
class profile::firewall (
Array[Stdlib::Port] $open_tcp_ports = [22],
) {
package { 'firewalld': ensure => installed }
service { 'firewalld':
ensure => running,
enable => true,
require => Package['firewalld'],
}
$open_tcp_ports.each |$port| {
exec { "firewalld-open-${port}":
command => "/usr/bin/firewall-cmd --permanent --add-port=${port}/tcp && /usr/bin/firewall-cmd --reload",
unless => "/usr/bin/firewall-cmd --list-ports | /bin/grep -qw ${port}/tcp",
require => Service['firewalld'],
}
}
}
A better version: The
exec above is guarded with unless, so it is idempotent. When you finish the project, replace it with the puppet-firewalld module resources (see the hardening lesson), and open ports for removal too.
Roles
puppet
# site-modules/role/manifests/base.pp
class role::base {
include profile::base
}
# site-modules/role/manifests/webserver.pp
class role::webserver {
include profile::base
include profile::nginx
include profile::firewall
}
Classification: site.pp
puppet
# manifests/site.pp
$role = $facts['pp_role'] ? {
undef => 'base',
default => $facts['pp_role'],
}
include "role::${role}"
bash
# On the VM (lab only): set the role with an external fact
sudo mkdir -p /etc/puppetlabs/facter/facts.d
echo 'pp_role=webserver' | sudo tee /etc/puppetlabs/facter/facts.d/role.txt
Run It
bash
cd ~/control-repo
puppet parser validate manifests/site.pp site-modules/*/manifests/*.pp
puppet-lint site-modules
sudo puppet module install puppetlabs-stdlib --modulepath ./modules # lab only: normally use a Puppetfile
sudo puppet apply --noop --environmentpath ~ --modulepath ./site-modules:./modules \
--hiera_config ./hiera.yaml manifests/site.pp
sudo puppet apply --modulepath ./site-modules:./modules --hiera_config ./hiera.yaml manifests/site.pp
# Second run: expect no changes
sudo puppet apply --modulepath ./site-modules:./modules --hiera_config ./hiera.yaml manifests/site.pp --detailed-exitcodes ; echo "exit: $?" # 0 means idempotent
curl -s http://localhost/
ss -tlnp | grep ':80'
sudo firewall-cmd --list-ports
Add Unit Tests
ruby
# site-modules/profile/spec/classes/nginx_spec.rb (module-style test, use pdk or rspec-puppet in the repo)
require 'spec_helper'
describe 'profile::nginx' do
let(:params) do
{ user: 'nginx', sites: { 'demo.example.com' => { 'port' => 80, 'docroot' => '/var/www/demo', 'title' => 'Hi' } } }
end
let(:facts) { { networking: { 'hostname' => 'web01' } } }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_package('nginx') }
it { is_expected.to contain_file('/etc/nginx/conf.d/demo.example.com.conf').that_notifies('Service[nginx]') }
it { is_expected.to contain_file('/var/www/demo/index.html').with_content(%r{<h1>Hi</h1>}) }
end
Extend the Project
- Add a second site with HTTPS (certificate files managed through Hiera and eyaml)
- Add
profile::monitoringwith a node exporter and open its port only to the monitoring network - Add
role::databasewith PostgreSQL from the Forge - Connect a Puppet server and agents: move the repo to Git, deploy with r10k, and classify by certificate extension
- Add a CI pipeline with lint, unit tests and an acceptance test with Litmus
Checklist
| Check | Done? |
|---|---|
| Second run reports no changes | |
| Changing the Hiera port changes the nginx file and reloads nginx | |
Deleting /etc/motd by hand is fixed on the next run | |
| Lint passes without warnings | |
| Unit tests pass for two operating systems | |
| No passwords or private keys in the repo |
Conclusion
You built a data-driven stack: Hiera data feeds profile classes, roles describe the server type, and a few small tests protect it. The same structure scales from one VM to a whole fleet.
Quick Summary: roles+profiles+Hiera; ensure_packages; templates; guarded exec; second run no change; lint + unit tests.

