Skip to content

Commit 456db6d

Browse files
committed
do ssh key handling on client node to simplify finding /home/rocky
1 parent 329175f commit 456db6d

File tree

2 files changed

+60
-66
lines changed

2 files changed

+60
-66
lines changed

ansible/roles/basic_users/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@ 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 specifying the host
39-
on which to manipulate home directories (assuming `create_home` is true). If home directories are exported with
40-
root squash, this *must* specify that server. If root squash is not used it
41-
can be any node in the `basic_users` group. Default is the `control` node,
42-
which assumes the default appliance NFS-exported home directory configuration.
38+
- `basic_users_homedir_host`: Optional inventory hostname defining the host
39+
to use to create home directories. If the home directory export is root squashed,
40+
this host *must* be the home directory server. Default is the `control` node,
41+
for the default appliance NFS-exported home directory configuration.
42+
Not relevant if `create_home` is false for all users.
4343
- `basic_users_homedir_host_path`: Optional path prefix for home directories on
44-
the `basic_users_homedir_host`. Default is `/exports/home` which assumes the
45-
default appliance NFS-exported home directory configuration. **NB**: This may
46-
vary depending on whether
47-
`basic_users_homedir_host` is a server or a client for the home directories.
44+
the `basic_users_homedir_host`, i.e. on the "server side". Default is
45+
`/exports/home`, for the default appliance NFS-exported home directory
46+
configuration.
4847

4948
Dependencies
5049
------------

ansible/roles/basic_users/tasks/main.yml

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,56 @@
3131
create_home: false
3232
generate_ssh_key: false
3333

34+
- name: Write sudo rules
35+
blockinfile:
36+
path: /etc/sudoers.d/80-{{ item.name}}-user
37+
block: "{{ item.sudo }}"
38+
create: true
39+
loop: "{{ basic_users_users }}"
40+
loop_control:
41+
label: "{{ item.name }}"
42+
when:
43+
- item.state | default('present') == 'present'
44+
- "'sudo' in item"
45+
3446
- name: Restart sssd if required
3547
systemd:
3648
name: sssd
3749
state: started
3850
when: _stop_sssd is changed
3951

40-
- name: Modify home directories
52+
# This task runs (only) on the home diretory server, if in the group, so it can
53+
# handle root squashed exports
54+
- name: Create home directories
55+
# doesn't delete with state=absent, same as ansible.builtin.user
56+
file:
57+
state: directory
58+
path: "{{ item.home | default( basic_users_homedir_host_path + '/' + item.name ) }}"
59+
owner: "{{ item.name }}"
60+
group: "{{ item.name }}"
61+
mode: u=rwX,go=
4162
delegate_to: "{{ basic_users_homedir_host }}"
4263
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
64+
loop: "{{ basic_users_users }}"
65+
loop_control:
66+
label: "{{ item.name }}"
67+
when:
68+
- item.state | default('present') == 'present'
69+
- item.create_home | default(true) | bool
5870

71+
# The following tasks deliberately run on a (single) *client* node, so that
72+
# home directory paths are easily constructed, becoming each user so that root
73+
# squash doesn't matter
74+
- delegate_to: "{{ groups['basic_users'] | difference([basic_users_homedir_host]) | first }}"
75+
run_once: true
76+
block:
5977
- name: Create ~/.ssh directories
6078
file:
6179
state: directory
62-
path: "{{ _homedir }}/.ssh/"
80+
path: ~/.ssh/
6381
owner: "{{ item.name }}"
6482
group: "{{ item.name }}"
6583
mode: u=rwX,go=
66-
vars:
67-
_homedir: "{{ item.home | default( basic_users_homedir_host_path + '/' + item.name ) }}"
6884
become_user: "{{ item.name }}"
6985
loop: "{{ basic_users_users }}"
7086
loop_control:
@@ -74,10 +90,9 @@
7490

7591
- name: Generate cluster ssh key
7692
community.crypto.openssh_keypair:
77-
path: "{{ item.ssh_key_file | default(_homedir + '/.ssh/' + _ssh_key_type )}}"
93+
path: "{{ item.ssh_key_file | default('~/.ssh/' + _ssh_key_type )}}" # NB: ssh_key_file is from ansible.builtin.user
7894
type: "{{ _ssh_key_type }}"
7995
vars:
80-
_homedir: "{{ item.home | default( basic_users_homedir_host_path + '/' + item.name ) }}"
8196
_ssh_key_type: "{{ item.ssh_key_type | default('ed25519') }}"
8297
become_user: "{{ item.name }}"
8398
loop: "{{ basic_users_users }}"
@@ -88,36 +103,13 @@
88103
- item.generate_ssh_key | default(true) | bool
89104
register: _cluster_ssh_keypair
90105

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
106+
- name: Write generated cluster ssh key to authorized_keys
113107
ansible.posix.authorized_key:
114108
user: "{{ item.item.name }}"
115109
state: present
116110
manage_dir: false
117111
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 ) }}"
112+
path: ~/.ssh/authorized_keys
121113
become_user: "{{ item.item.name }}"
122114
loop: "{{ _cluster_ssh_keypair.results }}"
123115
loop_control:
@@ -126,14 +118,17 @@
126118
- item.item.state | default('present') == 'present'
127119
- "'public_key' in item"
128120

129-
- name: Write sudo rules
130-
blockinfile:
131-
path: /etc/sudoers.d/80-{{ item.name}}-user
132-
block: "{{ item.sudo }}"
133-
create: true
134-
loop: "{{ basic_users_users }}"
135-
loop_control:
136-
label: "{{ item.name }}"
137-
when:
138-
- item.state | default('present') == 'present'
139-
- "'sudo' in item"
121+
- name: Write supplied public key to authorized_keys
122+
ansible.posix.authorized_key:
123+
user: "{{ item.name }}"
124+
state: present
125+
manage_dir: false
126+
key: "{{ item.public_key }}"
127+
path: ~/.ssh/authorized_keys
128+
become_user: "{{ item.name }}"
129+
loop: "{{ basic_users_users }}"
130+
loop_control:
131+
label: "{{ item.name }}"
132+
when:
133+
- item.state | default('present') == 'present'
134+
- item.public_key is defined

0 commit comments

Comments
 (0)