Puppet Classes, Defined Types, Parameters, Resource Ordering and Relationships

Puppet Training Course 28 min min read Updated: Nov 17, 2026 Intermediate
Puppet Classes, Defined Types, Parameters, Resource Ordering and Relationships
Intermediate Topic 3 of 4

Classes

A class is a named block of Puppet code that groups related resources. Defining a class does nothing by itself. It becomes active when you declare it.

puppet class ntp { package { 'chrony': ensure => installed, } file { '/etc/chrony/chrony.conf': ensure => file, content => "pool pool.ntp.org iburst\n", require => Package['chrony'], notify => Service['chrony'], } service { 'chrony': ensure => running, enable => true, } } # Declare it include ntp

Class Parameters

puppet class ntp ( Array[String] $servers = ['0.pool.ntp.org', '1.pool.ntp.org'], Boolean $manage_service = true, String $package_name = 'chrony', ) { package { $package_name: ensure => installed, } file { '/etc/chrony/chrony.conf': ensure => file, content => epp('ntp/chrony.conf.epp', { 'servers' => $servers }), notify => $manage_service ? { true => Service['chrony'], default => undef }, } if $manage_service { service { 'chrony': ensure => running, enable => true, } } }

Two Ways to Declare a Class

puppet # 1. include: no parameters in the call. Safe to repeat. Values come from Hiera or defaults include ntp include ntp # fine, declared once # 2. resource-like declaration: sets parameters directly. Can only be used ONCE per class class { 'ntp': servers => ['time.example.com'], }
Prefer include with Hiera: A resource-like class { 'ntp': } fails with a duplicate declaration error if the class was already included elsewhere. Using include and putting parameter values in Hiera (ntp::servers: [...]) avoids ordering problems.

Automatic Parameter Lookup (Data Binding)

For every parameter of a class, Puppet automatically looks in Hiera for the key classname::parametername. If it finds a value it uses it. Otherwise it uses the default in the class.

yaml # data/common.yaml ntp::servers: - time1.example.com - time2.example.com ntp::manage_service: true

Class Inheritance and Contain

puppet class base { package { 'curl': ensure => installed } } class base::extra inherits base { Package['curl'] { # override an attribute of the parent resource ensure => latest, } }

Inheritance is rarely used. Prefer composition: one class contains or includes another.

puppet class profile::webserver { contain profile::webserver::install # contain makes the class part of this class for ordering contain profile::webserver::config contain profile::webserver::service Class['profile::webserver::install'] -> Class['profile::webserver::config'] ~> Class['profile::webserver::service'] }
include vs contain: include declares a class, but it is not ordered with the parent. contain also declares it and makes it a part of the parent, so ordering a parent class relative to another class will include its children.

Defined Types: Repeatable Components

A defined type is like a class that you can declare many times with different titles. It is great for things like virtual hosts, users with SSH keys or cron entries.

puppet define webapp::vhost ( String $docroot, Integer[1,65535] $port = 80, Optional[String] $server_name = undef, ) { $real_name = pick($server_name, $title) # $title is the name given at declaration file { $docroot: ensure => directory, owner => 'www-data', } file { "/etc/nginx/conf.d/${title}.conf": ensure => file, content => epp('webapp/vhost.conf.epp', { 'server_name' => $real_name, 'port' => $port, 'docroot' => $docroot, }), notify => Service['nginx'], } } webapp::vhost { 'shop.example.com': docroot => '/var/www/shop', port => 8080, } webapp::vhost { 'blog.example.com': docroot => '/var/www/blog', }
ClassDefined type
How many timesOnce per node (singleton)As many times as you want
Declared withinclude or class { }Like a resource: name { 'title': }
NamingLowercase class nameTitle must be unique for each instance
Use forServices and components that exist onceRepeated things: vhosts, users, mounts
Resources inside defined types: Always include the $title (or $name) in inner resource titles, or two instances will create duplicates.

Resource Ordering

Puppet reads the whole manifest, builds a graph, and applies resources in an order that respects dependencies. Without a relationship, the order is not guaranteed (although it is stable between runs).

MetaparameterMeaning
require => XThis resource runs after X
before => XThis resource runs before X
notify => XBefore X, and if this resource changes, refresh X (restart or reload a service)
subscribe => XAfter X, and refresh this resource if X changes
puppet package { 'nginx': ensure => installed } file { '/etc/nginx/nginx.conf': ensure => file, source => 'puppet:///modules/nginx/nginx.conf', require => Package['nginx'], # install first notify => Service['nginx'], # restart when the file changes } service { 'nginx': ensure => running, enable => true, }

Chaining Arrows

puppet Package['nginx'] -> File['/etc/nginx/nginx.conf'] ~> Service['nginx'] # -> ordering arrow (runs left side first) # ~> notification arrow (ordering plus refresh) # Collectors with arrows Yumrepo <| |> -> Package <| |> # all repos before all packages Class['apt::update'] -> Package <| provider == 'apt' |>

Refresh: How Restarts Work

  • A refresh event is sent only when the notifying resource actually changed something
  • A service receiving a refresh restarts it (or runs your restart command)
  • An exec can use refreshonly => true so it runs only on refresh
  • No change means no restart. This is the safe, idempotent behaviour
puppet file { '/etc/app/schema.sql': ensure => file, source => 'puppet:///modules/app/schema.sql', notify => Exec['load-schema'], } exec { 'load-schema': command => '/usr/bin/mysql app < /etc/app/schema.sql', refreshonly => true, # only when the file changes }

Dependency Cycles

text Error: Failed to apply catalog: Found 1 dependency cycle: (File[/a] => File[/b] => File[/a]) Try the '--graph' option and opening the resulting '.dot' file in OmniGraffle or GraphViz

A cycle means A requires B and B requires A, directly or through other resources. Remove one of the relationships, or split the logic. Use puppet apply --graph to draw the graph.

Run Stages (Use Rarely)

Puppet has run stages (pre, main, post) as a blunt way to order whole classes. They cause hidden problems with modules and are discouraged. Prefer relationships between classes.

Tags

puppet package { 'nginx': ensure => installed, tag => 'webstack' } # run only tagged resources: # puppet agent -t --tags webstack # puppet apply site.pp --tags webstack

Practice Tasks

  1. Write a class motd with a parameter message and apply it with and without Hiera data
  2. Write a defined type app::user that creates a user and a home directory, then declare it three times
  3. Create nginx with package, config file and service, and prove with touch or content changes that the service restarts only on change
  4. Create a dependency cycle on purpose and read the error
  5. Convert a manifest from require to chaining arrows

Conclusion

Classes group resources, parameters make them reusable, and defined types let you repeat a pattern. Relationships (require, notify, arrows) control order and restarts, and contain keeps class ordering predictable.

Quick Summary: include + Hiera; class parameters typed; define for repeated things; require/before/notify/subscribe; -> and ~>; contain; no cycles.

Get Newsletter

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