# Passing vars to include: from a playbook (inside tasks:)

**URL:** <https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266>\
**Category:** Ansible Project\
**Created:** [August 7, 2013, 2:55am UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266 "2013-08-07T02:55:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neil\_Clayton](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@Neil\_Clayton](https://forum.ansible.com/u/Neil_Clayton)\
**Post date:** [August 7, 2013, 2:55am UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/1 "2013-08-07T02:55:06Z")

</div>

I’m trying to make a general “mysql populate / dump” script, but using defined by group\_vars/all and possibly overriden by host\_vars.  
These vars exist when the tasks defined within roles run, but do not appear to be defined at the playbook level.

Am I going nuts? Might be a separate question! So, the playbook looks like this:

---

<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:** [August 7, 2013, 6:21pm UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/2 "2013-08-07T18:21:29Z")

</div>

“These vars exist when the tasks defined within roles run, but do not appear to be defined at the playbook level.”

Inventory variables can be different for each of your tasks. It is 100% normal and expected that we can’t print a task header that says x=52 when there are 500 systems  
and some of them are x=11 and some are x=52 and some are x=59. If you do not give a name attribute to a task it will use the action line, so if this concerns you, just add a name  
for your task.

Run with “-v” and you will see what is going on.

---

<div class="post-metadata">

**Author:** ![Neil\_Clayton](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@Neil\_Clayton](https://forum.ansible.com/u/Neil_Clayton)\
**Post date:** [August 7, 2013, 10:01pm UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/3 "2013-08-07T22:01:21Z")

</div>

Sorry, I see I wasn’t very clear.  
What I mean is, the vars such as store\_db\_name exist in **other** playbooks (e.g: common/tasks/main.yml) - but do not exist at the top level.

What I see with the above example is the mysql/tasks/log.yml receives “$db\_name” as it’s variable. That’s the content. Not the name. db\_name is not being set. I hope that’s clearer…

So. I create another playbook, idea being:

1. top level (just a single include) → 2) call mysql task with some vars → 3) reuse the roles/mysql/load.yml definitions.

That too resulted in the same.  
At (3) I still see ‘db\_name’ not as the result of $store\_db\_name but as $db\_name. Does’t appear to be set.  
Incidentally, the name of the task **does** print just fine if I structure things this way, which indicates $store\_db\_name is defined. To me, it appears as if include isn’t resolving existing references. If I set db\_name to a literal, that is fine, that gets passed through. Only existing vars look like they are missed out.

N

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 7, 2013, 10:03pm UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/4 "2013-08-07T22:03:18Z")

</div>

Impossible to help without seeing your playbooks.

Gist perhaps?

---

<div class="post-metadata">

**Author:** ![Neil\_Clayton](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@Neil\_Clayton](https://forum.ansible.com/u/Neil_Clayton)\
**Post date:** [August 7, 2013, 10:14pm UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/5 "2013-08-07T22:14:25Z")

</div>

Funny you should ask. I _just_ went downstairs to do just that 🙂

1. top level : [https://gist.github.com/scornflake/ee800344854402c4bc0a](https://gist.github.com/scornflake/ee800344854402c4bc0a)
2. intermediate: [https://gist.github.com/scornflake/c6839afad79e5e1448ea](https://gist.github.com/scornflake/c6839afad79e5e1448ea)
3. goal: [https://gist.github.com/scornflake/c6839afad79e5e1448ea](https://gist.github.com/scornflake/c6839afad79e5e1448ea)

---

<div class="post-metadata">

**Author:** ![Neil\_Clayton](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@Neil\_Clayton](https://forum.ansible.com/u/Neil_Clayton)\
**Post date:** [August 8, 2013, 12:38am UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/6 "2013-08-08T00:38:21Z")

</div>

Ignore me for a moment. It may be working and I was looking at some bad output (from me).  
Please don’t spent time on this! I’ll post my results, but I think I did something stupid…!

---

<div class="post-metadata">

**Author:** ![Neil\_Clayton](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@Neil\_Clayton](https://forum.ansible.com/u/Neil_Clayton)\
**Post date:** [August 8, 2013, 12:46am UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/7 "2013-08-08T00:46:49Z")

</div>

Turns out I had some mis-spelled var names. It works just fine. Both at top level and if I use secondary.  
Nothing to see here!

Thanks for taking the time.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 8, 2013, 2:34am UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/8 "2013-08-08T02:34:10Z")

</div>

Sure thing, if you are running 1.3 devel code, there’s a nice feature (which we are going to turn on by default soon) that will raise errors when variables are undefined rather than leaving as is untemplated.

I originally thought we should remain compatible here, but honestly, can’t think of many cases where it should be the other way! Further, it’s easy to reconfigure if you want the other behavior.

---

<div class="post-metadata">

**Author:** ![Neil\_Clayton](https://avatars.discourse-cdn.com/v4/letter/n/e95f7d/32.png) [@Neil\_Clayton](https://forum.ansible.com/u/Neil_Clayton)\
**Post date:** [August 9, 2013, 2:37pm UTC](https://forum.ansible.com/t/passing-vars-to-include-from-a-playbook-inside-tasks/13266/9 "2013-08-09T14:37:16Z")

</div>

That sounds like a great idea. Bring it on!
