Collection Questions

18 views
Skip to first unread message

Stephan

unread,
Oct 11, 2011, 7:38:35 AM10/11/11
to Puppet Users
Hi,

I have a problem with getting puppet to do what I want. I've written a
module which I configure via a define, and one of the define's
settings is an array of all user names that are supposed to act as
admins of this particular service this module is rolling out, like
this:

application::config { "app_1":
app-admin => ["user1","user2],
}

application:config { "app_2":
app-admin => ["user1","user2,"user3"],
}

Now this application has a config file, which I've made member of a
certain group, and I want to add above users to this group:

define application::config ( $app-admin ) {
group { "$title":
ensure => present,
system => true,
}
file { "/etc/$title/app.conf":
group => "$title",
[...]

User<| title == $app-admin |> {
groups +> "$title",
}

So the last User<| |> collection is not adding this group to all app-
admin users, but only to "user1". Seems like it is unable to handle
arrays. Is there a good reason for that? How can I get this done?

Thanks
Stephan

jdk

unread,
Oct 11, 2011, 4:47:38 PM10/11/11
to puppet...@googlegroups.com
In playing around with something like this, I was able to tag the users and then use the tags to realize the correct set of users:

@user {
     "user1": tag => [ 'app_1' , 'app_2' ];
     "user2": tag => [ 'app_1' , 'app_2' ];
     "user3": tag => [ 'app_2' ];
}

Then, you can use this to realize them:

     User <| tag == $title |> {
          groups +> "$title",
     }

If you find another way to do it, please let me know.  This would work ok for a few apps, but doesn't scale well.

Best,
Jeremy

Nick Fagerlund

unread,
Oct 11, 2011, 5:48:51 PM10/11/11
to Puppet Users


On Oct 11, 4:38 am, Stephan <stephan.eckwei...@admin.ox.ac.uk> wrote:
>      User<| title == $app-admin |> {
>           groups +> "$title",
>      }

Stephan, I think you might want an "in" expression:

http://docs.puppetlabs.com/guides/language_guide.html#in-expressions

I've never tried these with the collection operator, but it would look
something like:

User<| title in $app-admin |> {
groups +> "$title",
}

Does that work?

Jeremy Kindy

unread,
Oct 11, 2011, 7:34:51 PM10/11/11
to puppet...@googlegroups.com
On Tue, Oct 11, 2011 at 5:48 PM, Nick Fagerlund <nick.fa...@puppetlabs.com> wrote:
I've never tried these with the collection operator, but it would look
something like:

User<| title in $app-admin |> {
 groups +> "$title",
}

Does that work?

I wish it did.  However, what you proposed results in an error.  I think the collection is expecting a attribute name on the left.

Error 400 on SERVER: Could not parse for environment production: Syntax error at 'in'; expected '|>'

Jeremy

Stephan

unread,
Oct 12, 2011, 6:02:01 AM10/12/11
to Puppet Users


On Oct 11, 9:47 pm, jdk <jeremy.ki...@gmail.com> wrote:
> In playing around with something like this, I was able to tag the users and
> then use the tags to realize the correct set of users:

Thanks for the reply, I had a look into that, the problem is that I
originally create this user via a define somewhere else. If I would do
that I would have to configure my app at 2 different places which I
want to avoid. I've ended up doing it like this:

User<| title == $app-admin[0] |> {
groups +> "$title",
}

User<| title == $app-admin[1] |> {
groups +> "$title",
}

User<| title == $app-admin[2] |> {
groups +> "$title",
}

Then I mention in a comment in the define that builds this app, that
the maximum number of users is 3. If you want more, you have to copy
and paste above lines a couple times in the config of the application.
It's ugly, but I don't expect many users in there anyways. Maybe
Puppetlabs one day support arrays in here to get rid of this ugly
code.

Thanks again

Stephan

unread,
Oct 12, 2011, 6:04:26 AM10/12/11
to Puppet Users

> I wish it did.  However, what you proposed results in an error.  I think the
> collection is expecting a attribute name on the left.
>
> Error 400 on SERVER: Could not parse for environment production: Syntax
> error at 'in'; expected '|>'

Seems to be as designed. I found this statement:

"The only comparisons available are equality and non-equality (using
the == and != operators, respectively), and you can join these
comparisons using or and and."

in http://docs.puppetlabs.com/guides/virtual_resources.html#how-to-realize-resources

jdk

unread,
Oct 12, 2011, 11:58:01 AM10/12/11
to puppet...@googlegroups.com

Thanks for the reply, I had a look into that, the problem is that I
originally create this user via a define somewhere else. If I would do
that I would have to configure my app at 2 different places which I
want to avoid. I've ended up doing it like this:

    User<| title == $app-admin[0] |> {
        groups +> "$title",
    }

I think I've got it!  You need a helper define:

define application::realize_users ($groups) {
     User <| title == $name |> {
          groups +> $groups,
     }
}

Then, to realize your array of users:


define application::config ( $app-admin ) {
     <snip>

     application::realize_users { $app-admin: groups => $title }
}


I think that will work for you.  I was able to implement it for our particular problem and it worked, but we also are trying to change shells and home directories, so it's a little more complicated than you need.

Good luck,
Jeremy
Reply all
Reply to author
Forward
0 new messages