Share variables between roles running on different hosts

Hello all,

I have a playbook that runs different roles on different hosts.
Is it possible to pass a variable from one role running on one host to another role on another host running within the same playbook run? Or any workaround ?

playbook
host1
role1
here I get some variables: var1 var2 …etc
host2
role2
here I need to use var1 var2 … etc.

Looks like you're in need of some magic.

http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts

Giovanni

Hi,

I tried with hostvars:

{{ hostvars.LBL.db.stdout }};"
{{ hostvars[‘LBL’][‘db’] }}
{{ hostvars[‘LBL’][‘db’][‘stdout’] }}

but it says:

in get_variables raise Exception(“host not found: %s” % hostname) Exception: host not found: LBL
And LBL is defined, after all I’m running the first role on LBL and it’s ok.
I’m using: ansible-1.8.4-1.el6.noarch

Working example:

I modified it to suit my hosts:

Tested on 1.8.4 and 1.9.0.1

Giovanni

I modified it to suit my hosts:

---
- name: test hostvars host1
  hosts: LBL
  tasks:
    - command: "ls /bin"
      register: ls_out

- name: test hostvars host2
  hosts: LM
  tasks:
    - debug:
        var: "{{ hostvars['LBL']['ls_out']['stdout'] }}"

and it doesn't work:

<snip>

FATAL: all hosts have already failed -- aborting

the ls /bin is been executed but it's the same error
this is the content of my hosts file:

[LBL]
10.104.148.136
[LM]
10.104.148.138

LBL is the name of the group, not the host. You'd want something like

  var: "{{ hostvars['10.104.148.136']['ls_out']['stdout'] }}"

You can also get the list of hosts in a group, using {{ groups['LBL'] }}, if you'd rather just get the first host from there or if you want to iterate over the entire group.

There's more info and examples about this on the link posted earlier in this thread: http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts

-A