Environments, site.pp, Node Classification, ENC and Code Deployment Workflow

Puppet Training Course 24 min min read Updated: Nov 17, 2026 Intermediate
Environments, site.pp, Node Classification, ENC and Code Deployment Workflow
Intermediate Topic 2 of 3

What Is an Environment?

An environment is an isolated copy of your Puppet code and data. Typical names are production, staging and development. A node belongs to exactly one environment at a time. Environments let you test a change on a few nodes before you give it to everyone.

text /etc/puppetlabs/code/environments/ production/ default environment environment.conf hiera.yaml data/ manifests/site.pp modules/ downloaded modules (r10k) site-modules/ your modules staging/ feature_new_ntp/ one environment per Git branch
ini # environment.conf (optional) modulepath = site-modules:modules:$basemodulepath manifest = manifests environment_timeout = unlimited # cache code until a deploy refreshes it config_version = /usr/local/bin/git-version.sh

Choosing an Agent's Environment

MethodHow
Defaultproduction
On the nodeenvironment = staging in puppet.conf or puppet config set environment staging
One timepuppet agent -t --environment feature_x
From the server (ENC)The classifier returns environment: staging. Server wins over the agent setting, which is the safest and recommended approach when you use an ENC
From trusted dataSet via certificate extension pp_environment and read in your ENC or site.pp

site.pp: The Classification Entry Point

Puppet reads manifests/site.pp for every node. Use it for node definitions and global settings.

puppet # manifests/site.pp # Global defaults for resources File { backup => false, owner => 'root', group => 'root' } # Exact name node 'db01.example.com' { include role::database } # Regular expression node /^web\d+\.example\.com$/ { include role::webserver } # Several names node 'app01.example.com', 'app02.example.com' { include role::appserver } # Everything else node default { include role::base }
Only one node block matches: Puppet picks one node definition: an exact name first, then the first matching regex in file order, then default. Code outside node blocks applies to all nodes. Do not rely on matching multiple blocks.

Classification with Data (No Node Blocks)

A popular design is to avoid node blocks entirely and pick the role from a certificate extension or from Hiera.

puppet # manifests/site.pp lookup('classes', Array[String], 'unique', []).include # data/role/webserver.yaml: # classes: # - role::webserver # data/common.yaml: # classes: # - profile::base
puppet # Role from the trusted certificate extension, with a safe fallback $role = pick($trusted['extensions']['pp_role'], 'base') include "role::${role}"

External Node Classifier (ENC)

An ENC is a script or service that the server runs for every node. It returns YAML with the classes, parameters and environment for that node. It lets a CMDB, an inventory app or a UI decide classification.

bash #!/bin/bash # /usr/local/bin/enc.sh called with the certname as $1 case "$1" in web*) cat <<EOF --- environment: production classes: role::webserver: parameters: datacenter: mumbai EOF ;; *) cat <<EOF --- environment: production classes: role::base: EOF ;; esac
ini # puppet.conf on the SERVER [server] node_terminus = exec external_nodes = /usr/local/bin/enc.sh
ENC outputMeaning
classesMap of class names. Parameters can be listed under each class
parametersTop-scope variables for the node ($datacenter)
environmentEnvironment for this node, overrides the agent's setting
yaml --- environment: production classes: profile::nginx: port: 8080 # class parameter (resource-like declaration) profile::base:

Puppet Enterprise includes its own classifier in the console. The same ideas apply (node groups, classes, data, rules).

Which Method to Choose?

ApproachBest when
site.pp node blocksSmall setups, labs
Role from trusted factCloud, automation, no per-node lists
Hiera classes keyData-driven teams that avoid node blocks
ENC or consoleLarge estates with a CMDB or UI-based management

Code Promotion Workflow

text feature branch ──► CI (lint, unit tests) ──► deploy as environment `feature_x` │ │ │ test nodes run: puppet agent -t --noop --environment feature_x ▼ ▼ pull request review ──► merge to `staging` ──► staging nodes follow automatically │ ▼ merge to `production` ──► r10k/Code Manager deploy ──► canary nodes ──► all nodes
bash # Developer testing one node against a branch (no merge needed) sudo puppet agent -t --noop --environment feature_new_ntp sudo puppet agent -t --environment feature_new_ntp # See what the change would do on production without applying it sudo puppet agent -t --noop # Compile a catalog on the server for another node, to compare before and after puppet catalog compile web01.example.com --environment production > /tmp/prod.json puppet catalog compile web01.example.com --environment feature_new_ntp > /tmp/feature.json diff <(jq -S .resources /tmp/prod.json) <(jq -S .resources /tmp/feature.json)

Canary Rollouts

  • Create a small group of canary nodes that use production code first (or have their own branch)
  • Watch their reports in PuppetDB or the console for 30 to 60 minutes
  • Then move the change to everyone
  • For risky changes, set noop => true on those nodes only and review the planned changes
Node-level noop: A simpler built-in approach is to set noop = true in the agent's puppet.conf, or to apply the noop metaparameter on individual resources: package { 'x': noop => true }.

Environment Caching

Puppet Server caches environment code for speed. With environment_timeout = unlimited, the cache is flushed by the deploy tool (r10k or Code Manager calls the environment-cache API). If code changes by hand and nothing seems to update, flush it:

bash curl -i --cert $(puppet config print hostcert) --key $(puppet config print hostprivkey) \ --cacert $(puppet config print localcacert) \ -X DELETE https://puppet.example.com:8140/puppet-admin-api/v1/environment-cache # or: sudo systemctl reload puppetserver

Practice Tasks

  1. Create staging and production environments with different Hiera data and run an agent against each
  2. Write a site.pp with an exact node, a regex node and a default node, and test which one matches with puppet apply --certname
  3. Write a small ENC script and configure the server to use it
  4. Compare compiled catalogs for two environments with puppet catalog compile
  5. Define your own promotion workflow on paper: branches, tests, canary and rollback

Conclusion

Environments isolate code and data. Classify nodes with roles from trusted facts, Hiera or an ENC, test changes on a branch environment with --noop, then promote through staging and canary nodes to production.

Quick Summary: environment per branch; one node block matches; trusted-fact roles; ENC returns classes/params/env; noop + canary.

Get Newsletter

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