Skip to content

Commit a1e5bd7

Browse files
committed
Reworked persist_hostkeys role to use common set of persistent keys from state directory
1 parent 781c2d4 commit a1e5bd7

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# persist_hostkeys
22

3-
Save hostkeys to persistent storage and restore them after a rebuild/reimage.
3+
Idempotently generates a persistent set of hostkeys and restores them after a rebuild/reimage.
44

5-
Add hosts to the `persist_hostkeys` group to enable.
6-
7-
This role has no variables but hosts in this group must have `appliances_state_dir`
8-
defined as a directory they can write to on persistent storage.
5+
Add hosts to the `persist_hostkeys` group to enable. All hosts in group will share the same set hostkeys.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
persist_hostkeys_state_server: "{{ groups['control'] | first }}"
2+
persist_hostkeys_state_dir: "{{ hostvars[persist_hostkeys_state_server]['appliances_state_dir'] }}/hostkeys"

ansible/roles/persist_hostkeys/tasks/main.yml

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
---
22

3-
- name: Ensure hostkeys directory exists on persistent storage
4-
file:
5-
path: "{{ appliances_state_dir }}/hostkeys/{{ inventory_hostname }}"
6-
state: directory
7-
owner: root
8-
group: root
9-
mode: 0600
3+
- name: Generate persistent hostkeys in state directory
4+
delegate_to: "{{ persist_hostkeys_state_server }}"
5+
block:
6+
- name: Ensure hostkeys directory exists on persistent storage
7+
file:
8+
path: "{{ persist_hostkeys_state_dir }}"
9+
state: directory
10+
owner: root
11+
group: root
12+
mode: 0600
1013

11-
- name: Copy hostkeys from persistent storage
12-
# won't fail if no keys are in persistent storage
13-
copy:
14-
src: "{{ appliances_state_dir }}/hostkeys/{{ inventory_hostname }}/"
15-
dest: /etc/ssh/
16-
remote_src: true
14+
- name: Check for existing hostkeys
15+
find:
16+
paths: "{{ persist_hostkeys_state_dir }}/"
17+
register: _files_found
18+
19+
- name: Generate hostkeys
20+
when: _files_found.matched == 0
21+
shell:
22+
cmd: |
23+
mkdir -p {{ persist_hostkeys_state_dir }}/etc/ssh
24+
ssh-keygen -A -N \"\" -f {{ persist_hostkeys_state_dir }}
25+
mv {{ persist_hostkeys_state_dir }}/etc/ssh/* {{ persist_hostkeys_state_dir }}
26+
rm -rf {{ persist_hostkeys_state_dir }}/etc/ssh
27+
28+
- name: Get created key names
29+
find:
30+
path: "{{ persist_hostkeys_state_dir }}/"
31+
register: _find_ssh_keys
1732

18-
- name: Find hostkeys
19-
find:
20-
path: /etc/ssh/
21-
patterns: ssh_host_*_key*
22-
register: _find_ssh_keys
33+
- name: Create in-memory copies of keys
34+
ansible.builtin.slurp:
35+
src: "{{ item.path }}"
36+
loop: "{{ _find_ssh_keys.files }}"
37+
register: _slurp_keys
2338

24-
- name: Persist hostkeys
39+
- name: Copy keys to hosts
40+
no_log: true
2541
copy:
26-
dest: "{{ appliances_state_dir }}/hostkeys/{{ inventory_hostname }}/"
27-
src: "{{ item }}"
28-
remote_src: true
29-
mode: preserve
30-
loop: "{{ _find_ssh_keys.files | map(attribute='path') }}"
42+
content: "{{ item.content | b64decode }}"
43+
dest: "/etc/ssh/{{ item.source | regex_search('[^/]+$') }}"
44+
loop: "{{ _slurp_keys.results }}"
3145

3246
- meta: reset_connection
33-

environments/common/layouts/everything

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ openhpc
6969
[manila]
7070
# Hosts to configure for manila fileshares
7171

72-
[persist_hostkeys]
73-
# Hosts to persist hostkeys for across reimaging. NB: Requires appliances_state_dir on hosts.
72+
[persist_hostkeys:children]
73+
# Hosts to use common set of hostkeys which persist across reimaging.
74+
login
75+
openondemand
7476

7577
[squid]
7678
# Hosts to run squid proxy

0 commit comments

Comments
 (0)