Iterating over array to create users and files

1,129 views
Skip to first unread message

Chris F

unread,
Dec 3, 2009, 4:29:42 PM12/3/09
to Puppet Users
I want to do the following

declare variable:

$usernames = [ "chris", "bob", "tim" ]

How would I iterate over it to create something like:

user { "$username":
home => "/home/$username",
}

Also is it possible to create a more complex structure with $UID and
$Shell

I'm thinking of something like this:

$users = [ "chris": {
UID => 800,
Shell => "/usr/bin/bash",
},
"bob": {
UID => 801,
Shell => "/usr/bin/zsh",
}, ]

Then I can reference via
foreach $user $users {
user { "$user":
home => "/home/$user",
UID => $user=>UID,
Shell => $user=>Shell,
}
}

Again this is only pseudocode, but I'm wondering if puppet has this
kind of functionality or if there's another way to do this that
haven't found.

Also I want to iterate over an array of names to create individual
files:

$usernames = [ "bob", "tim" ]

foreach $username $usernames {
file { "/etc/httpd/conf.d/${username}-www.conf
ensure => present,
owner => "root",
group => "root",
mode => "644",
notify => Service["httpd"],
content => $environment ? {
production => template("services/www/prod-www.conf.erb"),
staging => template("services/www/stg-www.conf.erb"),
development => template("services/www/dev-www.conf.erb"),
}
}

With $username in various point in the template.

Are either of these possible? Can anyone suggest a better way?

I know for the file I can iterate with the template and create one
large conf file, but I'd rather keep the conf files separate based on
user.

Thanks

Chris

Silviu Paragina

unread,
Dec 3, 2009, 9:22:32 PM12/3/09
to puppet...@googlegroups.com
On 03.12.2009 23:29, Chris F wrote:
> I want to do the following
>
> declare variable:
>
> $usernames = [ "chris", "bob", "tim" ]
>
> How would I iterate over it to create something like:
>
> user { "$username":
> home => "/home/$username",
> }
>
> Also is it possible to create a more complex structure with $UID and
> $Shell
>
> I'm thinking of something like this:
>
> $users = [ "chris": {
> UID => 800,
> Shell => "/usr/bin/bash",
> },
> "bob": {
> UID => 801,
> Shell => "/usr/bin/zsh",
> }, ]
>
This is kind of close to the user defintion. You may define multiple
resources of the same type somewhat like that
(http://reductivelabs.com/trac/puppet/wiki/LanguageTutorial#resource-collections)




> Then I can reference via
> foreach $user $users {
> user { "$user":
> home => "/home/$user",
> UID => $user=>UID,
> Shell => $user=>Shell,
> }
> }
>
As far as I know puppet does not have iterative statements (except for
templates which are a diffrent topic).
You could create a define, which receives your parameters and builds
other resources, and use the above semantics (from the link)

ie
define user_home($user,$UID, $Shell)
{
user { $user:
home => "/home/${user}",
uid => $UID,
shell => $Shell,
}

and use it as


user_home
{
"user1":
blah;
"user2":
blah;
}
> Again this is only pseudocode, but I'm wondering if puppet has this
> kind of functionality or if there's another way to do this that
> haven't found.
>
> Also I want to iterate over an array of names to create individual
> files:
>
> $usernames = [ "bob", "tim" ]
>
> foreach $username $usernames {
> file { "/etc/httpd/conf.d/${username}-www.conf
> ensure => present,
> owner => "root",
> group => "root",
> mode => "644",
> notify => Service["httpd"],
> content => $environment ? {
> production => template("services/www/prod-www.conf.erb"),
> staging => template("services/www/stg-www.conf.erb"),
> development => template("services/www/dev-www.conf.erb"),
> }
> }
>
> With $username in various point in the template.
>
> Are either of these possible? Can anyone suggest a better way?
>
> I know for the file I can iterate with the template and create one
> large conf file, but I'd rather keep the conf files separate based on
> user.
>
> Thanks
>
> Chris
>
> --
>
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
>
>
>
Cheers,
Silviu
Reply all
Reply to author
Forward
0 new messages