On 02/03/16 11:26, Thomas Bendler wrote:
> Hi @all,
>
> I write a module that create local users on my boxes. Now I try to make
> that module fully dynamic so that the user informations are passed to
> the module as parameter like this:
>
> class { 'local_users':
> user => [
> { 'john' => { name => 'John Doe', home => '/export/home/john' } },
> { 'jane' => { name => 'Jane Doe', home => '/export/home/jane' } }
> ]
> }
>
You could use a hash there directly instead of an array of hashes. The
'id' (e.g. 'john', 'jane' has to be unique anyway.
> So far, so good. But now I would like to iterate through the user array
> and create the user resource and I have no clue how this should be done
> correctly. My approach is to call a define:
>
> local_users::config::account { $local_users::user }
>
> Which look like this:
>
> define local_users::config::account (
> $id = $title,
> $name = undef,
> $home = undef
> ) {
> user { $id:
> ensure => present,
> comment => $name,
> home => $home,
> managehome => true,
> password => '!!';
> }
> }
>
> I guess the direction should be understandable, I would like to specify
> the users and their attributes as a parameter. What I don't get so far
> is, do I need one resource definition for each possible combination or
> is there a way that only the parameter that contain values are used
> within the resource type? Is the path in general the correct one that I
> use or is there a better approach to get this done?
>
If you are on 3.x with future parser, or on 4.x you can iterate.
I made some simplifications here, everything is one hash, and
I renamed 'name' to 'comment' so I could use the hash directly
to set all attributes without having to first transform 'name'
into 'comment'.
class { 'local_users':
user => {
'john' => { comment => 'John Doe', home => '/export/home/john' },
'jane' => { comment => 'Jane Doe', home => '/export/home/jane' }
}
}
class local_users($users) {
$users.each |$id, $attributes | {
user { $id:
managehome => true,
password => '!!',
* => $attributes # attributes from hash
}
}
}
With typed parameter
----
To make it more robust you can also type the $users argument
class local_users(
Hash[String, Struct[{
name => String,
home => String}]
] $users)
{
$users.each |$id, $attributes | {
user { $id:
managehome => true,
password => '!!',
* => $attributes
}
}
}
Hope that helps.
Regards
- henrik
--
Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/