Configuring Hiera
Hiera is an information repository for Puppet. Using Hiera you can have a hierarchical categorization of data about your nodes that is maintained outside of your manifests. This is very useful for sharing code and dealing with exceptions that will creep into any Puppet deployment.
Getting ready
Hiera should have already been installed as a dependency on your Puppet master. If it has not already, install it using Puppet:
root@puppet:~# puppet resource package hiera ensure=installed package { 'hiera': ensure => '1.3.4-1puppetlabs1', }
How to do it...
- Hiera is configured from a yaml file,
/etc/puppet/hiera.yaml
. Create the file and add the following as a minimal configuration:--- :hierarchy: - common :backends: - yaml :yaml: :datadir: '/etc/puppet/hieradata'
- Create the
common.yaml
file referenced in the hierarchy:root@puppet:/etc/puppet# mkdir hieradata root@puppet:/etc/puppet# vim hieradata/common.yaml --- message: 'Default Message'
- Edit the
site.pp
file and add a notify resource based on the Hiera value:node default { $message = hiera('message','unknown') notify {"Message is $message":} }
- Apply the manifest to a test node:
t@ckbk:~$ sudo puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin ... Info: Caching catalog for cookbook-test Info: Applying configuration version '1410504848' Notice: Message is Default Message Notice: /Stage[main]/Main/Node[default]/Notify[Message is Default Message]/message: defined 'message' as 'Message is Default Message' Notice: Finished catalog run in 0.06 seconds
How it works...
Hiera uses a hierarchy to search through a set of yaml files to find the appropriate values. We defined this hierarchy in hiera.yaml
with the single entry for common.yaml
. We used the hiera
function in site.pp
to lookup the value for message and store that value in the variable $message
. The values used for the definition of the hierarchy can be any facter facts defined about the system. A common hierarchy is shown as:
:hierarchy: - hosts/%{hostname} - os/%{operatingsystem} - network/%{network_eth0} - common
There's more...
Hiera can be used for automatic parameter lookup with parameterized classes. For example, if you have a class named cookbook::example
with a parameter named publisher
, you can include the following in a Hiera yaml file to automatically set this parameter:
cookbook::example::publisher: 'PacktPub'
Another often used fact is environment
you may reference the environment
of the client node using %{environment}
as shown in the following hierarchy:
:hierarchy: hosts/%{hostname} os/%{operatingsystem} environment/%{environment} common
Tip
A good rule of thumb is to limit the hierarchy to 8 levels or less. Keep in mind that each time a parameter is searched with Hiera, all the levels are searched until a match is found.
The default Hiera function returns the first match to the search key, you can also use hiera_array
and hiera_hash
to search and return all values stored in Hiera.
Hiera can also be searched from the command line as shown in the following command line (note that currently the command line Hiera utility uses /etc/hiera.yaml
as its configuration file whereas the Puppet master uses /etc/puppet/hiera.yaml
):
root@puppet:/etc/puppet# rm /etc/hiera.yaml root@puppet:/etc/puppet# ln -s /etc/puppet/hiera.yaml /etc/ root@puppet:/etc/puppet# hiera message Default Message
Note
For more information, consult the Puppet labs website at https://docs.puppetlabs.com/hiera/1/.