Hello,
You can use hostvars[hostname] to get facts set by other hosts. play_hosts
array is also useful.
The following playbook collects root's SSH public keys from all hosts, then
distributes keys back to all hosts.
---
- hosts: all
sudo: yes
gather_facts: no
vars:
pubkeys: |
{%- for i in play_hosts %}
{%- set k = hostvars[i].pubkey %}
{%- if k %}
{{ k }}
{% endif %}
{%- endfor %}
tasks:
- command: cat /root/.ssh/id_rsa.pub
register: r
failed_when: false
- set_fact:
pubkey: "{{ r.stdout | default('') }}"
- debug:
var: pubkeys
- authorized_key:
user: root
key: "{{ pubkeys }}"
Possible improvement: it would be nice if this playbook could also
distribute all host keys into /root/.ssh/known_hosts of each host.
On Thursday, September 3, 2015 at 9:13:50 PM UTC+9, Diwaakartg Ganesh wrote:
>
> checking out the documentation, modules like set_fact and register have
> scope only for that specific host. There are many purpose of using the
> variables from one host to another.
>
> Alternatives I can think of:
>
> 1.
>
> using Command module and echoing the variables to a file. Later, using
> the variable file using vars section or include.
> 2.
>
> Setting the env variables and then accessing it but this will be
> difficult.
>
> So what is the solution?
>
Regards,
--
YAEGASHI Takeshi <[email protected]>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/1c0fc55e-b0ba-4935-a6ed-a78a82560bea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.