Search code examples
resourceschef-infrarecipe

How to create user having member in multiple groups in chef user resource


While creating user we can specify group in gid to which he belongs and if this user belongs to multiple groups how can we specify in chef

user 'random' do
  supports :manage_home => true
  comment 'Random User'
  uid 1234
  gid 'users'
end

If user resource not providing this option how can we achieve in best way.


Solution

  • That's not a user which has multiple groups in fact, it'a group having multiple users (that's managed in /etc/groups not in /etc/passwd).

    the way to achieve that is:

    user 'random' do
      supports :manage_home => true
      comment 'Random User'
      uid 1234
      gid 'users'
    end
    
    %w{group1 group2 group2}.each do |g|
      group g do
        action :modify
        members "random"
        append true
      end
    end
    

    See the group resource documentation for more details and available attributes.

    Change action to :create if the group does not already exists.