Operations 11 min read

Modular Puppet Code: Environments, Modules, and Classes

This article explains how to structure modular Puppet code by configuring environments, creating reusable modules, and designing classes, covering environment paths, hiera data, module generation, publishing to the Forge, and key class functions such as include, require, contain, and hiera_include.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Modular Puppet Code: Environments, Modules, and Classes

After introducing the basic Puppet architecture, this guide dives into writing modular Puppet code by covering the concepts of environments, hiera, modules, facts, and templates.

Environments are isolated by setting the environmentpath in puppet.conf , which points to the environments directory. The basemodulepath (or modulepath ) defines where Puppet looks for modules. Example configuration:

basemodulepath= $confdir/modules:
$confdir/environments/modules

Each environment has its own environment.conf file, where environment_timeout can be set to control how often the master refreshes environment data.

To synchronize an environment, the agent runs:

Puppet agent -t --environment=production

or

Puppet agent -t --environment=staging

Modules are the building blocks that contain classes, files, lib plugins, templates, facts, examples, specs, functions, and types. A typical module layout includes:

Manifests : .pp files, with init.pp defining the main class.

Files : resources to be copied to nodes, referenced as puppet:///modules/ /file .

Lib : custom facts and resource types.

Templates : .erb or .epp files used by the template() or epp() functions.

Facts.d : external facts available to all nodes.

Examples, Spec, Functions, Types : supporting documentation and test code.

Modules can be generated with the command:

Puppet module generate
-

and published to the Puppet Forge after adding a proper metadata.json and building the package with:

Puppet module build

Classes are defined inside manifests and can inherit parameters from a params class. They are declared using standard functions such as include , require , contain , and hiera_include , which ensure idempotent loading and proper dependency ordering.

Key class keywords and relationships include:

before : ensures the preceding resource runs before the following one.

require : the current resource waits for another to succeed.

notify : actively informs another resource of its state.

subscribe : passively reacts to changes in another resource.

-> and ~> : arrow syntax for ordering and failure handling.

In summary, by combining environments, modules, classes, and hiera data, users can achieve a highly reusable and maintainable Puppet codebase that simplifies the management of packages, configuration files, and services across development, testing, and production environments.

automationconfiguration managementDevOpsInfrastructureModulesPuppet
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.