Skip to content

Commit 14caeb4

Browse files
committed
rekey-host.yml remove-key tag
1 parent 6931e1c commit 14caeb4

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

etc/kayobe/ansible/rekey-hosts.yml

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
hosts: overcloud,seed,seed-hypervisor,infra-vms
88
gather_facts: false
99
vars:
10-
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
10+
# The existing key is the key that is currently used to access overcloud hosts
1111
existing_private_key_path: "{{ ssh_private_key_path }}"
1212
existing_public_key_path: "{{ ssh_public_key_path }}"
13+
# The new key is the key that will be generated by this playbook
1314
new_private_key_path: "{{ ssh_private_key_path }}"
1415
new_public_key_path: "{{ ssh_public_key_path }}"
1516
new_key_type: "{{ ssh_key_type }}"
17+
# The existing key will locally be moved to deprecated_key_path once it is replaced
18+
deprecated_key_path: ~/old_ssh_key
1619
rekey_users:
1720
- stack
1821
- kolla
@@ -66,12 +69,12 @@
6669
become: true
6770

6871
- name: Locally deprecate existing key (private)
69-
command: "mv {{ existing_private_key_path }} {{ existing_public_key_path }}_old"
72+
command: "mv {{ existing_private_key_path }} {{ deprecated_key_path }}"
7073
delegate_to: localhost
7174
run_once: true
7275

7376
- name: Locally deprecate existing key (public)
74-
command: "mv {{ existing_public_key_path }} {{ existing_public_key_path }}_old"
77+
command: "mv {{ existing_public_key_path }} {{ deprecated_key_path }}.pub"
7578
delegate_to: localhost
7679
run_once: true
7780

@@ -85,13 +88,30 @@
8588
delegate_to: localhost
8689
run_once: true
8790

88-
- name: Remove old key from hosts
89-
vars:
90-
lookup_path: "{{ existing_public_key_path }}_old"
91-
ansible.posix.authorized_key:
92-
user: "{{ item }}"
93-
state: absent
94-
key: "{{ lookup('file', lookup_path) }}"
95-
loop: "{{ rekey_users }}"
96-
become: true
91+
- block:
92+
- name: Stat old key file
93+
ansible.builtin.stat:
94+
path: "{{ deprecated_key_path }}.pub"
95+
register: stat_result
96+
delegate_to: localhost
97+
run_once: true
98+
99+
- name: Fail when deprecated public key does not exist
100+
ansible.builtin.fail:
101+
msg: "No deprecated public key file found. Check deprecated_key_path is set correctly."
102+
when:
103+
- not stat_result.stat.exists
104+
delegate_to: localhost
105+
run_once: true
106+
107+
- name: Remove old key from hosts
108+
vars:
109+
lookup_path: "{{ deprecated_key_path }}.pub"
110+
ansible.posix.authorized_key:
111+
user: "{{ item }}"
112+
state: absent
113+
key: "{{ lookup('file', lookup_path) }}"
114+
loop: "{{ rekey_users }}"
115+
become: true
116+
tags: remove-key
97117
when: rekey_remove_existing_key | bool

0 commit comments

Comments
 (0)