Skip to content

Commit 6931e1c

Browse files
committed
Rework rekey-hosts.yml playbook
1 parent 0123c1f commit 6931e1c

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

etc/kayobe/ansible/rekey-hosts.yml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,63 @@
11
---
2+
# Playbook to rotate SSH keys across the cloud. By default it will rotate the
3+
# standard keys used by kayobe/kolla-ansible, but it can be configured for any
4+
# keys.
5+
26
- name: Rekey hosts
37
hosts: overcloud,seed,seed-hypervisor,infra-vms
48
gather_facts: false
59
vars:
610
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
11+
existing_private_key_path: "{{ ssh_private_key_path }}"
12+
existing_public_key_path: "{{ ssh_public_key_path }}"
13+
new_private_key_path: "{{ ssh_private_key_path }}"
14+
new_public_key_path: "{{ ssh_public_key_path }}"
15+
new_key_type: "{{ ssh_key_type }}"
716
rekey_users:
817
- stack
918
- kolla
1019
rekey_remove_existing_key: false
1120
tasks:
1221
- name: Stat existing private key file
1322
ansible.builtin.stat:
14-
path: "{{ ssh_private_key_path }}"
23+
path: "{{ existing_private_key_path }}"
1524
register: stat_result
1625
delegate_to: localhost
1726
run_once: true
1827

1928
- name: Fail when existing private key does not exist
2029
ansible.builtin.fail:
21-
msg: "No existing private key file found. Check ssh_private_key_path and is set correctly."
30+
msg: "No existing private key file found. Check existing_private_key_path is set correctly."
2231
when:
2332
- not stat_result.stat.exists
2433
delegate_to: localhost
2534
run_once: true
2635

2736
- name: Stat existing public key file
2837
ansible.builtin.stat:
29-
path: "{{ ssh_public_key_path }}"
38+
path: "{{ existing_public_key_path }}"
3039
register: stat_result
3140
delegate_to: localhost
3241
run_once: true
3342

3443
- name: Fail when existing public key does not exist
3544
ansible.builtin.fail:
36-
msg: "No existing public key file found. Check ssh_public_key_path and is set correctly."
45+
msg: "No existing public key file found. Check existing_public_key_path is set correctly."
3746
when:
3847
- not stat_result.stat.exists
3948
delegate_to: localhost
4049
run_once: true
4150

4251
- name: Generate a new SSH key
4352
community.crypto.openssh_keypair:
44-
path: "{{ ssh_private_key_path }}_new"
45-
type: "{{ ssh_key_type }}"
53+
path: "{{ existing_private_key_path }}_new"
54+
type: "{{ new_key_type }}"
4655
delegate_to: localhost
4756
run_once: true
4857

4958
- name: Set new authorized keys
5059
vars:
51-
lookup_path: "{{ ssh_private_key_path }}_new.pub"
60+
lookup_path: "{{ existing_private_key_path }}_new.pub"
5261
ansible.posix.authorized_key:
5362
user: "{{ item }}"
5463
state: present
@@ -57,32 +66,32 @@
5766
become: true
5867

5968
- name: Locally deprecate existing key (private)
60-
command: "mv {{ ssh_private_key_path }} {{ ssh_private_key_path }}_old"
69+
command: "mv {{ existing_private_key_path }} {{ existing_public_key_path }}_old"
6170
delegate_to: localhost
6271
run_once: true
6372

6473
- name: Locally deprecate existing key (public)
65-
command: "mv {{ ssh_public_key_path }} {{ ssh_public_key_path }}_old"
74+
command: "mv {{ existing_public_key_path }} {{ existing_public_key_path }}_old"
6675
delegate_to: localhost
6776
run_once: true
6877

6978
- name: Locally promote new key (private)
70-
command: "mv {{ ssh_private_key_path }}_new {{ ssh_private_key_path }}"
79+
command: "mv {{ existing_private_key_path }}_new {{ new_private_key_path }}"
7180
delegate_to: localhost
7281
run_once: true
7382

7483
- name: Locally promote new key (public)
75-
command: "mv {{ ssh_private_key_path }}_new.pub {{ ssh_public_key_path }}"
84+
command: "mv {{ existing_private_key_path }}_new.pub {{ new_public_key_path }}"
7685
delegate_to: localhost
7786
run_once: true
7887

7988
- name: Remove old key from hosts
8089
vars:
81-
lookup_path: "{{ ssh_public_key_path }}_old"
90+
lookup_path: "{{ existing_public_key_path }}_old"
8291
ansible.posix.authorized_key:
8392
user: "{{ item }}"
8493
state: absent
8594
key: "{{ lookup('file', lookup_path) }}"
8695
loop: "{{ rekey_users }}"
8796
become: true
88-
when: rekey_remove_existing_key
97+
when: rekey_remove_existing_key | bool

0 commit comments

Comments
 (0)