|
3 | 3 | hosts: overcloud,seed,seed-hypervisor,infra-vms
|
4 | 4 | gather_facts: false
|
5 | 5 | 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 |
8 | 13 | 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 |
12 | 18 | delegate_to: localhost
|
| 19 | + run_once: true |
13 | 20 |
|
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 |
27 | 28 |
|
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 |
37 | 35 |
|
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" |
39 | 39 | ansible.posix.authorized_key:
|
40 | 40 | user: "{{ item }}"
|
41 | 41 | 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 }}" |
46 | 44 | become: true
|
47 | 45 |
|
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" |
50 | 48 | delegate_to: localhost
|
| 49 | + run_once: true |
51 | 50 |
|
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" |
54 | 53 | delegate_to: localhost
|
| 54 | + run_once: true |
55 | 55 |
|
56 | 56 | - 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 }}" |
58 | 58 | delegate_to: localhost
|
| 59 | + run_once: true |
59 | 60 |
|
60 | 61 | - 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" |
62 | 63 | delegate_to: localhost
|
| 64 | + run_once: true |
63 | 65 |
|
64 | 66 | - name: Remove old key from hosts
|
| 67 | + vars: |
| 68 | + lookup_path: "{{ existing_key_path }}_old.pub" |
65 | 69 | ansible.posix.authorized_key:
|
66 | 70 | user: "{{ item }}"
|
67 | 71 | 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 }}" |
72 | 74 | become: true
|
| 75 | + when: rekey_remove_existing_key |
0 commit comments