# include file through variable.

**URL:** <https://forum.ansible.com/t/include-file-through-variable/15787>\
**Category:** Ansible Project\
**Created:** [July 27, 2014, 9:08pm UTC](https://forum.ansible.com/t/include-file-through-variable/15787 "2014-07-27T21:08:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [July 27, 2014, 9:08pm UTC](https://forum.ansible.com/t/include-file-through-variable/15787/1 "2014-07-27T21:08:20Z")

</div>

Hi,

I just updated to the latest and greatest revision and since the update I can no longer perform an include of a file defined through a variable.

For instance:

- include: “{{ file }}.yml”  
when: file is defined

now throws the following error:

ERROR: file could not read: /path/to/file/“file”.yml

(note the double quotes that are added around ‘file’).

-Nico.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 28, 2014, 2:04pm UTC](https://forum.ansible.com/t/include-file-through-variable/15787/2 "2014-07-28T14:04:50Z")

</div>

So this still works and we know because we have an integration test for it, but what you have with the “when: file is defined” has NEVER worked.

What happens with an include is the conditional is applied to all tasks in the include, it _cannot_ be used to decide to include something or not, because the same tasks (with different variable contexts of course) get applied to all hosts.

---

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [July 29, 2014, 3:55am UTC](https://forum.ansible.com/t/include-file-through-variable/15787/3 "2014-07-29T03:55:06Z")

</div>

Hi Michael,

It’s funny you say that, since I’ve been running this playbook for a while and ever since the update it started failing; it might very well be that the ‘when’ never did what I expected it to do though.

However, there is a clear change in behavior, further investigation shows that previously:

roles/x/tasks/main.yml:

- include: …/…/y/tasks/main.yml name=“sometask”

roles/y/tasks/main.yml

- include: “{{ name }}.yml”

would evaluate to “sometask.yml”, however now it evaluates to “sometask”.yml (Mind the quoting)  
When you remove the quotes around variable passed in (name=sometask) it does seem to work again.

-N.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 29, 2014, 12:54pm UTC](https://forum.ansible.com/t/include-file-through-variable/15787/4 "2014-07-29T12:54:59Z")

</div>

This is already fixed on devel and we’re looking at releasing 1.7 in a couple of weeks.

[https://github.com/ansible/ansible/commit/6e814566de36558a52bcb19ab4231e572cf5a63b](https://github.com/ansible/ansible/commit/6e814566de36558a52bcb19ab4231e572cf5a63b)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 29, 2014, 12:55pm UTC](https://forum.ansible.com/t/include-file-through-variable/15787/5 "2014-07-29T12:55:24Z")

</div>

The underlying problem is that parameters quoted to an “include” line were keeping quotes, which the above fixes.
