Ask Your Question
1

Split hiera yaml files based on module or profile

asked 2015-09-16 17:18:47 -0500

cybersamurai gravatar image

updated 2015-09-17 10:12:27 -0500

The conventional wisdom surrounding Hiera yaml files seems to dictate a split between FQDN or OS. From Hiera Tutorial:

---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppet/hieradata
:hierarchy:
  - "node/%{::fqdn}"
  - "virtual/%{::virtual}"
  - "osfamily/%{osfamily}"
  - common

Looking specifically at the split based on FQDN, I do not find this to be ideal. I don't like the idea of defining all node-specific data from ALL modules on that node in a single, monolithic file. I would much rather have a FQDN-named directory filled with separate yaml files for a given module. As the next level down in the hierarchy, splitting by profile or environment would also seem to also be ideal (especially as I intend to implement the roles/profile pattern).

The single monolithic file approach means you have to edit a master file anytime you want to add or delete a module instead of simply adding or deleting a file. Also, tasks like making sense of version control history become less user friendly when all values are combined in a master file.

QUESTION: Can Hiera yaml files be split to a more granular manner that is less dependent on node attributes (fqdn/OS/virtual/etc) and more tailored to modules, profiles, environments, etc?

I have found some Similar Questions regarding this topic, but none seem to get at what I am looking for.

edit retag flag offensive close merge delete

Comments

1

Thanks! These are both helpful!

cybersamurai gravatar imagecybersamurai ( 2015-09-17 09:40:43 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-09-16 22:47:07 -0500

Ancillas gravatar image

Yes.

Here's an example of using the $module_name in Hiera.

I created three modules: elephant, monkey, and boring. Each module simply prints $result_msg, which is sourced by hiera('result_msg'). Here's an example.

boring module - init.pp

class boring (
  $result_msg = hiera('result_msg'),
) {

  notify { 'print boring result_msg':
    message => "${result_msg}",
  }
}

The modules are called by site.pp.

site.pp

include elephant
include monkey
include boring

Class['elephant'] -> Class['monkey'] -> Class['boring']

Here's my Hiera configuration, and data files. The key is using %{module_name} to source the name of the module calling the hiera function.

hiera.yaml

---
:backends:
  - yaml
:yaml:
  :datadir: /etc/puppet/hieradata
:hierarchy:
  - "nodes/%{::fqdn}/%{module_name}"
  - "virtual/%{::virtual}"
  - "osfamily/%{osfamily}"
  - common

hieradata structure

hieradata/
    common.yaml
    nodes/
        myhostname.domain.com/
            elephant.yaml  monkey.yaml

common.yaml

---
result_msg: "This is a boring message with no animals."

elephant.yaml

---
result_msg: "This is an elephant message."

monkey.yaml

---
result_msg: "This is a monkey message."

And the output of running site.pp.

Notice: Compiled catalog for myhostname.domain.com in environment production in 0.03 seconds
Notice: This is an elephant message.
Notice: /Stage[main]/Elephant/Notify[print elephant result_msg]/message: defined 'message' as 'This is an elephant message.'
Notice: This is a monkey message.
Notice: /Stage[main]/Monkey/Notify[print monkey result_msg]/message: defined 'message' as 'This is a monkey message.'
Notice: This is a boring message with no animals.
Notice: /Stage[main]/Boring/Notify[print boring result_msg]/message: defined 'message' as 'This is a boring message with no animals.'
Notice: Finished catalog run in 0.04 seconds
edit flag offensive delete link more

Comments

Perfect! Thanks for putting together an example!

cybersamurai gravatar imagecybersamurai ( 2015-09-17 09:41:05 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-09-16 17:18:47 -0500

Seen: 448 times

Last updated: Sep 17 '15