Hiera 5: Separate Data from Code with Hierarchies, Lookups and Merge Strategies

Puppet Training Course 28 min min read Updated: Nov 17, 2026 Intermediate
Hiera 5: Separate Data from Code with Hierarchies, Lookups and Merge Strategies
Intermediate Topic 2 of 3

Why Hiera?

Without Hiera, values such as ports, package versions and server names are written inside manifests, so changing one means editing code. Hiera keeps these values in data files and picks the right value for each node using a hierarchy of files. The same code can serve development, staging and production with different data.

How a Lookup Works

text Puppet needs value for profile::nginx::port on node web01.example.com Hierarchy (first match wins, top to bottom): 1. nodes/web01.example.com.yaml <-- has profile::nginx::port: 8080 -> FOUND, use 8080 2. role/webserver.yaml 3. os/Debian.yaml 4. common.yaml If nothing is found: class default value, otherwise compile error (if no default)

hiera.yaml Version 5

yaml # /etc/puppetlabs/code/environments/production/hiera.yaml --- version: 5 defaults: datadir: data # relative to this file data_hash: yaml_data # backend: read YAML files hierarchy: - name: 'Per-node data' path: 'nodes/%{trusted.certname}.yaml' - name: 'Per-role and OS' paths: - 'role/%{trusted.extensions.pp_role}/%{facts.os.family}.yaml' - 'role/%{trusted.extensions.pp_role}.yaml' - name: 'Datacenter' path: 'dc/%{facts.datacenter}.yaml' - name: 'Operating system' path: 'os/%{facts.os.family}.yaml' - name: 'Everything else' path: 'common.yaml'
KeyMeaning
version: 5Always 5 for current Puppet
defaultsDefault backend and datadir for every level
hierarchyOrdered list. First found wins for normal lookups
nameLabel for humans (appears in --explain)
path / paths / glob / globsFile names using interpolation like %{facts.os.family}
data_hashyaml_data, json_data, eyaml_lookup_key (encrypted)
lookup_key, data_digOther backend types
Variables in paths: Use %{facts.os.family} (dot style), %{trusted.certname} and %{trusted.extensions.pp_role}. The old %{::osfamily} style is deprecated. Files that do not exist are simply skipped.

Three Layers

LayerFileUsed for
Global/etc/puppetlabs/puppet/hiera.yamlRare. Data that applies to all environments
Environmentenvironments/production/hiera.yaml plus data/Main place for your data (in the control repo)
Modulemodules/foo/hiera.yaml plus data/Defaults shipped with a module

Lookup order: global, then environment, then module. A value found in a higher layer overrides lower layers (for the normal first strategy).

Data Files

yaml # data/common.yaml --- profile::nginx::port: 80 profile::nginx::worker_processes: auto profile::base::ntp_servers: - 0.pool.ntp.org - 1.pool.ntp.org profile::app::settings: log_level: info features: new_ui: false motd::message: "Welcome to %{facts.networking.fqdn}" # data/role/webserver.yaml --- profile::nginx::port: 8080 profile::app::settings: log_level: warn # data/nodes/web01.example.com.yaml --- profile::nginx::port: 9000
YAML quoting: Quote values that look like other types, such as 'yes', '0755' or '1.10'. Use %{literal('%')} to write a literal percent sign.

Automatic Parameter Lookup

When a class has a parameter, Puppet looks up classname::parameter in Hiera automatically. You do not write any lookup code.

puppet class profile::nginx ( Integer $port = 80, # Hiera key: profile::nginx::port String $worker_processes = 'auto', # profile::nginx::worker_processes ) { ... } include profile::nginx # values from Hiera applied automatically

The lookup() Function

puppet $servers = lookup('profile::base::ntp_servers') # error if not found $level = lookup('profile::app::settings.log_level') # dotted key into a hash $opt = lookup('app::optional', { 'default_value' => 'none' }) # with default $all = lookup('profile::base::admins', Array, 'unique', []) # type, merge strategy, default $cfg = lookup('profile::app::settings', Hash, 'deep', {})

Merge Strategies

Normally Hiera returns the first value found. For hashes and arrays, you may want to combine values from several levels.

StrategyResult
first (default)The first level that has the key wins. Nothing else is read
uniqueCombine arrays from all levels, remove duplicates
hashMerge top-level keys of hashes, higher level wins on conflicts
deepMerge nested hashes recursively (and arrays unless knockout_prefix)
yaml # data/common.yaml profile::base::admins: [alice, bob] profile::app::settings: { log_level: info, features: { new_ui: false, beta: false } } # data/role/webserver.yaml profile::base::admins: [carol] profile::app::settings: { features: { new_ui: true } }
puppet lookup('profile::base::admins', Array, 'unique') # [carol, alice, bob] lookup('profile::app::settings', Hash, 'deep') # { log_level: info, features: { new_ui: true, beta: false } }

Set the strategy in the data: lookup_options

yaml # data/common.yaml lookup_options: profile::base::admins: merge: unique profile::app::settings: merge: strategy: deep '^profile::.*::users$': # regex for many keys merge: hash profile::base::admins: [alice, bob]

With lookup_options, automatic class parameter lookup merges too, so class code does not need to call lookup() with a strategy.

The puppet lookup Command

bash puppet lookup profile::nginx::port --node web01.example.com --environment production puppet lookup profile::base::admins --merge unique --node web01.example.com puppet lookup profile::nginx::port --node web01.example.com --explain # shows each level checked puppet lookup --facts facts.json profile::nginx::port # use offline facts puppet lookup profile::nginx::port --node db01 --facts db01-facts.yaml --render-as s
text Searching for "profile::nginx::port" Global Data Provider (hiera configuration version 5) Environment Data Provider (hiera configuration version 5) Hierarchy entry "Per-node data" Path "/etc/puppetlabs/code/environments/production/data/nodes/web01.example.com.yaml" Original path: "nodes/%{trusted.certname}.yaml" Path not found Hierarchy entry "Per-role and OS" Path ".../data/role/webserver.yaml" Found key: "profile::nginx::port" value: 8080
Debug with --explain: When a value is not what you expect, run puppet lookup KEY --node NAME --explain. It shows every file checked and where the value came from.

Module Data and Overrides

A module can ship defaults in its own data/. Your environment layer is searched first, so you can override module defaults without editing the module. A module can not override your environment.

Interpolation Functions in Data

yaml --- app::url: "https://%{facts.networking.fqdn}:%{lookup('app::port')}" # lookup() inside data app::home: "%{alias('base::home')}" # keep the original type app::dbhost: "%{hiera('db::host')}" # older name, still works app::literal: "100%{literal('%')}"

Common Mistakes

MistakeFix
Hierarchy file does not exist, value not foundHiera skips missing files silently. Check the path with --explain
Wrong key name (nginx::port vs profile::nginx::port)Class parameter keys must be fully::qualified::class::param
Expecting merge, getting only one levelSet lookup_options or pass a merge strategy
Using legacy %{::hostname} in dataUse %{facts.networking.hostname}
Facts used in hierarchy not availableA new node can have a fact missing. Provide fallback levels, and prefer trusted facts
Putting secrets in plain YAMLUse hiera-eyaml or a secret backend (next lesson)
Too many hierarchy levelsKeep it small: node, role, environment/datacenter, os, common

Design Tips

  • Put as much as possible in common.yaml, and override only what differs
  • Name keys after the class that uses them (profile::nginx::port)
  • Use Hiera for values, and Puppet code for logic
  • Keep the hierarchy short and documented in the repo README
  • Test with puppet lookup --explain for several nodes
  • Do not use the node name too much. Roles and data centres scale better

Practice Tasks

  1. Create hiera.yaml with levels node, role, os and common, plus data files
  2. Give a class three parameters and set them from Hiera for two different nodes using puppet lookup --node with --facts
  3. Set lookup_options so an admins array merges from all levels
  4. Use --explain to find why a key returns an unexpected value
  5. Add a module default and override it in the environment layer

Conclusion

Hiera lets you keep data outside code. Define a short hierarchy in hiera.yaml, name keys after class parameters, use merge strategies via lookup_options, and debug with puppet lookup --explain.

Quick Summary: hiera.yaml v5; first match wins; class::param auto lookup; merge unique/hash/deep; lookup_options; puppet lookup --explain.

Get Newsletter

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