# How to use variables from one task in another task

**URL:** <https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321>\
**Category:** Ansible Project\
**Created:** [September 20, 2019, 7:37am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321 "2019-09-20T07:37:20Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [September 20, 2019, 7:37am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/1 "2019-09-20T07:37:20Z")

</div>

Hello Ansible Gurus,

I am new to Ansible and trying to get my hands dirty on playbooks. I wrote an yaml file to use a variable from one host tasks to another. Here is my code ( logically this code may not make sense in real world, I am trying to understand features in Ansible )

---

<div class="post-metadata">

**Author:** ![Spiro\_Mitsialis](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Spiro\_Mitsialis](https://forum.ansible.com/u/Spiro_Mitsialis)\
**Post date:** [September 20, 2019, 6:19pm UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/2 "2019-09-20T18:19:29Z")

</div>

May not be exactly what you are looking for but you can have a new task with same variables but you use delegate\_to another host.

- name: “create a file in the directory”  
file:  
path: “/home/ansible\_user/from\_controller4/file\_name”  
state: touch  
when: result is succeeded

delegate\_to: another\_host

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 20, 2019, 10:09pm UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/3 "2019-09-20T22:09:02Z")

</div>

No. But you can use hostvars to lookup variable for other hosts.

---

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [September 30, 2019, 2:33pm UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/4 "2019-09-30T14:33:25Z")

</div>

Hi Kai,

I have been trying various ways to get my playbook working by using hostvars.

Here are some of the ways I have tied, with no luck

Trial1 : when: {{ hostvars['hostname1][‘ansible\_facts’][‘distribution’]}} == “CentOS”

Trial2: when: ansible\_facts[‘hostname1’][‘distribution’] == “CentOS”

Can you help me with this?

Thanks  
Srini.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 30, 2019, 4:18pm UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/5 "2019-09-30T16:18:01Z")

</div>

Try this one

&nbsp;&nbsp;when: "hostvars['hostname1']['ansible\_facts']['distribution'] == 'CentOS'"

---

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [October 1, 2019, 7:11am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/6 "2019-10-01T07:11:22Z")

</div>

Hi Vladimir,

I have tried with your suggestion and got following error.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 1, 2019, 7:25am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/7 "2019-10-01T07:25:28Z")

</div>

> \> \> \*Trial1\* :  
> \> \> when: {{hostvars['hostname1]['ansible\_facts']['distribution']}} ==  
> \> \> "CentOS"  
> \>  
> \> Try this one  
> \> when: "hostvars['hostname1']['ansible\_facts']['distribution'] ==  
> \> 'CentOS'"

> I have tried with your suggestion and got following error.  
> fatal: [172.19.251.173]: FAILED! =\> {"msg": "The conditional check  
> 'hostvars['hostname1']['ansible\_facts']['distribution'] == 'CentOS''  
> failed. The error was: error while evaluating conditional  
> (hostvars['hostname1']['ansible\_facts']['distribution'] == 'CentOS'):  
> \"hostvars['hostname1']\" is undefined\n\n

Don't use undefined hostnames.

---

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [October 1, 2019, 9:14am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/8 "2019-10-01T09:14:59Z")

</div>

Hi Vladimir,

hostname1 is defined in my inventory file. Here is the yml file snippet

- hosts: hostname2  
gather\_facts: true  
tasks:

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 1, 2019, 11:30am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/9 "2019-10-01T11:30:20Z")

</div>

> \> \> \> \> \*Trial1\* :  
> \> \> \> \> when: {{hostvars['hostname1]['ansible\_facts']['distribution']}} ==  
> \> \> \> \> "CentOS"  
> \> \> \>  
> \> \> \> Try this one  
> \> \> \> when: "hostvars['hostname1']['ansible\_facts']['distribution'] ==  
> \> \> \> 'CentOS'"  
> \>  
> \> \> I have tried with your suggestion and got following error.  
> \> \> fatal: [172.19.251.173]: FAILED! =\> {"msg": "The conditional check  
> \> \> 'hostvars['hostname1']['ansible\_facts']['distribution'] == 'CentOS''  
> \> \> failed. The error was: error while evaluating conditional  
> \> \> (hostvars['hostname1']['ansible\_facts']['distribution'] == 'CentOS'):  
> \> \> \"hostvars['hostname1']\" is undefined\n\n

> \> Don't use undefined hostnames.

> hostname1 is defined in my inventory file. Here is the yml file snippet
> 
> - hosts: hostname2  
> &nbsp;&nbsp;gather\_facts: true  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;-  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "/home/ansible\_user/controller3/file\_name"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: touch  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "hostvars['hostname1']['ansible\_facts']['distribution'] ==  
> 'CentOS'"  
> When I comment'when'this code runs fine. Please suggest.

The playbook is running at 'hostname2' only and knows nothing about  
'hostname1' at all. You might want to take a look at

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: hostvars

Cheers,

&nbsp;&nbsp;- vlado

---

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [October 1, 2019, 1:09pm UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/10 "2019-10-01T13:09:54Z")

</div>

Hello

- debug:  
var: hostvars

shows all the hosts in my inventory file, so I believe my playbook has access to all hosts. To hash out issue with inventory file, I modified my code as below.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 1, 2019, 2:11pm UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/11 "2019-10-01T14:11:08Z")

</div>

Sure. Attach the tarball with the output of "- debug: var=hostvars"

---

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [October 1, 2019, 2:50pm UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/12 "2019-10-01T14:50:57Z")

</div>

Sure. Attached output of hostvars.

> **(attachments)**
>
> [hostvars.log](https://forum.ansible.com/uploads/short-url/AgL5kkbsDeWMDfHVkYUigeCVcwl.log) (4.02 KB)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 1, 2019, 5:33pm UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/13 "2019-10-01T17:33:59Z")

</div>

The distribution variable is a gather facts variable, and when you run against hostname2 only that variable is not accessible because Ansible has not state between playbook run.

One solution would be to use fact caching, or the easiest, just run gather facts on hostname1 first in the same playbook by adding following code before you play above.

- hosts: hostname1  
&nbsp;&nbsp;father\_facts: true

---

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [October 3, 2019, 10:33am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/14 "2019-10-03T10:33:22Z")

</div>

Kai,

I don’t think it an issue with gather facts. Take a look at below code.

- hosts: **hostname2**  
tasks:

---

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [October 4, 2019, 6:18am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/15 "2019-10-04T06:18:39Z")

</div>

Hello Ansible Gurus,

Any inputs for me to resolve this issue.

---

<div class="post-metadata">

**Author:** ![Miroslav\_Windhouwer](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Miroslav\_Windhouwer](https://forum.ansible.com/u/Miroslav_Windhouwer)\
**Post date:** [October 4, 2019, 6:34am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/16 "2019-10-04T06:34:24Z")

</div>

If I understand you correctly than try to do this. Make a folder host\_var in your inventory folder. Than make a file with the exact same name as your host inside it,

something like: inventory/host\_var/host1.yaml

Inside that file you'll put the vars and those you can you use global for that host.

---

<div class="post-metadata">

**Author:** ![Srinivas\_Naram](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Srinivas\_Naram](https://forum.ansible.com/u/Srinivas_Naram)\
**Post date:** [October 7, 2019, 11:55am UTC](https://forum.ansible.com/t/how-to-use-variables-from-one-task-in-another-task/31321/17 "2019-10-07T11:55:47Z")

</div>

Hi All,

Thanks for your time and efforts to help me out with the issue. I understood the mistak. I missed out making changes in the config file as described in below link

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#caching-facts](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#caching-facts)

TA
