Ask Your Question
0

Puppet best practice. Class parameters vs. hiera_hash()

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

Red Cricket gravatar image

Hi,

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 (
    $users,
    $groups,
) {
  create_resources('user',  $users)
  create_resources('group', $groups)
}

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

Thanks!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

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

DarylW gravatar image

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

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

Comments

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 -0500 )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 https://docs.puppet.com/hiera/3.0/puppet.html#hiera-lookup-functions

DarylW gravatar imageDarylW ( 2017-01-11 17:45:13 -0500 )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 -0500 )edit

The latest style guide mentions this. I should have looked there: https://docs.puppet.com/puppet/latest/style_guide.html#hiera

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

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

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

Stats

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

Seen: 90 times

Last updated: Jan 11