title: How to use return values of one task in another task for a different host in ansible
body: <p>I was trying to setup mysql master slave replication with ansible for a hostgroup consisting of 2 mysql hosts.</p>
  
  <p>Here is my scenario:</p>
  
  <p>I run one task in the 1st host and skips the 2nd host, so the 1st task (i.e master replication status) returns some values like Position, File etc. </p>
  
  <p>Then, I run another task in 2nd host (skips the 1st hosts), This task uses the return values of the 1st task like master.Position, master.File etc. </p>
  
  <p>Now, when I run the playbook, the variables of the 1st task does not seem to be working in the 2nd task</p>
  
  <p><strong>Inventory File</strong></p>
  
  <pre><code>    [mysql]
      stagmysql01 ansible_host=1.1.1.1 ansible_ssh_user=ansible ansible_connection=ssh
      stagmysql02 ansible_host=1.1.1.2 ansible_ssh_user=ansible ansible_connection=ssh 
  </code></pre>
  
  <p><strong>Tasks on Master</strong></p>
  
  <pre><code>- name: Mysql - Check master replication status.
    mysql_replication: mode=getmaster
    register: master
  
  - debug: var=master
  </code></pre>
  
  <p><strong>Tasks on Slave</strong></p>
  
  <pre><code>- name: Mysql - Configure replication on the slave.
    mysql_replication:
      mode: changemaster
      master_host: "{{ replication_master }}"
      master_user: "{{ replication_user }}"
      master_password: "{{ replication_pass }}"
      master_log_file: "{{ master.File }}"
      master_log_pos: "{{ master.Position }}"
    ignore_errors: True
  </code></pre>
  
  <p><strong>Master Output</strong></p>
  
  <pre><code>TASK [Mysql_Base : Mysql - Check master replication status.] ****************
  skipping: [stagmysql02]
  ok: [stagmysql01]
  
  TASK [Mysql_Base : debug] ***************************************************
  ok: [stagmysql01] =&gt; {
      "master": {
          "Binlog_Do_DB": "", 
          "Binlog_Ignore_DB": "mysql,performance_schema", 
          "Executed_Gtid_Set": "", 
          "File": "mysql-bin.000003", 
          "Is_Master": true, 
          "Position": 64687163, 
          "changed": false, 
          "failed": false
      }
  }
  ok: [stagmysql02] =&gt; {
      "master": {
          "changed": false, 
          "skip_reason": "Conditional result was False", 
          "skipped": true
      }
  }
  </code></pre>
  
  <p><strong>Slave Output</strong></p>
  
  <pre><code>TASK [Mysql_Base : Mysql - Configure replication on the slave.] *************
  skipping: [stagmysql01]
  fatal: [stagmysql02]: FAILED! =&gt; {"failed": true, "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'File'\n\nThe error appears to have been in '/root/ansible/roles/Mysql_Base/tasks/replication.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: Mysql - Configure replication on the slave.\n  ^ here\n\nexception type: &lt;class 'ansible.errors.AnsibleUndefinedVariable'&gt;\nexception: 'dict object' has no attribute 'File'"}
  ...ignoring
  </code></pre>
  
  <p>As, you can see above, the 2nd task failed for 2nd host because of undefined variables. However the required variables are there in 1st task of 1st host.</p>
  
  <p>How do I use the variables returned from 1st host in 2nd host in another task ?</p>
  
  <p>P.S: I have seen the approach of using {{ hostvars['inventory_hostname']['variable'] }}.
  However I'm quite confused with this approach as the inventory_hostname or IP address needs to be added directly. I was looking for a common template that can be used for different inventory files and playbooks.</p>