This question already has an answer here:
-
Ansible - Could not use lookup file module for a file under /etc/
1 answer
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
.
But it is using the local('/home/charlie/.ssh/id_rsa.pub').
All lookup plugins work locally on the Ansible control machine.
You can fetch the contents of a remote file with slurp
module, for example:
- name: Fetch authorized key from alternate location
slurp:
src: /home/other_user/.ssh/id_rsa.pub
register: slurped_key_b64
- name: Ensure the fetched key is set for charlie
authorized_key:
user: charlie
state: present
key: "{{ slurped_key_b64.content | b64decode }}"
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.
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.