fix(ssh)!: refresh public keys after key rotation

Derive public keys on every run and update their files only when the content changes, so existing public keys cannot become stale.

BREAKING CHANGE: The role no longer generates a key pair automatically when root.ssh.private_keys is empty or omitted. Provide private keys explicitly when needed.

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
volker.raschekandCopilot committed 2026-10-18 14:46:48 +02:00
1 parent 44716aa722
commit c2f50c4423
1 file changed
+14 -28
+14 -28
View File
@@ -52,27 +52,6 @@
root.ssh.authorized_keys is not defined or
root.ssh is not defined
- name: "Generate private and public SSH key"
when: root.ssh is defined and
(
root.ssh.private_keys is not defined or
root.ssh.private_keys | length <= 0
)
block:
- name: "Check if private SSH key already exists"
ansible.builtin.stat:
path: "/root/.ssh/{{ inventory_hostname_short }}.key"
register: stat_result
- name: "Generate private SSH key"
community.crypto.openssh_keypair:
path: "/root/.ssh/{{ inventory_hostname_short }}.key"
type: ed25519
size: 512
state: present
comment: "root@{{ inventory_hostname_short }}"
force: false
when: not stat_result.stat.exists
- name: "Create private and public SSH key"
when: root.ssh is defined and
root.ssh.private_keys is defined and
@@ -90,18 +69,25 @@
label: "{{ item.filename }}"
- name: "Extract public SSH keys from private keys"
ansible.builtin.shell:
args:
executable: /bin/bash
cmd: "ssh-keygen -y -f {{ ('/root/.ssh/' + item.filename) | quote }} > {{ ('/root/.ssh/' + item.filename + '.pub') | quote }}"
creates: "/root/.ssh/{{ item.filename }}.pub"
ansible.builtin.command:
argv: [ "ssh-keygen", "-y", "-f", "/root/.ssh/{{ item.filename }}" ]
register: _root_ssh_pub_key_extraction
changed_when: _root_ssh_pub_key_extraction.rc == 0
failed_when: _root_ssh_pub_key_extraction.rc > 0
changed_when: false
loop: "{{ root.ssh.private_keys }}"
loop_control:
label: "{{ item.filename }}"
- name: "Write derived public SSH keys"
ansible.builtin.copy:
content: "{{ item.stdout }}\n"
dest: "/root/.ssh/{{ item.item.filename }}.pub"
owner: "root"
group: "root"
mode: "0644"
loop: "{{ _root_ssh_pub_key_extraction.results }}"
loop_control:
label: "{{ item.item.filename }}"
- name: "Create custom SSH client config"
ansible.builtin.template:
src: root/.ssh/config.j2