title: Ansible populate variable for global use
body: <p>Im having Ansible variable hell this week. I need to populate a variable dynamically during runtime, then it needs to be available for multiple tasks.</p>
  <p>ie <em>myplaybook.yml</em></p>
  <pre><code>---
  - hosts: Healthcheck_Host 
    gather_facts: no
    become: &quot;{{ True if deploy_user != '{{ deploy_user }}' else False }}&quot;
    become_user: &quot;{{ deploy_user }}&quot;
    tasks:
  
      - name: &quot;Get latest installed CL on groups['Healthcheck_Host'][0]&quot;
        shell: |
          grep -oP '(?&lt;=\:)(.*?)(?=\-)' {{ latest_deployed_build_dir.stdout }}/thebuildinfo.txt
        register: latest_stable_cl
  
      - debug:
          var: latest_stable_cl.stdout
  
      - name: Assign CL value from HC host
        set_fact:
          stable_cl_to_deploy: &quot;{{ latest_stable_cl.stdout }}&quot;
          cacheable: yes
  
  - hosts: Appserver
    gather_facts: no
    become: &quot;{{ True if deploy_user != '{{ deploy_user }}' else False }}&quot;
    become_user: &quot;{{ deploy_user }}&quot;
    tasks:
  
      - debug:
          var: stable_cl_to_deploy
  </code></pre>
  <p>In /etc/ansible/ansible.cfg I do have this set: <strong>fact_caching = memory</strong></p>
  <p>The debug output returns the value as expected from the 1st block, but once the second block is executed I am getting this error:</p>
  <pre><code>&quot;msg&quot;: &quot;The task includes an option with an undefined variable. The error was: 'stable_cl_to_deploy' is undefined\n\nThe error appears to have been in '/some/path/here/to/my/myplaybook.yml': line 30, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Set variables for tools package if deploying stable CL\n  ^ here\n\nexception type: &lt;class 'ansible.errors.AnsibleUndefinedVariable'&gt;\nexception: 'stable_cl_to_deploy' is undefined&quot;
  </code></pre>
  <p>Any help appreciated. Thanks</p>