Skip to content

Commit a74d7a0

Browse files
authored
Merge pull request #199 from plasmabio/create-delete-multiple-users
Create and delete multiple student users
2 parents 4392f0a + 433921f commit a74d7a0

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

ansible/students-create.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
3+
- hosts: all
4+
become: yes
5+
6+
vars:
7+
default_user_group: plasma-users
8+
9+
tasks:
10+
# Read a CSV file with users
11+
- name: Read users from CSV file and return a list
12+
read_csv:
13+
path: "{{ studentdef }}"
14+
register: users
15+
delegate_to: localhost
16+
become: no
17+
18+
- debug:
19+
msg: "Read user {{ item.username }} with password {{ item.password }} in group {{ item.group }}"
20+
loop: "{{ users.list }}"
21+
delegate_to: localhost
22+
become: no
23+
24+
- name: Add users
25+
user:
26+
name: "{{ item.username }}"
27+
groups: "{{ item.group if item.group is defined else default_user_group }}"
28+
password: "{{ item.password | password_hash('sha512') }}"
29+
shell: /bin/bash
30+
home: "{{ home_path if home_path is defined else '/home' }}/{{ item.username }}"
31+
loop: "{{ users.list }}"
32+
33+
- name: Set user quota
34+
shell: |
35+
setquota -u {{ item.username }} \
36+
{{ item.quota.soft if item.quota.soft is defined else quota.soft }} \
37+
{{ item.quota.hard if item.quota.hard is defined else quota.hard }} \
38+
0 0 {{ quota_device_path }}
39+
when: quota is defined or item.quota is defined
40+
loop: "{{ users.list }}"
41+
42+

ansible/students-remove.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
3+
- hosts: all
4+
become: yes
5+
6+
vars:
7+
default_user_group: plasma-users
8+
9+
tasks:
10+
# Read a CSV file with users
11+
- name: Read users from CSV file and return a list
12+
read_csv:
13+
path: "{{ studentdef }}"
14+
register: users
15+
delegate_to: localhost
16+
become: no
17+
18+
- debug:
19+
msg: "Read user {{ item.username }} in group {{ item.group }}"
20+
loop: "{{ users.list }}"
21+
delegate_to: localhost
22+
become: no
23+
24+
- name: Remove users
25+
user:
26+
name: "{{ item.username }}"
27+
state: absent
28+
remove: yes
29+
loop: "{{ users.list }}"

docs/configuration/batch-users.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
(batch-users)=
2+
3+
# Creating users in batches
4+
5+
The {ref}`install-users` section details how to create the initial list of users.
6+
7+
However in some cases it is useful to have a more automated way to create (and delete) users
8+
in batches. For example when preparing and cleaning up a group of students at the beginning and end of a semester.
9+
10+
This section details how to create users defined in a CSV file using Ansible Playbooks.
11+
12+
## Creating the CSV File
13+
14+
Create the `student.csv` file which should contain the `username`, `password` and `group` columns.
15+
16+
Here is an example of such file:
17+
18+
```text
19+
username,password,group
20+
stu-megm1-01,bn3r7RjtOyg15X,megm1
21+
stu-megm1-02,2shgP3xK7aTuMN,megm1
22+
stu-megm1-03,Kh5jn4GuEIFIzY,megm1
23+
stu-megm1-04,g9gBHQjJ4VqQG1,megm1
24+
stu-megm1-05,73O88oFb6B1TB0,megm1
25+
stu-megm1-06,laZubrmgKBNg1x,megm1
26+
stu-megm1-07,gAONEMsgdz28si,megm1
27+
stu-megm1-08,tjlGadyELWj59M,megm1
28+
stu-megm1-09,soIb4txJDPjo1d,megm1
29+
stu-megm1-10,QjhalcW9Uq5wxo,megm1
30+
```
31+
32+
````{warning}
33+
Since the fields in the CSV file are delimited by commas, passwords should not contain any `,` character.
34+
````
35+
36+
## Running the playbooks
37+
38+
To create the users, go to the `ansible/` folder and run the `student-create.yml` playbook with:
39+
40+
```sh
41+
ansible-playbook student-create.yml -u ubuntu -e "studentdef=students.csv"
42+
```
43+
44+
It is also possible to delete the users from the same CSV definition, using the `student-remove.yml` playbook:
45+
46+
```sh
47+
ansible-playbook student-remove.yml -u ubuntu -e "studentdef=students.csv"
48+
```
49+
50+
````{note}
51+
It is possible to pass additional parameters when creating users in batches.
52+
53+
For example if you have a file defining disk quotas for a group of students:
54+
55+
```yaml
56+
# default quotas for students
57+
quota:
58+
soft: 200G
59+
hard: 250G
60+
```
61+
62+
You can run the playbook and reference that extra file:
63+
64+
```bash
65+
ansible-playbook student-create.yml -u ubuntu -e "studentdef=students.csv" -e @students-config.yml
66+
```
67+
````

docs/configuration/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
monitoring
77
persistence
8+
batch-users
89
resources
910
cull
1011
namedservers

docs/install/users.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,7 @@ foo -- 16K 5120M 10240M 4 0 0
231231
bar -- 16K 10240M 12288M 4 0 0
232232
#62583 -- 4K 0K 0K 2 0 0
233233
```
234+
235+
### Creating user in batches
236+
237+
The {ref}`batch-users` section details another way to create users in batches.

0 commit comments

Comments
 (0)