Skip to content

Commit 7496bab

Browse files
committed
add possibility to restrict user creation to specific users on a host
1 parent 9b63293 commit 7496bab

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ selectivly run only parts:
122122
* sudo without password
123123
* X.config.users."username".sudo_selfservice: false
124124
* sudo without password to use passwd, smbpasswd on own user
125+
* lihas_users_filter: []
126+
* only create these users
127+
* TODO: lihas_users_filter_exclusive: false
128+
* remove existing users that would be created without a `lihas_users_filter`
125129

126130
## Variables example
127131
```

tasks/10-users.yml

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
- name: "Check if to create user"
2+
ansible.builtin.debug:
3+
msg: "Will create {{ user.key }}"
4+
when: lihas_users_filter is not defined or ( lihas_users_filter is defined and user.key in lihas_users_filter )
5+
tags:
6+
- users
7+
- name: "Check if to create user"
8+
ansible.builtin.debug:
9+
msg: "Will not create {{ user.key }}"
10+
when: lihas_users_filter is defined and user.key not in lihas_users_filter
11+
tags:
12+
- users
113
- name: Create/modify user
214
ansible.builtin.user:
315
name: "{{ user.key }}"
@@ -9,6 +21,7 @@
921
create_home: "{{ user.value.create_home | default(true) }}"
1022
groups: "{{ user.value.groups | default(omit) }}"
1123
state: "{{ user.value.state | default('present') }}"
24+
when: lihas_users_filter is not defined or ( lihas_users_filter is defined and user.key in lihas_users_filter )
1225
tags:
1326
- users
1427
become: "{{ lihas_become }}"
@@ -18,6 +31,7 @@
1831
state: directory
1932
owner: "{{ user.key }}"
2033
recurse: true
34+
when: ( lihas_users_filter is not defined or ( lihas_users_filter is defined and user.key in lihas_users_filter ) ) and user.value.force_home_user | default(false)
2135
when: user.value.force_home_user | default(false)
2236
tags:
2337
- users
@@ -31,6 +45,7 @@
3145
loop: "{{ user.value.ssh_authorized_keys | default([]) }}"
3246
loop_control:
3347
loop_var: sshkey
48+
when: lihas_users_filter is not defined or ( lihas_users_filter is defined and user.key in lihas_users_filter )
3449
tags:
3550
- users
3651
become: "{{ lihas_become }}"
@@ -45,5 +60,5 @@
4560
tags:
4661
- users
4762
become: "{{ lihas_become }}"
48-
when: user.value.sudo | default(false) or user.value.sudo_selfservice | default(false)
63+
when: ( lihas_users_filter is not defined or ( lihas_users_filter is defined and user.key in lihas_users_filter ) ) and ( user.value.sudo | default(false) or user.value.sudo_selfservice | default(false) )
4964
...

tasks/users.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@
1111
- users
1212
- name: "Users: show users"
1313
ansible.builtin.debug:
14-
var: users
14+
var: lihas_common_users
1515
verbosity: 1
16+
tags:
17+
- users
18+
- name: "Users: show users, flattened"
19+
ansible.builtin.debug:
20+
var: lihas_common_users | flatten(levels=1)
21+
verbosity: 1
22+
tags:
23+
- users
24+
#- name: "Users: filter wanted only"
25+
# ansible.builtin.set_fact:
26+
#
1627
- name: "Users: include tasks/10-users.yml"
1728
ansible.builtin.include_tasks: tasks/10-users.yml
1829
with_dict: "{{ lihas_common_users | default({}) }}"

0 commit comments

Comments
 (0)