This question already has an answer here:
I am facing a problem of copying ssh key between two accounts on a remote server. I have remote server called "rmt", on rmt I have one account called "clado" i want to copy the /root/.ssh/authorized_keys
(on rmt) to /home/clado/.ssh/authorized_keys
(on rmt) using Ansible.
I got this sample code:
- name: Set authorized key in alternate location
authorized_key:
user: charlie
state: present
key: "{{ lookup('file', '/home/charlie/.ssh/id_rsa.pub') }}"
But it is using the local /home/charlie/.ssh/id_rsa.pub
.
if you are picking it from
/root/.ssh/authorized
than replace home/charlie/.ssh/id_rsa.pub from /root/.ssh/authorized_keys
and do with it with sudo . Use
become: true
argument in your task.All lookup plugins work locally on the Ansible control machine.
You can fetch the contents of a remote file with
slurp
module, for example:Customise the details, because your description and your code don't match.
But generally this flow doesn't make much sense from system management point of view. Assign the key from the control machine.