A collection of task oriented solutions in Puppet

 

Add a local user

Challenge

You want to add a local user.

Solution

class add_user {

  # add a user, don't create a home directory
  user { 'reese':
    ensure => 'present',
  }

  # add user, create homedir and set a password
  user { 'finch':
    ensure     => 'present',
    comment    => 'H Finch',
    managehome => true,
    # note the single quotes to stop $ expanding
    password   => '$6$LD5..snip...gNY1',
  }

}
# run puppet and note the two resources
Notice: /Stage[main]/Add_user/User[reese]/ensure: created
Notice: /Stage[main]/Add_user/User[finch]/ensure: created

# password check
$ sudo egrep '(finch|reese)' /etc/shadow

reese:!!:17391:0:99999:7:::
finch:$6$LD5..snip...gNY1:17391:0:99999:7:::

# and check the home directories
$ ls -ald /home/{finch,reese}

ls: cannot access '/home/reese': No such file or directory
drwx------. 3 finch finch 4096 Aug 13 12:55 /home/finch

Explanation

Adding users in puppet is a task that can vary from simple to very complicated. The basic examples presented here will create two users. The first, reese, is completely bare bones and only adds the user itself. The second, finch will ensure the users home directory is created and assign a password to the user.

You can verify this by running the puppet code and then checking what puppet actually created:

# run puppet and note the two resources
Notice: /Stage[main]/Add_user/User[reese]/ensure: created
Notice: /Stage[main]/Add_user/User[finch]/ensure: created

# password check
$ sudo egrep '(finch|reese)' /etc/shadow

reese:!!:17391:0:99999:7:::
finch:$6$LD5..snip...gNY1:17391:0:99999:7:::

# and check the home directories
$ ls -ald /home/{finch,reese}

ls: cannot access '/home/reese': No such file or directory
drwx------. 3 finch finch 4096 Aug 13 12:55 /home/finch

The initial output shows that both resources were created, and we can verify this by looking in /etc/passwd but from that point on you can see how big a difference setting just a few properties makes.

See also