title: How to use variables between different roles in ansible
body: <p>my playbook structure looks like:</p>
  
  <pre><code>- hosts: all
    name: all
    roles:
    - roles1
    - roles2
  </code></pre>
  
  <p>In <code>tasks</code> of <code>roles1</code>, I define such a variable</p>
  
  <pre><code>   ---
     # tasks for roles1
     - name: Get the zookeeper image tag # rel3.0
       run_once: true
       shell: echo '{{item.split(":")[-1]}}' # Here can get the string rel3.0 normally
       with_items: "{{ret.stdout.split('\n')}}"
       when: "'zookeeper' in item"
       register: zk_tag
  </code></pre>
  
  <p>ret.stdout：</p>
  
  <pre><code>Loaded image: test/old/kafka:latest
  Loaded image: test/new/mysql:v5.7
  Loaded image: test/old/zookeeper:rel3.0
  </code></pre>
  
  <p>In <code>tasks</code> of <code>roles2</code>, I want to use the <code>zk_tag</code> variable</p>
  
  <pre><code>- name: Test if the variable zk_tag can be used in roles2
    debug: var={{ zk_tag.stdout }}
  </code></pre>
  
  <p>Error :</p>
  
  <pre><code>The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'
  </code></pre>
  
  <p>I think I encountered the following 2 problems:</p>
  
  <ol>
  <li><p>When registering a variable with register, when condition is added, this variable cannot be used in all groups. How to solve this problem? How to make this variable available to all groups?</p></li>
  <li><p>is my title, How to use variables between different roles in ansible?</p></li>
  </ol>