What is the best way for creating users in puppet/hiera ?

2,910 views
Skip to first unread message

AnOnJoe

unread,
Nov 26, 2012, 6:00:17 AM11/26/12
to puppet...@googlegroups.com
Hello,
I have recently discover hiera, and I would like to use it for creating users on my node.

I first think of someting like that :


common.yaml
lusers : - jodoe
         - jadoe
classes : - users


serv01.foo.com.yaml
lusers : - Alice
         - Bob


modules/users/manifest/init.pp
define users ($user = hiera("$lusers")) {
        user { "$user":           
                ensure          => present,
                shell           => '/bin/bash',
                home            => "/home/$user",
                managehome      => true,
        }
}

But I don't know how I can call my def type like that.

What about you ? How do you create your users in puppet / hiera ?

Thx


 

Ellison Marks

unread,
Nov 26, 2012, 12:51:01 PM11/26/12
to puppet...@googlegroups.com
You might look into the create_resources function. The example given is creating users even.

http://docs.puppetlabs.com/references/latest/function.html#createresources

jcbollinger

unread,
Nov 26, 2012, 2:20:12 PM11/26/12
to puppet...@googlegroups.com


A module's init.pp, if non-empty, should contain only the definition of a class (not a definition) sharing the name of the module.  That's what you want in this case anyway:

modules/users/manifests/init.pp:

class users {
  $users = flatten(hiera_array('lusers'))
  user::user { $users: }
}


modules/users/manifests/user.pp

define user::user () {
  user { "$name":

    ensure     => present,
    shell      => '/bin/bash',
    home       => "/home/$name",
    managehome => true
  }
}


Notes:
  1. To collect values for the same key from multiple levels of your data hierarchy, you need to use either hiera_array() or hiera_hash().  The plain hiera() function will give you only the value from the highest-priority level.
  2. The flatten() function comes from the "stdlib" add-on module.  You would need it in the example because, with the data as given, hiera_array() will return an array of arrays, whereas you want a single array whose elements are the usernames.
  3. The only reason you need a defined type is that you want to explicitly declare the home directory name based on the username.  If none of the properties were derived from the username then you could just use native User resources directly.
  4. All quoting (and non-quoting) in the example is exactly as you should have it.  In several cases, adding quotes or changing the quote type will change the meaning.
  5. You would use the example by via "include 'users'"

John

AnOnJoe

unread,
Nov 28, 2012, 9:33:49 AM11/28/12
to puppet...@googlegroups.com
thx Elison, my users are now created via the create_resources function, and it works like a charm.

and thx jcbollinge,
you show me the right way in using puppet/hiera.

I have now a lot of work to normalize my modules ;-)


Reply all
Reply to author
Forward
0 new messages