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:
using Command module and echoing the variables to a file. Later, using the variable file using vars section or include.
Setting the env variables and then accessing it but this will be difficult.
So what is the solution?
---
- 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 }}"