Skip to content

Commit 329175f

Browse files
committed
WIP: run ALL userdir tasks on basic_users_homedir_host
1 parent b6941a9 commit 329175f

File tree

3 files changed

+104
-42
lines changed

3 files changed

+104
-42
lines changed

ansible/roles/basic_users/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ without requiring LDAP etc. Features:
1212

1313
> [!IMPORTANT] This role assumes that `$HOME` for users managed by this role
1414
(e.g. not `rocky` and other system users) is on a shared filesystem. The export
15-
of this sharef filesystem may be root squashed if the server is in the
16-
`basic_user` group - see configuration advice below.
15+
of this shared filesystem may be root squashed if its server is in the
16+
`basic_user` group - see configuration examples below.
1717

1818
Role Variables
1919
--------------
@@ -35,8 +35,8 @@ Role Variables
3535
- Any other keys may present for other purposes (i.e. not used by this role).
3636
- `basic_users_groups`: Optional, default empty list. A list of mappings defining information for each group. Mapping keys/values are passed through as parameters to [ansible.builtin.group](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/group_module.html) and default values are as given there.
3737
- `basic_users_override_sssd`: Optional bool, default false. Whether to disable `sssd` when ensuring users/groups exist with this role. Permits creating local users/groups even if they clash with users provided via sssd (e.g. from LDAP). Ignored if host is not in group `sssd` as well. Note with this option active `sssd` will be stopped and restarted each time this role is run.
38-
- `basic_users_homedir_host`: Optional inventory hostname. Host to run actions
39-
which manipulate the home directories. If home directories are exported with
38+
- `basic_users_homedir_host`: Optional inventory hostname specifying the host
39+
on which to manipulate home directories (assuming `create_home` is true). If home directories are exported with
4040
root squash, this *must* specify that server. If root squash is not used it
4141
can be any node in the `basic_users` group. Default is the `control` node,
4242
which assumes the default appliance NFS-exported home directory configuration.

ansible/roles/basic_users/defaults/main.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
basic_users_homedir_host: "{{ groups['control'] | first }}" # no way, generally, to find the nfs_server
22
basic_users_homedir_host_path: /exports/home
3-
_basic_users_manage_homedir: "{{ ansible_hostname == basic_users_homedir_host }}"
3+
# _basic_users_manage_homedir: "{{ ansible_hostname == basic_users_homedir_host }}"
44
basic_users_userdefaults:
5-
state: present
6-
home: "{{ basic_users_homedir_host_path }}/{{ item.name }}"
7-
create_home: "{{ _basic_users_manage_homedir }}"
8-
generate_ssh_key: "{{ _basic_users_manage_homedir }}"
5+
state: present # need this here so don't have to add default() everywhere
6+
generate_ssh_key: true
97
ssh_key_comment: "{{ item.name }}"
108
ssh_key_type: ed25519
119
shell: "{{'/sbin/nologin' if 'control' in group_names else omit }}"

ansible/roles/basic_users/tasks/main.yml

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,110 @@
2121
ansible.builtin.group: "{{ item }}"
2222
loop: "{{ basic_users_groups }}"
2323

24-
- name: Create users and generate public keys
25-
user: "{{ basic_users_userdefaults | combine(item) | filter_user_params() }}"
24+
- name: Create users
25+
user: "{{ basic_users_userdefaults | combine(item) | filter_user_params() | combine(_disable_homedir) }}"
2626
loop: "{{ basic_users_users }}"
2727
loop_control:
28-
label: "{{ item.name }} [{{ item.state | default('present') }}]"
29-
register: basic_users_info
28+
label: "{{ item.name }}"
29+
vars:
30+
_disable_homedir: # ensure this task doesn't touch $HOME
31+
create_home: false
32+
generate_ssh_key: false
3033

3134
- name: Restart sssd if required
3235
systemd:
3336
name: sssd
3437
state: started
3538
when: _stop_sssd is changed
3639

37-
- name: Write supplied public key as authorized for SSH access
38-
authorized_key:
39-
user: "{{ item.name }}"
40-
state: present
41-
key: "{{ item.public_key }}"
42-
become_user: "{{ item.name }}"
43-
loop: "{{ basic_users_users }}"
44-
loop_control:
45-
label: "{{ item.name }} [{{ item.state | default('present') }}]"
46-
when:
47-
- item.state | default('present') == 'present'
48-
- item.public_key is defined
49-
- _basic_users_manage_homedir
40+
- name: Modify home directories
41+
delegate_to: "{{ basic_users_homedir_host }}"
42+
run_once: true
43+
block:
44+
- name: Create home directories
45+
# doesn't delete with state=absent, same as ansible.builtin.user
46+
file:
47+
state: directory
48+
path: "{{ item.home | default( basic_users_homedir_host_path + '/' + item.name ) }}"
49+
owner: "{{ item.name }}"
50+
group: "{{ item.name }}"
51+
mode: u=rwX,go=
52+
loop: "{{ basic_users_users }}"
53+
loop_control:
54+
label: "{{ item.name }}"
55+
when:
56+
- item.state | default('present') == 'present'
57+
- item.create_home | default(true) | bool
5058

51-
- name: Write generated public key as authorized for SSH access
52-
# this only runs on the _basic_users_manage_homedir so has registered var
53-
# from that host too
54-
authorized_key:
55-
user: "{{ item.name }}"
56-
state: present
57-
manage_dir: no
58-
key: "{{ item.ssh_public_key }}"
59-
become_user: "{{ item.name }}"
60-
loop: "{{ basic_users_info.results }}"
61-
loop_control:
62-
label: "{{ item.name }}"
63-
when:
64-
- item.ssh_public_key is defined
65-
- _basic_users_manage_homedir
59+
- name: Create ~/.ssh directories
60+
file:
61+
state: directory
62+
path: "{{ _homedir }}/.ssh/"
63+
owner: "{{ item.name }}"
64+
group: "{{ item.name }}"
65+
mode: u=rwX,go=
66+
vars:
67+
_homedir: "{{ item.home | default( basic_users_homedir_host_path + '/' + item.name ) }}"
68+
become_user: "{{ item.name }}"
69+
loop: "{{ basic_users_users }}"
70+
loop_control:
71+
label: "{{ item.name }}"
72+
when:
73+
- item.state | default('present') == 'present'
74+
75+
- name: Generate cluster ssh key
76+
community.crypto.openssh_keypair:
77+
path: "{{ item.ssh_key_file | default(_homedir + '/.ssh/' + _ssh_key_type )}}"
78+
type: "{{ _ssh_key_type }}"
79+
vars:
80+
_homedir: "{{ item.home | default( basic_users_homedir_host_path + '/' + item.name ) }}"
81+
_ssh_key_type: "{{ item.ssh_key_type | default('ed25519') }}"
82+
become_user: "{{ item.name }}"
83+
loop: "{{ basic_users_users }}"
84+
loop_control:
85+
label: "{{ item.name }}"
86+
when:
87+
- item.state | default('present') == 'present'
88+
- item.generate_ssh_key | default(true) | bool
89+
register: _cluster_ssh_keypair
90+
91+
# - debug:
92+
# var: _cluster_ssh_keypair
93+
# - meta: end_here
94+
95+
- name: Write supplied public key as authorized for SSH access
96+
ansible.posix.authorized_key:
97+
user: "{{ item.name }}"
98+
state: present
99+
manage_dir: false
100+
key: "{{ item.public_key }}"
101+
path: "{{ _homedir }}/.ssh/authorized_keys"
102+
vars:
103+
_homedir: "{{ item.home | default( basic_users_homedir_host_path + '/' + item.name ) }}"
104+
become_user: "{{ item.name }}"
105+
loop: "{{ basic_users_users }}"
106+
loop_control:
107+
label: "{{ item.name }}"
108+
when:
109+
- item.state | default('present') == 'present'
110+
- item.public_key is defined
111+
112+
- name: Write generated public key as authorized for SSH access
113+
ansible.posix.authorized_key:
114+
user: "{{ item.item.name }}"
115+
state: present
116+
manage_dir: false
117+
key: "{{ item.public_key }}"
118+
path: "{{ _homedir }}/.ssh/authorized_keys"
119+
vars:
120+
_homedir: "{{ item.item.home | default( basic_users_homedir_host_path + '/' + item.item.name ) }}"
121+
become_user: "{{ item.item.name }}"
122+
loop: "{{ _cluster_ssh_keypair.results }}"
123+
loop_control:
124+
label: "{{ item.item.name }}"
125+
when:
126+
- item.item.state | default('present') == 'present'
127+
- "'public_key' in item"
66128

67129
- name: Write sudo rules
68130
blockinfile:
@@ -72,4 +134,6 @@
72134
loop: "{{ basic_users_users }}"
73135
loop_control:
74136
label: "{{ item.name }}"
75-
when: "'sudo' in item"
137+
when:
138+
- item.state | default('present') == 'present'
139+
- "'sudo' in item"

0 commit comments

Comments
 (0)