# Unable to get value of variable from another Play in Ansible

**URL:** <https://forum.ansible.com/t/unable-to-get-value-of-variable-from-another-play-in-ansible/31104>\
**Category:** Ansible Project\
**Created:** [August 22, 2019, 7:26pm UTC](https://forum.ansible.com/t/unable-to-get-value-of-variable-from-another-play-in-ansible/31104 "2019-08-22T19:26:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [August 22, 2019, 7:26pm UTC](https://forum.ansible.com/t/unable-to-get-value-of-variable-from-another-play-in-ansible/31104/1 "2019-08-22T19:26:00Z")

</div>

I wish to obtain the value of “f\_APP” variable of Play 2 into Play 3.

Below is my playbook which case be used as a testcase:

`

- name: “Play 1”  
hosts: localhost  
tasks:

- add\_host: name={{ item }}  
groups=dest\_nodes  
ansible\_user={{ USER }}  
with\_items: “{{ Dest\_IP.split(‘,’) }}”

- name: “Play 2”  
hosts: dest\_nodes  
user: “{{ USER }}”  
tasks:

- set\_fact:  
f\_APP: “Yellow”

- name: “Play 3”

hosts: localhost  
user: “{{ USER }}”

vars:  
dbfiledet: “{{ hostvars[‘dest\_nodes’][‘f\_APP’] }}”

tasks:

- debug: msg=“{{ dbfiledet.stdout }}”

`

I get the below error for my attempt:

playbook RUN command:

ansible-playbook variabletest.yml -e “USER=user1 Dest\_IP=10.17.44.26,10.17.54.26”

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’

PLAY [Play 1]

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\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17.44.26] ok: [10.17.54.26]

PLAY [Play 3]

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ok: [localhost]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: "hostvars[‘dest\_nodes’]" is undefined\n\nThe error appears to be in ‘variabletest.yml’: line 36, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug: msg="{{ dbfiledet.stdout }}"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with\_items:\n - {{ foo }}\n\nShould be written as:\n\n with\_items:\n - "{{ foo }}"\n”}

PLAY RECAP

10.17.44.26 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

10.17.54.26 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 localhost

: ok=3 changed=1 unreachable=0 failed=1 skipped=0

rescued=0 ignored=0

I’m on the latest version of ansible and python 2.7.5

Can someone suggest what is wrong and how can i get the value for the variable in Play 3 please ?

---

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [August 22, 2019, 7:50pm UTC](https://forum.ansible.com/t/unable-to-get-value-of-variable-from-another-play-in-ansible/31104/2 "2019-08-22T19:50:14Z")

</div>

Unfortunately play variables do not persist in between plays by design in Ansible, generally I would try and group the tasks in a play together but you can write your vars to files on the local Host or remote node in play 2 and then read that file in play 3. One thing I can also suggest although I don’t have a laptop to test currently is:

Play2:  
Hosts: test  
Tasks:  
set\_fact:  
myvar: foo

Play3  
Hosts: test  
Tasks:  
set\_fact:  
myvar: “{{ hostvars[‘test’][‘myvar’] }}”

^ that might work as I believe hostvars once defined will be carried throughout execution

---

<div class="post-metadata">

**Author:** ![Mike\_Eggleston](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Mike\_Eggleston](https://forum.ansible.com/u/Mike_Eggleston)\
**Post date:** [August 22, 2019, 8:57pm UTC](https://forum.ansible.com/t/unable-to-get-value-of-variable-from-another-play-in-ansible/31104/3 "2019-08-22T20:57:47Z")

</div>

I’ve read variables don’t survive across “hosts” lines.

Mike

---

<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:** [August 22, 2019, 9:23pm UTC](https://forum.ansible.com/t/unable-to-get-value-of-variable-from-another-play-in-ansible/31104/4 "2019-08-22T21:23:37Z")

</div>

They do.  
Variables is register to the host and is available in subsequent plays.

But to read variables from another host you always need to use hostvars to access them.

---

<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:** [August 22, 2019, 9:35pm UTC](https://forum.ansible.com/t/unable-to-get-value-of-variable-from-another-play-in-ansible/31104/5 "2019-08-22T21:35:19Z")

</div>

"dest\_nodes" is the group of hosts. "hostvars" requires a host as an index.  
This is the reason of the error:

&nbsp;&nbsp;&nbsp;&nbsp;The error was: "hostvars['dest\_nodes']" is undefined

Cheers,

&nbsp;&nbsp;-vlado
