I have a playbook that plays different roles on different hosts. Is it possible to transfer a variable from one role running on one host to another role on another host running in the same 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 from the above host/role
The task in role1, which sets the variable db, is as follows:
- shell: cd /ACE/conf && grep ^db.url local1.properties | awk -F/ '{print $4}' | awk -F? '{print $1}'
register: db
UPDATE: on the first host, the values are dynamic, it is like a configuration file that is always updated. After I save the values in variables on host 1 with role1, I then go to host2, run role2 and make stuff with these values from the variables stored in host1.
I tried with hostvars:
{{ hostvars.LBL.db.stdout }}
{{ hostvars['LBL']['db'] }}
{{ hostvars['LBL']['db']['stdout'] }}
:
in get_variables raise Exception("host not found: %s" % hostname) Exception: host not found: LBL
LBL ,
, , . . ?
hostvars :
---
- 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'] }}"
:
fatal: [10.104.148.138] => host not found: LBL
/ ..//
[root@NS1 ansible]# cat /etc/ansible/hosts
[LBL]
10.104.148.136
[LM]
10.104.148.138