Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is it possible to add an existing user to a group with puppet 2.7.18?

We have two modules, each one defines one class :

  • module "user" creates all users, including user foo and user bar.
  • module "subversion" deals with various conf files and creates group svn.

We would like to add user foo to group svn inside module "subversion".

I've tried the membership parameter as described in the existing feature request:

group {
"svn":
    ensure  => present,
    gid     => xxxxx;
}
user {
"foo":
    group      => ["svn"],
    membership => minimum;
}

But I get the following error :

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Duplicate declaration: User[foo] is already declared in file /pathto/modules/subversion/manifests/init.pp at line xx; cannot redeclare at /pathto/modules/users/manifests/init.pp:xxx on node myserver.example.com

Is this feature is already implemented? If not, is there a good workaround?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

If you declare users as virtual resources , you can then use 'realize' or the collection syntax ( User <| ... |>). Here's an example:

@user { 'foo':
    ...,
    groups      => ["somegroup"]
    membership => minimum,
}

Then realize that virtual user with then collection syntax:

User <| title == foo |>

And elsewhere you can add to the parameters for that virtual resource using plusignment:

User <| title == foo |> { groups +> "svn" }
share|improve this answer
    
So I guess there's no alternative except virtual ressources at this time :( Thanks a lot for the answer. –  coincoin Sep 5 '12 at 13:51
1  
There is a typo or attribute changed between Puppet versions: It is not group but groups (plural). Using Puppet 3.2.2 –  schkovich Jun 21 '13 at 11:04

Using Puppet virtual resources is the right way to do it - but if you can't change the user definitions and need a workaround fix meanwhile, the following is horrible and hacky, but will work:

exec {"foo somegroup membership":
  unless => "grep -q 'somegroup\\S*foo' /etc/group",
  command => "usermod -aG somegroup foo",
  require => User['foo'],
}

Essentially we just check if somegroup contains user foo yet... if not, use the normal usermod commands to add it in addition to the existing groups that foo belongs to.

share|improve this answer
1  
Puppet might riquire fully qualified paths to the executables. Edited to reflect this. –  h7r Apr 18 at 13:12
    
Horrible & hacky, but really helpful. I was hoping not to have to learn a new language construct to do something a conceptually simple as adding a user to a group. –  BillMan Jul 1 at 14:45
    
Perhaps a more proper (while still being hacky of course) way would be to use getent group somegroup|cut -d: -f4|grep -q foo to not assume we're using /etc/group. –  falstro Dec 5 at 11:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.