Puppet Hiera Configuration

Hiera is Puppet’s built-in key/value data lookup system. By default, it uses simple YAML or JSON files, although you can extend it to work with almost any data source. Hiera is the most flexible way to get configuration data into Puppet. Hiera is immensely powerful, and with great power comes great responsibility. Specifically, you’re responsible for making your infrastructure maintainable and legible, both for your co-workers and for your future self.

Let’s Started

The default location for the global hiera.yaml is $confdir/hiera.yaml. Depending on your platform, that’s usually at /etc/puppetlabs/puppet/hiera.yaml. You can use the hiera_config setting in puppet.conf to change the location of the global hiera.yaml.

root@puppet:~# cat /etc/puppetlabs/puppet/puppet.conf 
....
....
[master]
vardir = /opt/puppetlabs/server/data/puppetserver
logdir = /var/log/puppetlabs/puppetserver
rundir = /var/run/puppetlabs/puppetserver
pidfile = /var/run/puppetlabs/puppetserver/puppetserver.pid
codedir = /etc/puppetlabs/code
hiera_config = /etc/puppetlabs/code/environments/production/hiera.yaml 
....
....
root@puppet:~# 

Hiera Config

Hiera’s config file is called hiera.yaml. It configures the hierarchy for a given layer of data.

root@puppet:~# cat /etc/puppetlabs/code/environments/production/hiera.yaml 
---
:merge_behavior: deeper
:backends:
 - yaml
:logger: console
:yaml:
 :datadir: "/etc/puppetlabs/code/environments/%{environment}/hieradata"
:hierarchy:
 - "common"
root@puppet:~#

root@puppet:~# cat /etc/puppetlabs/code/environments/production/hieradata/common.yaml 
---
krishna: "1000"
chandra: "5000"
prajapati: "10000"
root@puppet:~# 

After modifying hiera configs, restart puppetserver services.

Accessing hiera data using cli

Once you have the hiera data ready in the puppet server, you can check the values using hiera CLI.

root@puppet:~# hiera -c /etc/puppetlabs/code/environments/production/hiera.yaml prajapati environment=production
10000
root@puppet:~# 
root@puppet:~# hiera -c /etc/puppetlabs/code/environments/production/hiera.yaml krishna environment=production
1000
root@puppet:~# 
root@puppet:~# 
(This record doesn't exist in hiera, therefore it's giving nil)
root@puppet:~# hiera -c /etc/puppetlabs/code/environments/production/hiera.yaml chandr environment=production
nil
root@puppet:~# 
root@puppet:~# hiera -c /etc/puppetlabs/code/environments/production/hiera.yaml chandra environment=production
5000
root@puppet:~#

Conclusion:

We have successfully setup hiera. Now, hiera is ready to serve the parameters.

Enjoy !!!

Leave a Reply

Your email address will not be published. Required fields are marked *