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:
1 file changed
+14
-28
@@ -52,27 +52,6 @@
|
|||||||
root.ssh.authorized_keys is not defined or
|
root.ssh.authorized_keys is not defined or
|
||||||
root.ssh is not defined
|
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"
|
- name: "Create private and public SSH key"
|
||||||
when: root.ssh is defined and
|
when: root.ssh is defined and
|
||||||
root.ssh.private_keys is defined and
|
root.ssh.private_keys is defined and
|
||||||
@@ -90,18 +69,25 @@
|
|||||||
label: "{{ item.filename }}"
|
label: "{{ item.filename }}"
|
||||||
|
|
||||||
- name: "Extract public SSH keys from private keys"
|
- name: "Extract public SSH keys from private keys"
|
||||||
ansible.builtin.shell:
|
ansible.builtin.command:
|
||||||
args:
|
argv: [ "ssh-keygen", "-y", "-f", "/root/.ssh/{{ item.filename }}" ]
|
||||||
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"
|
|
||||||
register: _root_ssh_pub_key_extraction
|
register: _root_ssh_pub_key_extraction
|
||||||
changed_when: _root_ssh_pub_key_extraction.rc == 0
|
changed_when: false
|
||||||
failed_when: _root_ssh_pub_key_extraction.rc > 0
|
|
||||||
loop: "{{ root.ssh.private_keys }}"
|
loop: "{{ root.ssh.private_keys }}"
|
||||||
loop_control:
|
loop_control:
|
||||||
label: "{{ item.filename }}"
|
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"
|
- name: "Create custom SSH client config"
|
||||||
ansible.builtin.template:
|
ansible.builtin.template:
|
||||||
src: root/.ssh/config.j2
|
src: root/.ssh/config.j2
|
||||||
Reference in new issue
Block a user