title: Pass Ansible variables from one role (running on one host) to another role running on another host within the same playbook
body: <p>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 ?</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The task in role1 that sets teh variable <code>db</code> looks like this:</p>
  
  <pre><code>- shell: cd /ACE/conf &amp;&amp; grep ^db.url local1.properties | awk -F/ '{print $4}' | awk -F? '{print $1}'
    register: db
  </code></pre>
  
  <p>UPDATE: On the first host the values are dynamic, it's like a configuration file that is always updated. After I store the values in variables on host1 with the role1 I then move to the host2, run the role2 and do stuff with those values from variables stored by host1. </p>
  
  <p>I tried with hostvars:</p>
  
  <pre><code>{{ hostvars.LBL.db.stdout }}
  {{ hostvars['LBL']['db'] }}
  {{ hostvars['LBL']['db']['stdout'] }}
  </code></pre>
  
  <p>and I get error:</p>
  
  <pre><code>in get_variables raise Exception("host not found: %s" % hostname) Exception: host not found: LBL
  </code></pre>
  
  <p>LBL exists in hosts as on it I run the first role</p>
  
  <p>I set a variable on one host and I want that variable to be available to the other host. All this within a single playbook. Can it be done ?</p>
  
  <p>hostvars is not working using it like this:</p>
  
  <pre><code>---   
  - 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'] }}"
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>fatal: [10.104.148.138] =&gt; host not found: LBL
  </code></pre>
  
  <p>/etc/ansible/hosts</p>
  
  <pre><code>[root@NS1 ansible]# cat /etc/ansible/hosts
  [LBL]
  10.104.148.136
  [LM]
  10.104.148.138
  </code></pre>