# How to distribute public keys to every server except myself?

**URL:** <https://forum.ansible.com/t/how-to-distribute-public-keys-to-every-server-except-myself/25182>\
**Category:** Ansible Project\
**Created:** [August 4, 2017, 10:20am UTC](https://forum.ansible.com/t/how-to-distribute-public-keys-to-every-server-except-myself/25182 "2017-08-04T10:20:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chbndrhnns](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@chbndrhnns](https://forum.ansible.com/u/chbndrhnns)\
**Post date:** [August 4, 2017, 10:20am UTC](https://forum.ansible.com/t/how-to-distribute-public-keys-to-every-server-except-myself/25182/1 "2017-08-04T10:20:34Z")

</div>

Hey there,

I have a setup with host1, host2, host3. I need to read a file into a variable per server, pass it to a Jinja2 template and fill in values. Each host should only get the values from the other two ones. I have no good idea how to achieve this with Jinja2 syntax. Is somebody willing to help?  
My code looks like this now. The do keywords seems not to be supported by Ansible

- set\_fact:  
public\_keys: \>-  
{% set me = inventory\_hostname.split(‘host’)[1] | int %}  
{% for svr in groups.cluster|difference([me]) %}  
{% do public\_keys[svr] = {{ lookup(‘file’, ‘keys/’ + svr + ‚.key.public’) }} %}  
{% endfor %}

Bye,  
Jo

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [August 4, 2017, 10:31am UTC](https://forum.ansible.com/t/how-to-distribute-public-keys-to-every-server-except-myself/25182/2 "2017-08-04T10:31:48Z")

</div>

Just to question the obvious: this is not the use case to distribute public keys into ~/.ssh/authorized\_keys?

If it is, use the "authorized\_key" module…

---

<div class="post-metadata">

**Author:** ![chbndrhnns](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@chbndrhnns](https://forum.ansible.com/u/chbndrhnns)\
**Post date:** [August 4, 2017, 10:39am UTC](https://forum.ansible.com/t/how-to-distribute-public-keys-to-every-server-except-myself/25182/3 "2017-08-04T10:39:34Z")

</div>

No, I am following [1] to script a kubernetes installation which requires me to follow the steps described in [2].

There is no connection the `authorized_keys` file.

[1] [https://github.com/hobby-kube/guide](https://github.com/hobby-kube/guide)  
[2] [https://github.com/hobby-kube/guide#wireguard-setup](https://github.com/hobby-kube/guide#wireguard-setup)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 4, 2017, 11:04am UTC](https://forum.ansible.com/t/how-to-distribute-public-keys-to-every-server-except-myself/25182/4 "2017-08-04T11:04:09Z")

</div>

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public\_keys: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for svr in groups.cluster|difference([inventory\_hostname]) %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('file', 'keys/' + svr + '.key.public') }} %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

Loop like this should do what you want.  
But you might have som newline challenges to sort out.
