How to distribute public keys to every server except myself?

32 views
Skip to first unread message

chbndrhnns

unread,
Aug 4, 2017, 6:20:34 AM8/4/17
to Ansible Project
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

Uwe Sauter

unread,
Aug 4, 2017, 6:32:12 AM8/4/17
to ansible...@googlegroups.com
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…
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com
> <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/592b9213-23c3-4b99-ace7-10fb159b4706%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/592b9213-23c3-4b99-ace7-10fb159b4706%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

chbndrhnns

unread,
Aug 4, 2017, 6:39:34 AM8/4/17
to Ansible Project, uwe.sa...@gmail.com
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.



Am Freitag, 4. August 2017 12:32:12 UTC+2 schrieb Uwe Sauter:
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…



Am 04.08.2017 um 12:20 schrieb chbndrhnns:
> 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
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com

Kai Stian Olstad

unread,
Aug 4, 2017, 7:04:31 AM8/4/17
to ansible...@googlegroups.com
- 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.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages