Skip to content

Commit 0123c1f

Browse files
committed
Change rekey playbook to use existing ssh vars
1 parent c527579 commit 0123c1f

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

etc/kayobe/ansible/rekey-hosts.yml

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,52 @@
33
hosts: overcloud,seed,seed-hypervisor,infra-vms
44
gather_facts: false
55
vars:
6-
new_key_type: ed25519
76
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
87
rekey_users:
98
- stack
109
- kolla
11-
existing_key_path: "~/.ssh/id_rsa"
12-
rekey_remove_existing_key: true
10+
rekey_remove_existing_key: false
1311
tasks:
14-
- name: Stat existing key file
12+
- name: Stat existing private key file
1513
ansible.builtin.stat:
16-
path: "{{ existing_key_path }}"
14+
path: "{{ ssh_private_key_path }}"
1715
register: stat_result
1816
delegate_to: localhost
1917
run_once: true
2018

21-
- name: Fail when existing key does not exist
19+
- name: Fail when existing private key does not exist
2220
ansible.builtin.fail:
23-
msg: "No existing key file found. Check existing_key_path is set correctly."
21+
msg: "No existing private key file found. Check ssh_private_key_path and is set correctly."
22+
when:
23+
- not stat_result.stat.exists
24+
delegate_to: localhost
25+
run_once: true
26+
27+
- name: Stat existing public key file
28+
ansible.builtin.stat:
29+
path: "{{ ssh_public_key_path }}"
30+
register: stat_result
31+
delegate_to: localhost
32+
run_once: true
33+
34+
- name: Fail when existing public key does not exist
35+
ansible.builtin.fail:
36+
msg: "No existing public key file found. Check ssh_public_key_path and is set correctly."
2437
when:
2538
- not stat_result.stat.exists
2639
delegate_to: localhost
2740
run_once: true
2841

2942
- name: Generate a new SSH key
3043
community.crypto.openssh_keypair:
31-
path: "~/.ssh/id_{{ new_key_type }}_new"
32-
type: "{{ new_key_type }}"
44+
path: "{{ ssh_private_key_path }}_new"
45+
type: "{{ ssh_key_type }}"
3346
delegate_to: localhost
3447
run_once: true
3548

3649
- name: Set new authorized keys
3750
vars:
38-
lookup_path: "~/.ssh/id_{{ new_key_type }}_new.pub"
51+
lookup_path: "{{ ssh_private_key_path }}_new.pub"
3952
ansible.posix.authorized_key:
4053
user: "{{ item }}"
4154
state: present
@@ -44,28 +57,28 @@
4457
become: true
4558

4659
- name: Locally deprecate existing key (private)
47-
command: "mv {{ existing_key_path }} {{ existing_key_path }}_old"
60+
command: "mv {{ ssh_private_key_path }} {{ ssh_private_key_path }}_old"
4861
delegate_to: localhost
4962
run_once: true
5063

5164
- name: Locally deprecate existing key (public)
52-
command: "mv {{ existing_key_path }}.pub {{ existing_key_path }}_old.pub"
65+
command: "mv {{ ssh_public_key_path }} {{ ssh_public_key_path }}_old"
5366
delegate_to: localhost
5467
run_once: true
5568

5669
- name: Locally promote new key (private)
57-
command: "mv ~/.ssh/id_{{ new_key_type }}_new ~/.ssh/id_{{ new_key_type }}"
70+
command: "mv {{ ssh_private_key_path }}_new {{ ssh_private_key_path }}"
5871
delegate_to: localhost
5972
run_once: true
6073

6174
- name: Locally promote new key (public)
62-
command: " mv ~/.ssh/id_{{ new_key_type }}_new.pub ~/.ssh/id_{{ new_key_type }}.pub"
75+
command: "mv {{ ssh_private_key_path }}_new.pub {{ ssh_public_key_path }}"
6376
delegate_to: localhost
6477
run_once: true
6578

6679
- name: Remove old key from hosts
6780
vars:
68-
lookup_path: "{{ existing_key_path }}_old.pub"
81+
lookup_path: "{{ ssh_public_key_path }}_old"
6982
ansible.posix.authorized_key:
7083
user: "{{ item }}"
7184
state: absent

0 commit comments

Comments
 (0)