Home > Puppet > Hiera + Puppet = winning combination

Hiera + Puppet = winning combination

Lean on me,
when you’re not strong
and I’ll be your friend
I’ll help you carry on
(Bill Withers)

I’m currently in the middle of a process of implementing puppet as configuration management, so every now and then I still get surprised at some nice things it has to offer. When writing puppet manifests for heterogenic environments, one has to assure that manifests can be reusable for different purposes. For example, not every host in our environment has same NTP servers, although configuration is pretty much the same. Writing two classes is out of question. So, one has to either use parametrized classes and inherit parameters or use variables and set them up in code. Both ways assume mixing code with data. As the code grows, so does the reconfiguration become an issue. How to find data, and not disturb the code? Welcome, Hiera!
Hiera is rubygem for implementing hierarchical data stores. What does it mean? It means that if queried value isn’t found in top level datastore, hiera will proceed deeper in the hierarchy. First answer will be accepted. This is ideal for puppet, so they have now been connected. As of 3.0 hiera will be a part of Puppet, but even if you use stable version (as I do) you can install hiera as an addon. I’ve built RPM’s of two rubygems, because I hate deploying software that’s not packaged. So first step to install hiera is (you have to have SRCE yum repos to make this work):

# yum install rubygem-hiera-puppet

which will also pull rubygem-hiera as a dependency. Next step is to link hiera’s directory to puppet master’s modulepath. To check which modulepath you use, run the following command:

# puppet master --configprint modulepath

In my case, it’s /etc/puppet/modules and /usr/share/puppet/modules. So:

# cd /usr/share/puppet/modules
# ln -s /usr/lib/ruby/gems/1.8/gems/hiera-puppet-0.3.0 hiera-puppet

Now we need to tell puppet to create libraries in /var/lib/puppet:

# puppet agent -t --pluginsync

This will produce some errors if you don’t manage your puppet master machine with puppet, but will create files that are needed for Hiera to work.
Now, let’s make a datastore. First, create /etc/puppet/hiera.yaml, that looks something like this:

---
:hierarchy:
    - %{hostname}
    - %{operatingsystem}
    - common
:backends:
    - yaml
:yaml:
    :datadir: '/etc/puppet/hieradata'

Also don’t forget to create /etc/puppet/hieradata directory. This configuration says to hiera that data is stored in yaml, and in three levels of hierarchy. First level is per hostname, second is per operating system and third is common. So you will have the following tree:

# find /etc/puppet/hieradata
/etc/puppet/hieradata
/etc/puppet/hieradata/myhost1.yaml
/etc/puppet/hieradata/myhost2.yaml
/etc/puppet/hieradata/CentOS.yaml
/etc/puppet/hieradata/Debian.yaml
/etc/puppet/hieradata/common.yaml

This is the example of common.yaml:

---
ntpserverip: 10.200.211.5

If I define ntpserverip in some of the top layers (OS or hostname), then that value will be used and not the value from common.yaml. So common is in this case a default which will be used is any other value isn’t defined.
And that’s it, to use it in your manifests, simply assign the value of hiera’s function output to some variable:

$ntpserver = hiera('ntpserverip')

and that’s it! You can lean on Hiera 😉

Advertisements
Categories: Puppet Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: