Ask Your Question
2

When to use hiera, hiera_array, and hiera_hash?

asked 2014-09-03 11:00:34 -0500

ramindk gravatar image

updated 2014-09-03 11:08:01 -0500

What are some example cases of when I should use hiera, hiera_array, and hiera_hash?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
8

answered 2014-09-03 11:07:46 -0500

ramindk gravatar image

The short answer is to use hiera whenever you expect to need a single lookup from a single part of your hierachy. If you need to merge value across the matching parts of your hierarchy, then you'll need hiera_hash or hiera_array.

hiera - Performs a standard priority lookup and returns the most specific value for a given key. The returned value can be data of any type (strings, arrays, or hashes).

hiera_array - Returns all matches throughout the hierarchy — not just the first match — as a flattened array of unique values. If any of the matched values are arrays, they’re flattened and included in the results.

hiera_hash - Returns a merged hash of matches from throughout the hierarchy. In cases where two or more hashes share keys, the hierarchy order determines which key/value pair will be used in the returned hash, with the pair in the highest priority data source winning.

An example of hiera_array might be that I want to allow the corporate office IPs access to machines as well as local monitoring servers, but not monitoring servers from other sites. Assuming a hierarchy of

hiera.yaml

:hierarchy:
    - %{location}
    - common

common.yaml

ssh_allowed:
  - '10.1.x.251' # corp firewall 1
  - '10.1.x.252' # corp firewall 2

colo1.yaml

ssh_allowed:
  - '10.10.x.11' # nagios01.colo1
  - '10.10.x.12' # nagios02.colo2

A lookup using hiera_array on the ssh_allowed key will return all matching values as an array.

ssh_allowed => [ '10.1.x.251', '10.1.x.252', '10.10.x.11', '10.10.x.12' ]

An example using hiera_hash might be that that I want to manage a set of production and stage web servers. They are going to have some of the same vhost, but some may be slightly different. Assuming a hierarchy of

hiera.yaml

:hierarchy:
    - %{environment}/%{role}
    - %{role}
    - common

frontend.yaml

---
apache::vhosts:
  example.com:
    priority: '00'
apache::vhosts:
  api.example.com:
    priority: '99'

stage/frontend.yaml

---
apache::vhosts:
  api.example.com:
    priority: '99'
    template: 'debug.example.com'

In this example production servers will get the two vhost as is. However as stage server will get the standard example.com vhost, but will notice that the api.example.com keys match and merge the data based on stage/frontend being higher in the hierarchy ultimately swapping out the default template for the 'debug' one.

apache::vhosts:
  example.com:
    priority: '00'
apache::vhosts:
  api.example.com:
    priority: '99'
    template: 'debug.example.com'
edit flag offensive delete link more

Comments

This is a really good explanation.

lvidler gravatar imagelvidler ( 2014-09-03 21:15:29 -0500 )edit

One for the best explanations on this topic I have found so far!

cm gravatar imagecm ( 2015-08-13 17:14:34 -0500 )edit
0

answered 2017-04-23 15:26:58 -0500

I like this example so much I printed this MOFO out !

This may be old but I like it, here is what Im trying to accomplish

We are installing SQL besides puppet environments the Org has their own SQL environments which will be told to us puppet admins at time of implementation.

The SQL username is based on the SQL Env given at implementation time.

Is a hiera hash a good place where I can do this ?

Im not 100% I can use a hiera hash for my objective , it still would need to determine which hiera location to use ... As of now I want to setup hieradata/application/sql <--- then im guessing Im guessing I want to set a fact that we pass in VIA the webconsole that determines SQL env then I can so something along the lines of hiera($sqlenv\sqlusername) ? I already went out of the scope of using a hiera hash .... Help me out here someone ... I think I may be on the right track

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2014-09-03 11:00:34 -0500

Seen: 14,844 times

Last updated: Sep 03 '14