Search code examples
linuxpuppet

How to create user having a primary group with the same name using puppet script?


I was reading this article and it suggests creating user by using this code:

user { 'myuser':
  ensure     => present,
  uid        => '1000',
  gid        => '1000',
  shell      => '/bin/bash',
  home       => '/home/myuser'
}

However, when I run it, throws following error:

Error: Could not create user myuser: Execution of '/usr/sbin/useradd -g 1000 -G wheel -d /home/myuser -s /bin/bash -u 1000 -M myuser' returned 6: useradd: group '1000' does not exist

I made it to work with the following:

group {'myuser':
  ensure     => present
}

user { 'myuser':
  ensure     => present,
  uid        => '1000',
  gid        => 'myuser',
  shell      => '/bin/bash',
  home       => '/home/myuser'
}

But I am not sure whether is the right way. Is there a way to achieve what I want by just using the user puppet resource?


Solution

  • I think the answer depends on OS and/or provider you used to create an user. In your configuration it is useradd. According to documentation, when used with -g option:

    The group name must exist. A group number must refer to an already existing group.

    Possible solution is to remove gid:

    user { 'myuser':
        ensure     => present,
        uid        => '1020',
        shell      => '/bin/bash',
        home       => '/home/myuser',
        managehome => true,
    }
    

    Then puppet will automatically create poper group:

    root@vagrant-ubuntu-trusty-64:/home# su myuser

    myuser@vagrant-ubuntu-trusty-64:/home$ id

    uid=1020(myuser) gid=1020(myuser) groups=1020(myuser)

    Good idea is to add an option managehome => true,. It will:

    create the home directory when ensure => present, and delete the home directory when ensure => absent