David Moreau Simard

3 minute read

If you use Puppet at all, you should be using Hiera if you’re not already !

Hiera is a powerful key/value lookup tool for configuration data for puppet. Here’s some quick tips on how you can use it.

Puppet 3 ships with Hiera by default so that’s a good start…

Using Hiera

For the first example I’ll be showing in this post, I’ll be using the following simplistic files and configuration:

  • Puppet in /etc/puppet
  • Hiera configuration file in /etc/puppet/hiera.yaml
---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppet/hiera
:hierarchy:
  - global
:logger: console
  • Hiera values inside /etc/puppet/hiera/global.yaml
  • A test manifest in /etc/puppet/manifests/test.pp

Automatic lookup

Let’s pretend you want to send the URL “http://domain.com/index.html“ to a test class. You could do something like this:

global.yaml

protocol: 'http://'  
hostname: 'domain.com'  
path: '/index.html'  

test.pp

class test (
  $protocol,
  $hostname,
  $path,
) {
  notify { 'Print an URL':
    message => "${protocol}${hostname}${path}"
  }
}
 
class { 'test':
  protocol => hiera('protocol'),
  hostname => hiera('hostname'),
  path     => hiera('path')
}

And it would work - you’d get the following:

# puppet apply test.pp
Notice: http://domain.com/index.html Notice: /Stage[main]/Test/Notify[Print an URL]/message: defined 'message' as 'http://domain.com/index.html' Notice: Finished catalog run in 0.05 seconds

Now, let’s use something awesome instead: Automatic lookup.
This allows you to set the variables for your classes right within Hiera. Essentially, puppet will look in Hiera if the variable can be found there when compiling the catalog.

global.yaml

test::protocol: 'http://'  
test::hostname: 'domain.com'  
test::path: '/index.html'  

The above sets these variables for the test class. Thus:
test.pp

class test (
  $protocol,
  $hostname,
  $path,
) {
  notify { 'Print an URL':
    message => "${protocol}${hostname}${path}"
  }
}
 
class { 'test': }

Let’s look at the result - it’s the same thing !

# puppet apply test.pp
Notice: http://domain.com/index.html Notice: /Stage[main]/Test/Notify[Print an URL]/message: defined 'message' as 'http://domain.com/index.html' Notice: Finished catalog run in 0.05 seconds`

Anchors

Another cool trick is that you can declare an anchor on a Hiera variable.
You can think of it as declaring a variable and it’s value in any programming language.

Using the anchor ensures that the value is the same wherever it is used and simplifies your life whenever you need to change that value - you’ll only need to change it at one place.

It’s simple: you declare an anchor before the value of your variable and you can use it afterwards an asterix (*) as such:

# Value for variable is 'value'.  
# Using \*anchorname elsewhere will also yield 'value'.  
variable: &anchorname 'value'  
# This also works even if you're using automatic lookup syntax  
someclass::somevar: &anotheranchor 'anothervalue'

otherclass::othervar: *anchorname  
anotherclass::anothervar: *anotheranchor  

Interpolation

Interpolation lets you call Hiera variables or define values which are returned by a function or a fact.

At it’s simplest, you could use that method a bit like you would with anchors:

variable: 'value'  
class::somevar: %{variable}  

But then, you can also do something like this:

hostname: %{::fqdn}  

Or concatenate various values right within hiera:

protocol: 'http//'  
hostname: 'domain.com'  
path: '/index.html'

address: %{protocol}%{hostname}%{path}  

That’s it for now.
Leave a comment if you have any questions !