Skip to content

Commit cb2deaa

Browse files
committed
Initial commit - moved from ansible_configs
0 parents  commit cb2deaa

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Users role
2+
3+
Role to manage users on a system.
4+
5+
## Role configuration
6+
7+
* users_create_per_user_group (default: true) - when creating users, also
8+
create a group with the same username and make that the user's primary
9+
group.
10+
* users_group (default: users) - if users_create_per_user_group is _not_ set,
11+
then this is the primary group for all created users.
12+
* users_default_shell (default: /bin/bash) - the default shell if none is
13+
specified for the user.
14+
* users_create_homedirs (default: true) - create home directories for new
15+
users. Set this to false is you manage home directories separately.
16+
17+
## Creating users
18+
19+
Add a users variable containing the list of users to add. A good place to put
20+
this is in `group_vars/all` or `group_vars/groupname` if you only want the
21+
users to be on certain machines.
22+
23+
The following attributes are required for each user:
24+
25+
* username - The user's username.
26+
* name - The full name of the user (gecos field)
27+
* uid - The numeric user id for the user. This is required for uid consistency
28+
across systems.
29+
* groups - a list of supplementary groups for the user.
30+
* ssh-key - This should be a list of ssh keys for the user. Each ssh key
31+
should be included directly and should have no newlines.
32+
33+
In addition, the following items are optional for each user:
34+
35+
* shell - The user's shell. This defaults to /bin/bash. The default is
36+
configurable using the users_default_shell variable if you want to give all
37+
users the same shell, but it is different than /bin/bash.
38+
39+
Example:
40+
41+
---
42+
users:
43+
- username: foo
44+
name: Foo Barrington
45+
groups: ['wheel','systemd-journal']
46+
uid: 1001
47+
ssh_key:
48+
- "ssh-rsa AAAAA.... foo@machine"
49+
- "ssh-rsa AAAAB.... foo2@machine"
50+
users_deleted:
51+
- username: bar
52+
name: Bar User
53+
uid: 1002
54+
55+
## Deleting users
56+
57+
The `users_deleted` variable contains a list of users who should no longer be
58+
in the system, and these will be removed on the next ansible run. The format
59+
is the same as for users to add, but the only required field is `username`.
60+
However, it is recommended that you also keep the `uid` field for reference so
61+
that numeric user ids are not accidentally reused.

defaults/main.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
# Create a group for every user and make that their primary group
3+
users_create_per_user_group: true
4+
# If we're not creating a per-user group, then this is the group all users
5+
# belong to
6+
users_group: users
7+
# The default shell for a user if none is specified
8+
users_default_shell: /bin/bash
9+
# Create home dirs for new users? Set this to false if you manage home
10+
# directories in some other way.
11+
users_create_homedirs: true
12+
13+
# Lists of users to create and delete
14+
users: []
15+
users_deleted: []

meta/main.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
galaxy_info:
3+
author: Mark Harrison
4+
description: User creation role
5+
license: MIT
6+
min_ansible_version: 1.2
7+
platforms:
8+
- name: EL
9+
versions:
10+
- all
11+
- name: GenericUNIX
12+
versions:
13+
- all
14+
- any
15+
- name: Fedora
16+
versions:
17+
- all
18+
- name: opensuse
19+
versions:
20+
- all
21+
- name: Ubuntu
22+
versions:
23+
- all
24+
- name: SLES
25+
versions:
26+
- all
27+
- name: GenericLinux
28+
versions:
29+
- all
30+
- any
31+
- name: Debian
32+
versions:
33+
- all
34+
categories:
35+
- system
36+
dependencies: []

tasks/main.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
- name: Per-user group creation
3+
group: name="{{item.username}}" gid="{{item.uid}}"
4+
with_items: users
5+
when: users_create_per_user_group
6+
7+
- name: User creation
8+
user: name="{{item.username}}"
9+
group="{{item.username if users_create_per_user_group
10+
else users_group}}"
11+
groups="{{item.groups | join(',')}}"
12+
shell={{item.shell if item.shell is defined else users_default_shell}}
13+
comment="{{item.name}}"
14+
uid="{{item.uid}}"
15+
createhome="{{'yes' if users_create_homedirs else 'no'}}"
16+
with_items: users
17+
18+
- name: SSH keys
19+
authorized_key: user="{{item.0.username}}" key="{{item.1}}"
20+
with_subelements:
21+
- users
22+
- ssh_key
23+
24+
- name: Deleted user removal
25+
user: name="{{item.username}}" state=absent
26+
with_items: users_deleted
27+
28+
- name: Deleted per-user group removal
29+
group: name="{{item.username}}" state=absent
30+
with_items: users_deleted
31+
when: users_create_per_user_group

0 commit comments

Comments
 (0)