# passing vars: to included tasks/files

**URL:** <https://forum.ansible.com/t/passing-vars-to-included-tasks-files/12396>\
**Category:** Ansible Project\
**Created:** [February 21, 2013, 4:37am UTC](https://forum.ansible.com/t/passing-vars-to-included-tasks-files/12396 "2013-02-21T04:37:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brice\_Burgess](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@Brice\_Burgess](https://forum.ansible.com/u/Brice_Burgess)\
**Post date:** [February 21, 2013, 4:37am UTC](https://forum.ansible.com/t/passing-vars-to-included-tasks-files/12396/1 "2013-02-21T04:37:19Z")

</div>

Is there a way (or would a patch be welcomed??) to allow cleanly supplying variables to included tasks/playbooks/etc. similar to with\_items? E.g.

tasks:

- include: $task\_dir/leiningen-install.yml
- include: $task\_dir/push-ansible.yml  
vars:  
push\_ansible\_version: devel  
push\_ansible\_dir: ~/src/ansible-devel

– VS. –

tasks:

- include: $task\_dir/leiningen-install.yml
- include: $task\_dir/push-ansible.yml push\_ansible\_version=devel push\_ansible\_dir=~/src/ansible-devel

Also; it seems that variables are not shared when passing playbooks (I load a lot of common variables via a vars\_file, and then include playbooks where I need to re-include the vars\_file). Is this a possibility?

Thanks,

~ Brice

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 21, 2013, 4:40am UTC](https://forum.ansible.com/t/passing-vars-to-included-tasks-files/12396/2 "2013-02-21T04:40:06Z")

</div>

Pretty sure Daniel already made that work, at least for playbook  
includes, at one point.

He'll have to remind me 🙂

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [February 21, 2013, 7:58am UTC](https://forum.ansible.com/t/passing-vars-to-included-tasks-files/12396/3 "2013-02-21T07:58:57Z")

</div>

Michael DeHaan wrote:

> Pretty sure Daniel already made that work, at least for playbook  
> includes, at one point.
> 
> He'll have to remind me 🙂

Yep, vars: works on both task includes and playbook includes. Probably needs  
to get documented though...

> > Is there a way (or would a patch be welcomed??) to allow cleanly  
> > supplying  
> > variables to included tasks/playbooks/etc. similar to with\_items? E.g.
> > 
> > &nbsp;&nbsp;tasks:
> > 
> > &nbsp;&nbsp;- include: $task\_dir/leiningen-install.yml  
> > &nbsp;&nbsp;- include: $task\_dir/push-ansible.yml  
> > &nbsp;&nbsp;&nbsp;&nbsp;vars:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push\_ansible\_version: devel  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push\_ansible\_dir: ~/src/ansible-devel
> > 
> > -- VS. --
> > 
> > &nbsp;&nbsp;tasks:
> > 
> > &nbsp;&nbsp;- include: $task\_dir/leiningen-install.yml  
> > &nbsp;&nbsp;- include: $task\_dir/push-ansible.yml push\_ansible\_version=devel  
> > push\_ansible\_dir=~/src/ansible-devel
> > 
> > Also; it seems that variables are not shared when passing playbooks (I  
> > load  
> > a lot of common variables via a vars\_file, and then include playbooks  
> > where  
> > I need to re-include the vars\_file). Is this a possibility?

No, vars are per play, and there are no global vars. If you aren't using any  
of the vars\_files specialities, you could likely just use group\_vars/all to  
contain your variables, and completely avoid using vars\_files.

Daniel

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [February 21, 2013, 2:46pm UTC](https://forum.ansible.com/t/passing-vars-to-included-tasks-files/12396/4 "2013-02-21T14:46:25Z")

</div>

+1

This would be very useful!
