Ask Your Question

Puppet best practice. Class parameters vs. hiera_hash()

asked 2017-01-11 13:58:37 -0600

Red Cricket gravatar image


I was wondering what is considered best practice when it comes to delivering hiera data to my puppet classes.

Example 1)

class os_accounts (
) {
  create_resources('user', hiera_hash("os_accounts::user",{}))
  create_resources('group', hiera_hash("os_accounts::group",{}))

Example 2)

class os_accounts (
) {
  create_resources('user',  $users)
  create_resources('group', $groups)

I tend to prefer Example 2 just because there is less code.


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-01-11 16:29:07 -0600

DarylW gravatar image

updated 2017-01-11 16:29:58 -0600

There is a difference in behavior between the two examples.

hiera_hash will do a hash merge across your hierarchy in the first case

The implicit hiera call from Automatic Parameter Lookup in your second call will only take the 'top' result (priority lookup) from the hierarchy. This is referenced in the Hiera Limitations documentation.

You second example is the equivalent of

class os_accounts (
    $users = hiera("os_accounts::user")),   #note no default value
    $groups = hiera("os_accounts::group")), #note no default value
) {
  create_resources('user',  $users)
  create_resources('group', $groups)
edit flag offensive delete link more


Thanks for the reply. Does calling `hiera("os_accounts::user"))` imply a data type (string vs array vs hash) or does the hiera() function figure that out?

Red Cricket gravatar imageRed Cricket ( 2017-01-11 16:40:55 -0600 )edit

hiera_hash doesn't create a hash type explicitly, it pulls the hash structure out of hiera. the 'hash' part is the merge strategy... see

DarylW gravatar imageDarylW ( 2017-01-11 17:45:13 -0600 )edit

as far as type information, I'm still on puppet3 and I haven't had a chance to explore that yet, sorry!

DarylW gravatar imageDarylW ( 2017-01-11 17:45:32 -0600 )edit

The latest style guide mentions this. I should have looked there:

Red Cricket gravatar imageRed Cricket ( 2017-01-12 13:19:52 -0600 )edit

answered 2017-01-11 15:46:19 -0600

lavaman gravatar image

Example 2 would be considered more proper style because it would allow the code to be run with or without hiera.

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


Asked: 2017-01-11 13:58:37 -0600

Seen: 1,062 times

Last updated: Jan 11 '17