HIERA THE SINGLE SOURCE OF TRUTH FOR PUPPET
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
implementation.
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.
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
formats.
Lookup Functions:
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.
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 {
‘/etc/motd’:
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.
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.
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.
YAML: YAML Ain't Markup Language :What it is: - Human friendly - Data-serialization standard - For programming languages |
:hierarchy:
- "nodes/%{::hostname}"
- "nodes/%{facts.os.hostname}"
:hierarchy:
- "nodes/%{facts.networking.hostname}"
- "%{facts.os.family}”
- common
:backends: yaml
:yaml:
:datadir: "/etc/puppetlabs/code/environments/hieradata"
Defining a Hiera Configuration
Creating a new hiera.yaml from scratch helps us understand the settings
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
When using scalar strings in YAML they can be unquoted: ntp::ntp_local_server: 192.168.0.3
However PLEASE quote string always to avoid confusion with Boolean values
ntp::ntp_local_server: '192.168.0.3'
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
ntp::ntp_regional_server:
- 'server1'
- 'server2'
nodes/centos7.yaml RedHat.yaml common.yaml |
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
Thank you!