Puppet Hiera

by scmGalaxy.com

About Me


Puppet 4: Hiera the Single Source of Truth


Module Overview

  • Lab Environment
  • Pluralsight Courses
  • Course Contents

Lab Environment

Puppet 4 Series

This course follows on from the Puppet 4: Working with
Files and Templates course. In this course you will learn
to add Hiera to Puppet to abstract Puppet code from the

And That Means……?

Without modification to the Puppet code or passing
parameters to classes, Hiera provides a lookup data-
source. Enabling custom values to be retrieved from
configuration data rather than within the code.

Course Overview

  1. Puppet 4: Language Essentials
  2. Puppet 4: Working with Modules and Classes
  3. Puppet 4: Working with Files and Templates
  4. Puppet 4: Hiera the Single Source of Truth
  5. Puppet 4: Server and Puppet Enterprise

Puppet 4: Hiera the Single Source of Truth

Hiera in Puppet

Configuration: The Hiera configuration
file is often known as the hiera.yaml file.
The file is used to configure the hierarchy
of backends to be used for lookups.

Backends: Can be written in yaml or json

Lookup Functions:

  • hiera()
  • hiera_array ()
  • hiera_hash()

Automatic Parameter Lookup: Puppet
automatically retrieves class parameters
from Hiera. This is regular and
predictable and anyone downloading a
module can reference the first line of a
class to determine the keys that need to
be set.


Next up: Getting Started with Hiera

Getting Started with Hiera


  • Creating a new and simple MOTD module
  • Confirming the hiera.yaml.
  • Editing the data source.

Hiera Abstracts Implementation from Code

By reading the first lines from the class definitions we
can view input parameters. Without modification of the
code hiera searches for the custom values from local data sources.

	$ cd /etc/puppetlabs/code/environments/production/modules
	$ sudo rm -rf motd
	$ sudo mkdir -p motd/manifests  
	$ sudo vim motd/manifests/init.pp

Create the MOTD Module

From the start let’s create a very simple MOTD module that makes use of hiera.

	class motd
	$message = "Welcome to Pluralsight",
		file {
	ensure => ‘file’,
	content => $message,

Define MOTD in the init.pp

The class accepts just one argument that provides the text to add to the message of the day file. If a supplied value is not available then we provide a default.

	$ sudo puppet apply -e ‘include motd’

Declare to Class

We want to be to execute the code without any modification. Ideally just with the include statement. Without configuring hiera the default value from the class is used.

	$ sudo puppet config print confdir
	$ cat /etc/puppetlabs/puppet/hiera.yaml

Configuring Hiera.yaml

In Puppet 4 the configuration directory stores the hiera.yaml file and we can make use of the defaults found in the file.

	$ sudo puppet config print environmentpath environment
	$ cd /etc/puppetlabs/code/environments/production/hieradata
	$ sudo vim common.yaml
	motd::message: "This is a development system for devops"

Configuring a Data Source

The hiera.yaml is used to create the hierarchy of the lookups and to identify the location of the lookup files. The default location is the hieradata directory within the environment. The catch-all file is the common.yaml.

The MOTD Hiera Solutuion

In this module we have been able to see how we can build a simple module to make use of Hiera without modification to the code.

Downloading a module we only need to check the class for parameter names to populate in the data source.

Next up: Hiera Configuration

Hiera Configuration


  • Locating and relocating the hiera.yaml
  • What is YAML
  • Creating a hiera.yaml

Hiera Configuation

The configuration file for Hiera is referred to as the hiera.yaml and is used to configure:
The lookup heirarchy
The data backends to use
Settings for each backend

	$ sudo puppet config print hiera_config


Configuration Location

By default the configuration file for hiera is:
/etc/puppetlabs/puppet/hiera.yaml. Hiera will revert to its defaults if the configuration file cannot be found or is empty. Puppet will issue a warning if the file does not exist.

	$ sudo puppet config set hiera_config <new path>


Changing the Location

To change the file location you can use thehiera_config setting in the puppet.conf.

Locating the Hiera Configuration


YAML: YAML Ain't Markup Language
:What it is:
- Human friendly
- Data-serialization standard
- For programming languages


- "nodes/%{::hostname}"
- "nodes/%{facts.os.hostname}"

	-  "nodes/%{facts.networking.hostname}"
	-  "%{facts.os.family}”
	-  common
	:backends: yaml
	:datadir: "/etc/puppetlabs/code/environments/hieradata"

Defining a Hiera Configuration

Creating a new hiera.yaml from scratch helps us understand the settings

Creating a Hiera Configuration

In which file can we specify the location of the hiera configuration? An alternative location can be set in the puppet.conf. We can edit the file directly or with sudo puppet config set hiera_config

What sequence of characters are used to represent the start of a YAML document
The three hyphens ---

In YAML how do we represent a sequence?
Indented lines starting with a hyphen. The indent groups the sequence together.
- one
- two
- three

Next up: Writing the Data Source

Writing the Data Source


  • Scalar strings in YAML
  • Boolean values in YAML
  • Arrays in YAML
  • Using the lookup hierarchy


When using scalar strings in YAML they can be unquoted: ntp::ntp_local_server:
However PLEASE quote string always to avoid confusion with Boolean values
ntp::ntp_local_server: ''
ntp::monitor: false


When using Boolean values in YAML: do not quote them:
ntp::monitor: false The values can be yes, no, true, false, on or off


Arrays in YAML are known as sequences and are written on multiple lines. The indents DO matter
- 'server1'
- 'server2'

Writing YAML Data Sources



Investigating the Hierarchy

Booleans are unquoted strings using the values: true, false, yes, no, on or off

Strings can be unquoted but are better quoted so we can easily include the string 'on' if we need.

Arrays are multi-valued sequences across multiple lines. Each entry is indented and is preceded by a hyphen

The search hierarchy is defined by the hiera.yaml

Once a match is found the search stops

Avoid having too many levels

Next up: Command Line Verification


Thank you!