title: How to access task variables between the tasks in Ansible?
body: <p>I have a simple playbook like bellow which consists of a few tasks. On top of a playbook, I am creating the YAML file from provided CSV file, but if I want to define a variable within the task, this variable is not defined in another task. So what is visibility or/and scope of Ansible variables? I know if I define the variable within the hosts section that variable will persists in all tasks, but as I will define variable dynamically this is not a good approach. </p>
  
  <pre><code>- name: Create IAM users
    vars:
      password: Sometestpassword123
    iam:
      iam_type: user
      name: "{{ item.username }}"
      password: "{{ password }}"
      access_key_state: create
      state: present
      groups: "{{ item.group }}"
  
    with_items:
      - "{{ users.users }}"
    register: user
  
  - name: Create a file with user credentials
    local_action: template src=template.j2 dest=./users_credentials.csv
  </code></pre>
  
  <p>So, if I am defining the password within the task, this variable is used as a module parameter, but I can't reuse this variable in the second task, where I want to concatenate the first task filtered output in a registered variable with the defined variable, which will be dynamically changed in each iteration of loop. </p>
  
  <pre><code> TASK [Create a file with user credentials] *********************************************************************************************************************************************************************
      fatal: [localhost -&gt; localhost]: FAILED! =&gt; {"changed": false, "msg": "AnsibleUndefinedVariable: 'password' is undefined"}
  </code></pre>
  
  <p>Basically, I want to create the CSV file consisting of filtered iam module return values saved in registered variable 'user' and dynamically calculated value of 'password' variable. </p>
  
  <p>Unfortunately, the AWS iam module doesn't return the password. </p>
  
  <p>The template is really simple, as for testing purpose I just want a value of the 'password' value defined in a task. Here it goes. </p>
  
  <pre><code>{{ password }}
  </code></pre>