Skip to content

Commit 799bb26

Browse files
committed
Rekey playbook misc improvements
1 parent 3e57769 commit 799bb26

File tree

2 files changed

+46
-43
lines changed

2 files changed

+46
-43
lines changed

etc/kayobe/ansible/rekey-hosts.yml

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,73 @@
33
hosts: overcloud,seed,seed-hypervisor,infra-vms
44
gather_facts: false
55
vars:
6-
ansible_user: stack
7-
ansible_python_interpreter: /usr/bin/python3
6+
new_key_type: ed25519
7+
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
8+
rekey_users:
9+
- stack
10+
- kolla
11+
existing_key_path: "~/.ssh/id_rsa"
12+
rekey_remove_existing_key: true
813
tasks:
9-
- name: Generate a fresh SSH key
10-
community.crypto.openssh_keypair:
11-
path: ~/.ssh/id_rsa_new
14+
- name: Stat existing key file
15+
ansible.builtin.stat:
16+
path: "{{ existing_key_path }}"
17+
register: stat_result
1218
delegate_to: localhost
19+
run_once: true
1320

14-
# - name: Copy new key to hosts
15-
# ansible.builtin.copy:
16-
# src: /tmp/id_rsa_new.pub
17-
# dest: /tmp/id_rsa_new.pub
18-
# mode: '0600'
19-
# become: true
20-
21-
- name: Copy old key to hosts
22-
ansible.builtin.copy:
23-
src: ~/.ssh/id_rsa.pub
24-
dest: /tmp/id_rsa_old.pub
25-
mode: '0777'
26-
become: true
21+
- name: Fail when existing key does not exist
22+
ansible.builtin.fail:
23+
msg: "No existing key file found. Check existing_key_path is set correctly."
24+
when:
25+
- not stat_result.stat.exists
26+
delegate_to: localhost
27+
run_once: true
2728

28-
- name: Set new stack authorized keys
29-
ansible.posix.authorized_key:
30-
user: "{{ item }}"
31-
state: present
32-
key: "{{ lookup('file', '~/.ssh/id_rsa_new.pub') }}"
33-
loop:
34-
- "stack"
35-
- "kolla"
36-
become: true
29+
- name: Generate a new SSH key
30+
community.crypto.openssh_keypair:
31+
path: "~/.ssh/id_{{ new_key_type }}_new"
32+
type: "{{ new_key_type }}"
33+
delegate_to: localhost
34+
run_once: true
3735

38-
- name: Set new stack authorized keys
36+
- name: Set new authorized keys
37+
vars:
38+
lookup_path: "~/.ssh/id_{{ new_key_type }}_new.pub"
3939
ansible.posix.authorized_key:
4040
user: "{{ item }}"
4141
state: present
42-
key: "{{ lookup('file', '~/.ssh/id_rsa_new.pub') }}"
43-
loop:
44-
- "stack"
45-
- "kolla"
42+
key: "{{ lookup('file', lookup_path) }}"
43+
loop: "{{ rekey_users }}"
4644
become: true
4745

48-
- name: Locally deprecate old key (private)
49-
command: "mv ~/.ssh/id_rsa ~/.ssh/id_rsa_old"
46+
- name: Locally deprecate existing key (private)
47+
command: "mv {{ existing_key_path }} {{ existing_key_path }}_old"
5048
delegate_to: localhost
49+
run_once: true
5150

52-
- name: Locally deprecate old key (public)
53-
command: "mv ~/.ssh/id_rsa.pub ~/.ssh/id_rsa_old.pub"
51+
- name: Locally deprecate existing key (public)
52+
command: "mv {{ existing_key_path }}.pub {{ existing_key_path }}_old.pub"
5453
delegate_to: localhost
54+
run_once: true
5555

5656
- name: Locally promote new key (private)
57-
command: "mv ~/.ssh/id_rsa_new ~/.ssh/id_rsa"
57+
command: "mv ~/.ssh/id_{{ new_key_type }}_new ~/.ssh/id_{{ new_key_type }}"
5858
delegate_to: localhost
59+
run_once: true
5960

6061
- name: Locally promote new key (public)
61-
command: " mv ~/.ssh/id_rsa_new.pub ~/.ssh/id_rsa.pub"
62+
command: " mv ~/.ssh/id_{{ new_key_type }}_new.pub ~/.ssh/id_{{ new_key_type }}.pub"
6263
delegate_to: localhost
64+
run_once: true
6365

6466
- name: Remove old key from hosts
67+
vars:
68+
lookup_path: "{{ existing_key_path }}_old.pub"
6569
ansible.posix.authorized_key:
6670
user: "{{ item }}"
6771
state: absent
68-
key: "{{ lookup('file', '/tmp/id_rsa_old.pub') }}"
69-
loop:
70-
- "stack"
71-
- "kolla"
72+
key: "{{ lookup('file', lookup_path) }}"
73+
loop: "{{ rekey_users }}"
7274
become: true
75+
when: rekey_remove_existing_key

releasenotes/notes/add-rekey-playbook-0065c5057b1639f8.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
features:
33
- |
44
Added the ``rekey-hosts.yml`` playbook to automatically rotate the SSH
5-
keys on all hosts in the cloud for the stack and kolla users.
5+
keys on all hosts.

0 commit comments

Comments
 (0)