title: Access variable from one role in another role in an Ansible playbook with multiple hosts
body: <p>I'm using the latest version of Ansible, and I am trying to use a default variable in <code>role-one</code> used on host <code>one</code>, in <code>role-two</code>, used on host <code>two</code>, but I can't get it to work.</p>
  <p>Nothing I have found in the documentation or on StackOverflow has really helped. I'm not sure what I am doing wrong. Ideally I want to set the value of the variable once, and be able to use it in another role for any host in my playbook.</p>
  <p>I've broken it down below.</p>
  <hr />
  <p>In my inventory I have a hosts group called <code>[test]</code> which has two hosts aliased as <code>one</code> and <code>two</code>.</p>
  <pre><code>[test]
  one ansible_host=10.0.1.10 ansible_connection=ssh ansible_user=centos ansible_ssh_private_key_file=&lt;path_to_key&gt;
  two ansible_host=10.0.1.20 ansible_connection=ssh ansible_user=centos ansible_ssh_private_key_file=&lt;path_to_key&gt;
  </code></pre>
  <p>I have a <em>single playbook</em> with a <em>play for each of these hosts</em> and I supply the <code>hosts:</code> value as <code>&quot;{{ host_group }}[0]&quot;</code> for host <code>one</code> and <code>&quot;{{ host_group }}[1]&quot;</code> for host <code>two</code>.</p>
  <p>The play for host <code>one</code> uses a role called <code>role-one</code> and the play for host <code>two</code> uses a role called <code>role-two</code>.</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: Test Sharing Role Variables
    hosts: &quot;{{ host_group }}[0]&quot;
    roles:
      - ../../ansible-roles/role-one
  
  - name: Test Sharing Role Variables
    hosts: &quot;{{ host_group }}[1]&quot;
    roles:
      - ../../ansible-roles/role-two  
  </code></pre>
  <p>In <code>role-one</code> I have set a variable <code>variable-one</code>.</p>
  <pre class="lang-yaml prettyprint-override"><code>---
  # defaults file for role-one
  
  variable_one: Role One Variable
  </code></pre>
  <p>I want to use the value of <code>variable_one</code> in a template in <code>role-two</code> but I haven't had any luck. I'm using the below as a task in <code>role-two</code> to test and see if the variable is getting &quot;picked-up&quot;.</p>
  <pre class="lang-yaml prettyprint-override"><code>---
  # tasks file for role-two
  - debug:
      msg: &quot;{{ variable_one }}&quot;
  </code></pre>
  <p>When I run the playbook with <code>ansible-playbook test.yml --extra-vars &quot;host_group=test&quot;</code> I get the below failure.</p>
  <pre class="lang-sh prettyprint-override"><code>TASK [../../ansible-roles/role-two : debug] ***********************************************************************************************************************************************************************************************
  fatal: [two]: FAILED! =&gt; {&quot;msg&quot;: &quot;The task includes an option with an undefined variable. The error was: \&quot;hostvars['test']\&quot; is undefined\n\nThe error appears to be in 'ansible-roles/role-two/tasks/main.yml': line 3, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# tasks file for role-two\n- debug:\n  ^ here\n&quot;}
  </code></pre>