Puppet Roles and Profiles Pattern: Organise Code for Real Infrastructure

Puppet Training Course 24 min min read Updated: Nov 17, 2026 Intermediate
Puppet Roles and Profiles Pattern: Organise Code for Real Infrastructure
Intermediate Topic 3 of 4

The Problem

Imagine 200 servers. Some are web servers, some are databases, some are build machines. If you list classes directly in site.pp for each node, you get a huge file with copy-paste everywhere. The roles and profiles pattern solves this with simple layers.

The Layers

text Node (web01.example.com) └─ Role role::webserver "what this machine IS" (business function) ├─ Profile profile::base "how we configure common technology" ├─ Profile profile::nginx └─ Profile profile::app_runtime └─ Component modules puppet-nginx, puppetlabs-stdlib, puppetlabs-apt ... (generic, reusable) Hiera data values for profiles, per environment, role, node
LayerJobRules
Component moduleManages one technology in a generic way (nginx, postgresql)From the Forge or written by you. No company-specific choices
ProfileConfigures one technology stack our way, using component modulesContains logic and looks up data from Hiera. Each profile is one class
RoleNames a business function by including profilesOnly include profile::x lines. No logic and no parameters
NodeGets exactly one roleinclude role::webserver in site.pp or an ENC

Component Module (Generic)

These are the Forge modules or your own, such as puppet-nginx. They offer classes and defined types with parameters. You do not edit them.

Profile: Your Technology Stack

puppet # site-modules/profile/manifests/base.pp class profile::base { include profile::base::ntp include profile::base::ssh include profile::base::users ensure_packages(['vim', 'curl', 'htop']) } # site-modules/profile/manifests/nginx.pp class profile::nginx ( Integer[1,65535] $port = 80, Hash $vhosts = {}, ) { class { 'nginx': manage_repo => true, } $vhosts.each |$name, $cfg| { nginx::resource::server { $name: listen_port => $port, www_root => $cfg['root'], } } firewall { "100 allow nginx ${port}": dport => $port, proto => 'tcp', jump => 'accept', } }
Look up data in profiles: A profile class can use class { 'nginx': } with parameters, because it is the only place that declares it. Parameters come from the class parameters and from Hiera (profile::nginx::vhosts).

Role: The Business Function

puppet # site-modules/role/manifests/webserver.pp class role::webserver { include profile::base include profile::nginx include profile::app_runtime } # site-modules/role/manifests/database.pp class role::database { include profile::base include profile::postgresql include profile::backup } # site-modules/role/manifests/webserver/staging.pp class role::webserver::staging { include role::webserver include profile::debug_tools # small difference as a sub-role }

Classification: Node Gets a Role

puppet # manifests/site.pp node /^web\d+\.example\.com$/ { include role::webserver } node /^db\d+\.example\.com$/ { include role::database } node default { include role::base # a minimal role: just profile::base }

Often the role comes from a trusted fact so no node list is needed. A certificate extension pp_role (set at agent install time) or a custom fact decides the role.

puppet # manifests/site.pp $role = $trusted['extensions']['pp_role'] if $role { include "role::${role}" } else { include role::base }

Hiera Data for Profiles

yaml # hiera.yaml (environment level) --- version: 5 defaults: datadir: data data_hash: yaml_data hierarchy: - name: 'Per node' path: 'nodes/%{trusted.certname}.yaml' - name: 'Per role' path: 'role/%{trusted.extensions.pp_role}.yaml' - name: 'Per OS' path: 'os/%{facts.os.family}.yaml' - name: 'Common' path: 'common.yaml' # data/common.yaml profile::nginx::port: 80 # data/role/webserver.yaml profile::nginx::vhosts: shop.example.com: root: /var/www/shop # data/nodes/web01.example.com.yaml profile::nginx::port: 8080

Rules to Follow

RuleReason
A node has one roleKeeps meaning clear. Variations become new roles or Hiera data
Roles only include profilesNo logic, no parameters, no resources
Profiles include or declare component modulesOne place that decides how a technology is configured
Data lives in Hiera, not in profile codeChange values without changing code
Do not include a role from a profileLayers go downward only
Do not use class { 'x': } for one class in two profilesDuplicate declaration. Use include and Hiera instead
Name profiles by technology (profile::postgresql)Not by environment or node

Anti-Patterns

  • Putting if $hostname == 'web01' in profile code. Put the difference in Hiera
  • A role per node (role::web01). That is just node-based classification again
  • Profiles that are huge. Split them (profile::base::ssh)
  • Setting parameters of component modules from site.pp
  • Using inheritance between profiles
  • Many roles that differ by one profile. Use data to switch the behaviour instead

Full Mini Example

puppet class profile::postgresql ( String $version = '16', Sensitive[String] $admin_password, Hash $databases = {}, ) { class { 'postgresql::globals': version => $version, } class { 'postgresql::server': postgres_password => $admin_password, listen_addresses => '*', } $databases.each |$db, $cfg| { postgresql::server::db { $db: user => $cfg['user'], password => postgresql::postgresql_password($cfg['user'], $cfg['password']), } } }
yaml # data/role/database.yaml profile::postgresql::version: '16' profile::postgresql::databases: shop: user: shop_app password: ENC[PKCS7,Mm4...] # encrypted with hiera-eyaml

Practice Tasks

  1. Create profile::base, profile::nginx and role::webserver in a local site-modules directory
  2. Classify two fake nodes with site.pp and apply with puppet apply --modulepath
  3. Move the nginx port and vhosts into Hiera data and override per node
  4. Add role::database and make site.pp select the role from a custom fact
  5. Refactor a copy-pasted site.pp into roles and profiles

Conclusion

Use component modules for technologies, profiles to configure them your way with Hiera data, roles to name the business function, and give each node one role. This keeps classification simple and code reusable.

Quick Summary: component -> profile -> role -> node; one role per node; roles only include; data in Hiera; no logic in site.pp.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators