I need one remote to be able to connect to another, to fetch some data, so I need to share public key from remote A to remote B.
But it seems that ansible is a bit clunky on this part. Or I did not see solid solution.
I saw some people suggest, to fetch public key on control machine and then copy it to another remote.
But it seems quite clunky that we need to work around it. With ansible you have access to both remotes, so isn't there a simpler way to do it (that ansible would handle such transfer automatically)?
Let say I have public key on remote A
in ~/.ssh/id_ed25519.pub
. How do I transfer it and add it to authorized_keys
on remote B
?
Update
Tried to fetch key like this:
- name: Fetch public key data from backups_host
ansible.builtin.set_fact:
backups_host_public_key: "{{ lookup('file', '~/.ssh/id_ed25519.pub') }}"
delegate_to: "{{ backups_host }}"
when: backups_host is defined
It does fetch it, but it fetches from my computer, not from delegated remote..
Solved it with this:
- block:
- name: Fetch public key data from backups_host
ansible.builtin.command: cat ~/.ssh/id_ed25519.pub
delegate_to: "{{ backups_host }}"
register: public_key_data
- name: "Add public key from backups_host"
ansible.posix.authorized_key:
user: root
state: present
key: "{{ public_key_data.stdout }}"
when: backups_host is defined
I use delegate_to
, to read public key data from remote A and save it in variable. Then pass it on remote B, when adding authorized_key
.