# Is there a way to access a play's vars section?

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-access-a-plays-vars-section/28997>\
**Category:** Ansible Project\
**Created:** [November 9, 2018, 11:59pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-a-plays-vars-section/28997 "2018-11-09T23:59:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Kakouros](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@Nick\_Kakouros](https://forum.ansible.com/u/Nick_Kakouros)\
**Post date:** [November 9, 2018, 11:59pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-a-plays-vars-section/28997/1 "2018-11-09T23:59:16Z")

</div>

Given a play, I would like to reference somehow its vars section. I would like to achieve any of the two:

- The next play to have access to the vars of the previous play like “{{ prev\_play\_vars }}”
- The vars section of each play to be available to the rest of the plays via sth like “{{play\_name\_here\_vars}}”

I tried messing around with a custom callback plugin but that didn’t get me anywhere.

How could I achieve sth like that?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 10, 2018, 1:00am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-a-plays-vars-section/28997/2 "2018-11-10T01:00:24Z")

</div>

You cannot, play vars are only in scope for that play. The only vars  
in a playbook you can reference across all plays are host vars.

---

<div class="post-metadata">

**Author:** ![Nick\_Kakouros](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@Nick\_Kakouros](https://forum.ansible.com/u/Nick_Kakouros)\
**Post date:** [November 10, 2018, 1:50pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-a-plays-vars-section/28997/3 "2018-11-10T13:50:04Z")

</div>

Thanks. In that case, could I add a task to a play programmatically? I have around 20 plays and I try to have the vars section of each play to be saved locally to a yaml file. Right now, at the end of each play, I use a ‘copy’ tasks that reads the vars via a yaml alias. I would like to not repeat myself. I tried to place this task in a different play that is run after each one of the above plays , but ansible complains about an undefined alias. Do you see any way to not have to repeat the task in every play?

---

<div class="post-metadata">

**Author:** ![Nick\_Kakouros](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@Nick\_Kakouros](https://forum.ansible.com/u/Nick_Kakouros)\
**Post date:** [November 10, 2018, 1:56pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-a-plays-vars-section/28997/4 "2018-11-10T13:56:45Z")

</div>

I forgot to mention that `import_tasks` or `include_tasks` also results in the undefined alias error.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 12, 2018, 2:25pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-a-plays-vars-section/28997/5 "2018-11-12T14:25:50Z")

</div>

To save vars to a yaml file i would just use a template task, unsure  
what you are doing or why, but this seems wrong to me.

On the import\_tasks, include\_tasks .. no clue what 'undefined alias'  
is nor can i reproduce it, you need to give a full error with -vvv  
(which will include traceback)
