How to distribute public keys to every server except myself?

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

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…

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
[2] https://github.com/hobby-kube/guide#wireguard-setup

- set_fact:
     public_keys: |
       {% for svr in groups.cluster|difference([inventory_hostname]) %}
       {{ lookup('file', 'keys/' + svr + '.key.public') }} %}
       {% endfor %}

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