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
hiera.yaml Version 5
| Key | Meaning |
|---|---|
version: 5 | Always 5 for current Puppet |
defaults | Default backend and datadir for every level |
hierarchy | Ordered list. First found wins for normal lookups |
name | Label for humans (appears in --explain) |
path / paths / glob / globs | File names using interpolation like %{facts.os.family} |
data_hash | yaml_data, json_data, eyaml_lookup_key (encrypted) |
lookup_key, data_dig | Other backend types |
%{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
| Layer | File | Used for |
|---|---|---|
| Global | /etc/puppetlabs/puppet/hiera.yaml | Rare. Data that applies to all environments |
| Environment | environments/production/hiera.yaml plus data/ | Main place for your data (in the control repo) |
| Module | modules/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
'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.
The lookup() Function
Merge Strategies
Normally Hiera returns the first value found. For hashes and arrays, you may want to combine values from several levels.
| Strategy | Result |
|---|---|
first (default) | The first level that has the key wins. Nothing else is read |
unique | Combine arrays from all levels, remove duplicates |
hash | Merge top-level keys of hashes, higher level wins on conflicts |
deep | Merge nested hashes recursively (and arrays unless knockout_prefix) |
Set the strategy in the data: lookup_options
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
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
Common Mistakes
| Mistake | Fix |
|---|---|
| Hierarchy file does not exist, value not found | Hiera 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 level | Set lookup_options or pass a merge strategy |
Using legacy %{::hostname} in data | Use %{facts.networking.hostname} |
| Facts used in hierarchy not available | A new node can have a fact missing. Provide fallback levels, and prefer trusted facts |
| Putting secrets in plain YAML | Use hiera-eyaml or a secret backend (next lesson) |
| Too many hierarchy levels | Keep 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 --explainfor several nodes - Do not use the node name too much. Roles and data centres scale better
Practice Tasks
- Create
hiera.yamlwith levels node, role, os and common, plus data files - Give a class three parameters and set them from Hiera for two different nodes using
puppet lookup --nodewith--facts - Set
lookup_optionsso anadminsarray merges from all levels - Use
--explainto find why a key returns an unexpected value - 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.

