# Using Ansible authorized\_key, how can I copy the keys from all hosts in a group?

**URL:** <https://forum.ansible.com/t/using-ansible-authorized-key-how-can-i-copy-the-keys-from-all-hosts-in-a-group/24545>\
**Category:** Ansible Project\
**Created:** [May 22, 2017, 8:01pm UTC](https://forum.ansible.com/t/using-ansible-authorized-key-how-can-i-copy-the-keys-from-all-hosts-in-a-group/24545 "2017-05-22T20:01:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_Lawson](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@Bob\_Lawson](https://forum.ansible.com/u/Bob_Lawson)\
**Post date:** [May 22, 2017, 8:01pm UTC](https://forum.ansible.com/t/using-ansible-authorized-key-how-can-i-copy-the-keys-from-all-hosts-in-a-group/24545/1 "2017-05-22T20:01:18Z")

</div>

I’m trying to use Ansible to copy keys from multiple hosts to a remote host user’s authorized\_key file. In my inventory, I have a collectors group and receiver group, as follows:

```auto
[collectors]
c1
c2
c3

[receiver]
r1

```

I want to copy the ssh key from each host in the collectors group to the authorized\_key file of user1 in the receiver group. I know I should user the authorized\_key module, but I’m stuck on how to generate the “key” directive so it contains all the keys from the collectors group.

```auto
- hosts: receiver
  tasks:
    - name: copy collectors group keys to authorized_keys file
      authorized_key:
      user: user1
      key: ????? (not sure what to use here)
      exclusive: yes
 

```

I’m looking for some help to properly set the “key” value, or if there is some completely different and better way to accomplish my goal, please share. Thanks!

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 19, 2017, 11:31pm UTC](https://forum.ansible.com/t/using-ansible-authorized-key-how-can-i-copy-the-keys-from-all-hosts-in-a-group/24545/2 "2017-06-19T23:31:59Z")

</div>

You need use fetch/slurp on each host, then once you have all the keys  
you can pass them to authorized\_keys.
