title: ansible reuse variables defined on another host and another role
body: <p>I want to register a variable on a host and then reused in another host for a different role.</p>
  
  <p>My main.yml</p>
  
  <pre><code>- hosts: icinga
    user: test
  
    tasks:
      - name: generetaing tickets for monitoring hosts
        command: /usr/sbin/icinga2 pki ticket --cn '{{ db01_hostname }}'
        register: pkidb01
  
      - set_fact:
          test: "{{ pkidb01 }}"
  
      - debug: msg="{{ pkidb01.stdout }}"
      - debug: msg="{{ test.stdout }}"
  
  - hosts: dbserver02
    user: test
  
    tasks:
      - debug: msg="{{ hostvars['icinga']['pkidb01']['stdout'] }}"        
      - debug: msg="{{ pkidb01 }}"
      - debug: msg="{{ hostvars['icinga'] }}"
  </code></pre>
  
  <p>The var pkidb01 is registered properly and I can access it with set_fact and also with the normal variable on the same host.
  But on the next host  I can't access them in no way.</p>
  
  <p>Output 1.host:</p>
  
  <pre><code>TASK [generetaing tickets for monitoring hosts] *********************************************************************************************************************************************************************************************
  Friday 23 February 2018  10:23:22 +0100 (0:00:09.593)       0:00:09.620 *******
  changed: [10.10.10.100]
  
  TASK [set_fact] *****************************************************************************************************************************************************************************************************************************
  Friday 23 February 2018  10:23:23 +0100 (0:00:01.521)       0:00:11.142 *******
  ok: [10.10.10.100]
  
  TASK [debug] ********************************************************************************************************************************************************************************************************************************
  Friday 23 February 2018  10:23:23 +0100 (0:00:00.040)       0:00:11.182 *******
  ok: [10.10.10.100] =&gt; {
      "msg": "08fcba18866e563dcded00e43637fec6dbb025e8"
  }
  
  TASK [debug] ********************************************************************************************************************************************************************************************************************************
  Friday 23 February 2018  10:23:24 +0100 (0:00:00.039)       0:00:11.222 *******
  ok: [10.10.10.100] =&gt; {
      "msg": "08fcba18866e563dcded00e43637fec6dbb025e8"
  }
  </code></pre>
  
  <p>Output 2.host:</p>
  
  <pre><code>TASK [debug] ********************************************************************************************************************************************************************************************************************************
  Friday 23 February 2018  10:23:33 +0100 (0:00:09.293)       0:00:20.558 *******
  fatal: [10.10.10.30]: FAILED! =&gt; {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['icinga']\" is undefined\n\nThe error appears to have been in '/main.yml': line 45, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - debug: msg=\"{{ hostvars['icinga']['pkidb01']['stdout'] }}\"\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n\nexception type: &lt;class 'ansible.errors.AnsibleUndefinedVariable'&gt;\nexception: \"hostvars['icinga']\" is undefined"}
  </code></pre>
  
  <p>Hostfile:</p>
  
  <pre><code>[dbserver01]
  10.10.10.30
  
  [icinga]
  10.10.10.100
  </code></pre>
  
  <p>This solutions here didnt worked:
  <a href="https://stackoverflow.com/questions/29368686/pass-ansible-variables-from-one-role-running-on-one-host-to-another-role-runni">Pass Ansible variables from one role (running on one host) to another role running on another host within the same playbook</a></p>