# how do i lookup id\_rsa.pub on a host and copy it to authorized keys on multiple servers

**URL:** <https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877>\
**Category:** Ansible Project\
**Created:** [July 20, 2022, 1:24pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877 "2022-07-20T13:24:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 20, 2022, 1:24pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/1 "2022-07-20T13:24:21Z")

</div>

how do I lookup id\_rsa.pub on a host and then run ansible playbook to copy it to their authorized\_key file?

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 20, 2022, 2:51pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/2 "2022-07-20T14:51:09Z")

</div>

1. [https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file\_lookup.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_lookup.html)  
2. [https://docs.ansible.com/ansible/latest/collections/ansible/posix/authorized\_key\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/posix/authorized_key_module.html)

---

<div class="post-metadata">

**Author:** ![mboncalo](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mboncalo](https://forum.ansible.com/u/mboncalo)\
**Post date:** [July 20, 2022, 5:30pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/3 "2022-07-20T17:30:59Z")

</div>

Just did that, you use authorized\_key module

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 20, 2022, 9:12pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/4 "2022-07-20T21:12:41Z")

</div>

can i use jinja like this?

- name: Setup authkeys for user rke  
authorized\_key:  
user: rke  
state: present  
key: “{{ lookup(‘file’, ‘{{ authorized\_key }}’) }}”

keep getting error

Setup authkeys for user rke] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [k8master]: FAILED! =\> {“msg”: “template error while templating string: unexpected char ‘‘’ at 11. String: “{{ lookup(‘file’, ‘{{ authorized\_key }}’) }}””}  
fatal: [k8node02]: FAILED! =\> {“msg”: “template error while templating string: unexpected char ‘‘’ at 11. String: “{{ lookup(‘file’, ‘{{ authorized\_key }}’) }}””}  
fatal: [k8node01]: FAILED! =\> {“msg”: “template error while templating string: unexpected char ‘‘’ at 11. String: “{{ lookup(‘file’, ‘{{ authorized\_key }}’) }}””}

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 20, 2022, 9:36pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/5 "2022-07-20T21:36:50Z")

</div>

tried this way and got same error

- name: Setup authkeys for user rke  
authorized\_key:  
user: ‘{{ username }}’  
state: present  
key: “{{ lookup(‘file’, ‘/home/{{ username }}/.ssh/id\_rsa.pub’) }}”

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 20, 2022, 9:50pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/6 "2022-07-20T21:50:36Z")

</div>

Mustaches never nest.

key: “{{ lookup(‘file’, ‘/home/’ ~ username ~ ‘/.ssh/id\_rsa.pub’) }}”

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 20, 2022, 10:07pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/7 "2022-07-20T22:07:09Z")

</div>

now different error

TASK [rancherpocreplay : Setup authkeys for user rke] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[WARNING]: Unable to find ‘/home/rke/.ssh/id\_rsa.pub’ in expected paths (use -vvvvv to see paths)  
fatal: [k8master]: FAILED! =\> {“msg”: “An unhandled exception occurred while running the lookup plugin ‘file’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: could not locate file in lookup: /home/rke/.ssh/id\_rsa.pub”}  
[WARNING]: Unable to find ‘/home/rke/.ssh/id\_rsa.pub’ in expected paths (use -vvvvv to see paths)  
fatal: [k8node01]: FAILED! =\> {“msg”: “An unhandled exception occurred while running the lookup plugin ‘file’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: could not locate file in lookup: /home/rke/.ssh/id\_rsa.pub”}  
[WARNING]: Unable to find ‘/home/rke/.ssh/id\_rsa.pub’ in expected paths (use -vvvvv to see paths)  
fatal: [k8node02]: FAILED! =\> {“msg”: “An unhandled exception occurred while running the lookup plugin ‘file’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: could not locate file in lookup: /home/rke/.ssh/id\_rsa.pub”}

but /home/rke/.ssh/id\_rsa.pub is there on the ansible host

---

<div class="post-metadata">

**Author:** ![skratbag](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/skratbag/32/4836_2.png) [@skratbag](https://forum.ansible.com/u/skratbag)\
**Post date:** [July 20, 2022, 10:12pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/8 "2022-07-20T22:12:56Z")

</div>

Usually the .ssh/authorized\_key file has fairly specific permissions (rw user only) as does the .ssh directory. It may well be the ansible user cannot see the files in the .ssh directory as it may not have the correct permissions.

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 20, 2022, 10:23pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/9 "2022-07-20T22:23:57Z")

</div>

But I used become: in my main.yml

Would that have root access?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 20, 2022, 11:31pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/10 "2022-07-20T23:31:42Z")

</div>

It would have root access — on the target machine, but not on the Ansible controller.

---

<div class="post-metadata">

**Author:** ![skratbag](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/skratbag/32/4836_2.png) [@skratbag](https://forum.ansible.com/u/skratbag)\
**Post date:** [July 20, 2022, 11:35pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/11 "2022-07-20T23:35:06Z")

</div>

I didn’t see the main.yml file and can’t comment on your setup. Yes, root should have access however I’m not sure become would carry over all tasks. I thought it was task specific unless you set it in your group vars

[https://docs.ansible.com/ansible/latest/user\_guide/become.html](https://docs.ansible.com/ansible/latest/user_guide/become.html)

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 21, 2022, 12:19pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/12 "2022-07-21T12:19:19Z")

</div>

main.yml

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 21, 2022, 12:32pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/13 "2022-07-21T12:32:04Z")

</div>

how do i access to lookup the id\_rsa.pub file? The user running ansible playbook has sudo rights on the controller

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 21, 2022, 1:09pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/14 "2022-07-21T13:09:13Z")

</div>

[WARNING]: Unable to find ‘/home/rke/.ssh/id\_pub.rsa’ in expected paths (use -vvvvv to see paths)  
fatal: [k8master]: FAILED! =\> {“msg”: “An unhandled exception occurred while running the lookup plugin ‘file’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: could not locate file in lookup: /home/rke/.ssh/id\_pub.rsa”}  
[WARNING]: Unable to find ‘/home/rke/.ssh/id\_pub.rsa’ in expected paths (use -vvvvv to see paths)  
fatal: [k8node01]: FAILED! =\> {“msg”: “An unhandled exception occurred while running the lookup plugin ‘file’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: could not locate file in lookup: /home/rke/.ssh/id\_pub.rsa”}  
[WARNING]: Unable to find ‘/home/rke/.ssh/id\_pub.rsa’ in expected paths (use -vvvvv to see paths)  
fatal: [k8node02]: FAILED! =\> {“msg”: “An unhandled exception occurred while running the lookup plugin ‘file’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: could not locate file in lookup: /home/rke/.ssh/id\_pub.rsa”}

---

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [July 21, 2022, 2:10pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/15 "2022-07-21T14:10:17Z")

</div>

Does /home/rke/.ssh/id\_pub.rsa exist on the host you are running the ansible playbook from? Also, what happens if you try to do a ls on that directory as the user that is executing the ansible playbook, are you getting any errors?

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 21, 2022, 2:32pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/16 "2022-07-21T14:32:25Z")

</div>

yes it does, but the user (ansible) i am running the playbook with even though it has sudo rights and in root group cant access that folder.

i tried to copy the id\_rsa.pub to /tmp and it works

---

<div class="post-metadata">

**Author:** ![jcpetro](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jcpetro](https://forum.ansible.com/u/jcpetro)\
**Post date:** [July 21, 2022, 2:43pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/17 "2022-07-21T14:43:40Z")

</div>

Sounds like you have a local permissions issue.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 21, 2022, 2:48pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/18 "2022-07-21T14:48:03Z")

</div>

> yes it does, but the user (ansible) i am running the playbook with even though it has sudo rights and in root group cant access that folder.

Your authorized\_keys task is run on the remote host, but using the  
lookup/file plugin in one of the arguments doesn't allow for privilege  
escalation locally.  
I think for fetching the materials, you should have an initial  
set\_fact task with delegate\_to=localhost and set become=true on that.

(not verified)

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 21, 2022, 4:42pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/19 "2022-07-21T16:42:16Z")

</div>

do you mean something like this?

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [July 22, 2022, 6:06pm UTC](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877/20 "2022-07-22T18:06:55Z")

</div>

trying to do this another way

- name: copy id\_rsa.pub to tmp for reading on localhost  
ansible.builtin.shell:  
cmd: “{{ command2 }}”  
register: shell\_output  
become: true  
delegate\_to: localhost

where command2 is ‘cp /home/rke/.ssh/id\_rsa.pub /tmp’

I am trying to run this only on the ansible controller (localhost)

but it looks like its trying to run on remote nodes

fatal: [k8node02 → localhost]: FAILED! =\> {“changed”: false, “msg”: “Unsupported parameters for (command) module: cmd Supported parameters include: \_raw\_params, \_uses\_shell, argv, chdir, creates, executable, removes, stdin, stdin\_add\_newline, strip\_empty\_ends, warn”}  
fatal: [k8master → localhost]: FAILED! =\> {“changed”: false, “msg”: “Unsupported parameters for (command) module: cmd Supported parameters include: \_raw\_params, \_uses\_shell, argv, chdir, creates, executable, removes, stdin, stdin\_add\_newline, strip\_empty\_ends, warn”}  
fatal: [k8node01 → localhost]: FAILED! =\> {“changed”: false, “msg”: “Unsupported parameters for (command) module: cmd Supported parameters include: \_raw\_params, \_uses\_shell, argv, chdir, creates, executable, removes, stdin, stdin\_add\_newline, strip\_empty\_ends, warn”}

any idea?

[Next page](https://forum.ansible.com/t/how-do-i-lookup-id-rsa-pub-on-a-host-and-copy-it-to-authorized-keys-on-multiple-servers/36877.md?page=2)
