Replicating += with hiera

93 views
Skip to first unread message

Kenton Brede

unread,
Mar 12, 2014, 12:11:02 PM3/12/14
to puppet...@googlegroups.com
I'm in the process of moving to Puppet 3 and hiera.

With my old setup I placed users that were on all servers in basenode.  Then did a += for any additional users in the node definition.

node basenode {
  users = ['user1', 'user2']
}

node server.example.com inherits basenode {
  users += ['user3']
  # or simple exclude the line, if there were no additional users
}

With the new setup I've got a common.yaml that contains a hash of users with access to all boxes.  Then I thought I'd place additional users for "server1" in server1.yaml.

common.yaml
users_common:
  user1:
    ensure: present
    home: /home/user1
    ......

server1.yaml
server1_users:
  user3:
    ensure: present
    home: /home/user3
    ......

Then I call like this, which just pulls the usernames from the hash and creates home directories with a file type:

class users::ldap {
  # regular users
  $users_common = hiera('users_common')
  $users_common_keys = keys($users_common)
  $users_hosts = hiera("${::hostname}_users")
  $users_hosts_keys = keys($users_hosts)
  $adusers_combined = flatten([ $users_common_keys, $users_hosts_keys ])

  # create ldap user home directories
  users::admin_homedir_define { $adusers_combined: }
}

Works great until there are no users for "${::hostname}_users."

How can I make this work when "${::hostname}_users" is empty?
Thanks,

--
Kent




Jonathan Proulx

unread,
Mar 12, 2014, 12:19:03 PM3/12/14
to puppet...@googlegroups.com
see http://docs.puppetlabs.com/hiera/1/puppet.html

"If you need to merge arrays or merge hashes from multiple hierarchy
levels, you will have to use the hiera_array or hiera_hash functions
in the body of your classes."

There's not really a good example in that page, but essentially where
'hiera' takes the most specific patch hiera_array and heira_hash
collect all the values across all matching hierarchies (though you
needn't define the key in every level)

we use this for packages to install:

$basepackages = hiera_array('basepackages')
ensure_packages($basepackages)

Then in hiera be define (or not) arrays of packages by os version, role, etc...

-Jon
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/CA%2BnSE3-%3D9zQvajiNMt9e%2BOA64fHrYwPkk4WEwhm0JBPHN598PA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Felix Frank

unread,
Mar 12, 2014, 12:19:15 PM3/12/14
to puppet...@googlegroups.com
Hi,

On 03/12/2014 05:11 PM, Kenton Brede wrote:
> $users_hosts = hiera("${::hostname}_users")
>
> Works great until there are no users for "${::hostname}_users."
>
> How can I make this work when "${::hostname}_users" is empty?

I don't fully understand you problem, but I believe you want to pass a
default value to your hiera call so that it will work in the absence of
the key. E.g.

$users_hosts = hiera("${::hostname}_users", [])

HTH,
Felix

José Luis Ledesma

unread,
Mar 12, 2014, 1:38:42 PM3/12/14
to puppet...@googlegroups.com


El 12/03/2014 17:19, "Jonathan Proulx" <j...@jonproulx.com> escribió:
>
> see http://docs.puppetlabs.com/hiera/1/puppet.html
>
> "If you need to merge arrays or merge hashes from multiple hierarchy
> levels, you will have to use the hiera_array or hiera_hash functions
> in the body of your classes."
>
> There's not really a good example in that page, but essentially where
> 'hiera' takes the most specific patch hiera_array and heira_hash
> collect all the values across all matching hierarchies (though you
> needn't define the key in every level)
>

+1 it should be something like

common.yaml
users:


  user1:
    ensure: present
    home: /home/user1
    ......

server1.yaml
users:


  user3:
    ensure: present
    home: /home/user3

And then
$users= hiera_hash('users')
users::admin_homedir_define { $users: }

Note: code not tested

Regards

> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CABZB-sgYSky1BzZ6Vf1OHuYonAhLh-gKV%2BN_RmiQreRyWbVk_w%40mail.gmail.com.

Kenton Brede

unread,
Mar 12, 2014, 2:14:50 PM3/12/14
to puppet...@googlegroups.com
Thanks for the help all.  Maybe I'm approaching this in the wrong way but the example puts all users on each box.  I'm trying to do something like:

users_common = admin1, admin2
server1 = users_common + user1
server2 = users_common
server3 = users_common + user1, user3

The goal is to not have to list the admin users in each host.yaml file, but include them.

From what I gather hiera doesn't allow:

sever1.yaml
include users_common

server1_users:

  user1:
    ensure: present
    home: /home/user1

Thanks,
Kent
 

 




For more options, visit https://groups.google.com/d/optout.



--
Kent Brede




Jonathan Proulx

unread,
Mar 12, 2014, 2:33:56 PM3/12/14
to puppet...@googlegroups.com
To sort of meta example the pseudo code :)

On Wed, Mar 12, 2014 at 12:11 PM, Kenton Brede <kbr...@gmail.com> wrote:

> With the new setup I've got a common.yaml that contains a hash of users with
> access to all boxes. Then I thought I'd place additional users for
> "server1" in server1.yaml.
>
> common.yaml
> users_common:
> user1:
> ensure: present
> home: /home/user1
> ......
>
> server1.yaml
> server1_users:
> user3:
> ensure: present
> home: /home/user3
> ......

in stead of using users_common and server1_users just call both 'users':

common.yaml
users:
user1:
ensure: present
home: /home/user1
......

server1.yaml
users:
user3:
ensure: present
home: /home/user3
......

> Then I call like this, which just pulls the usernames from the hash and
> creates home directories with a file type:
>
> class users::ldap {
> # regular users
> $users_common = hiera('users_common')
> $users_common_keys = keys($users_common)
> $users_hosts = hiera("${::hostname}_users")
> $users_hosts_keys = keys($users_hosts)
> $adusers_combined = flatten([ $users_common_keys, $users_hosts_keys ])
>
> # create ldap user home directories
> users::admin_homedir_define { $adusers_combined: }
> }

You can then collect all the users with hiera_hash('users') rather
than fussing with seperate $users_common and $users_hosts

class users::ldap {
# regular users
$users = hiera('users')
$users_keys = keys($users)

# create ldap user home directories
users::admin_homedir_define { $users_keys: }
}

Note I just copy pasted your code example & substituted syntax didn't
test, seems a bit odd that you're operating on just the keys not the
collected hash but left that as is since I don't knwo what it's
ultimately being fed to.


-Jon

José Luis Ledesma

unread,
Mar 12, 2014, 2:42:53 PM3/12/14
to puppet...@googlegroups.com

Try my code, its just what you want.

Kenton Brede

unread,
Mar 12, 2014, 3:06:32 PM3/12/14
to puppet...@googlegroups.com
Thanks all!  Hiera is smarter than I knew.  This is great. :)
Kent



For more options, visit https://groups.google.com/d/optout.



--
Kent Brede




Reply all
Reply to author
Forward
0 new messages