# handlers and include files with variables in filenames

**URL:** <https://forum.ansible.com/t/handlers-and-include-files-with-variables-in-filenames/24679>\
**Category:** Ansible Project\
**Created:** [June 8, 2017, 10:20am UTC](https://forum.ansible.com/t/handlers-and-include-files-with-variables-in-filenames/24679 "2017-06-08T10:20:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [June 8, 2017, 10:20am UTC](https://forum.ansible.com/t/handlers-and-include-files-with-variables-in-filenames/24679/1 "2017-06-08T10:20:41Z")

</div>

So I am running ansible 2.3.0.0. What I want to do is to have my  
handlers/main.yml file to include another file (where some handlers  
are defined) whose name depends on a variable defined somewhere else.  
i.e.

- include: stuff"{{ myvariable }}".yml

where myvariable is defined in the file group\_vars/all. I also have

[defaults]  
handler\_includes\_static = True

defined in my ansible.cfg file per  
[https://github.com/ansible/ansible/issues/13485](https://github.com/ansible/ansible/issues/13485). But when I run it I  
get the following message:

ERROR! Error when evaluating variable in include name: stuff"{{  
myvariable }}".yml.

When using static includes, ensure that any variables used in their  
names are defined in  
vars/vars\_files or extra-vars passed in from the command line. Static  
includes cannot use  
variables from inventory sources like group or host vars.

Where does it want me to define myvariable then? Somewhere in, say,  
roles/role\_in\_question/vars/main.yml?

---

<div class="post-metadata">

**Author:** ![Jeff\_Li](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@Jeff\_Li](https://forum.ansible.com/u/Jeff_Li)\
**Post date:** [June 23, 2017, 7:13am UTC](https://forum.ansible.com/t/handlers-and-include-files-with-variables-in-filenames/24679/2 "2017-06-23T07:13:12Z")

</div>

I solved the problem by using vars\_files directive in playbook. The directory structure looks like

`

playbook.yml  
vars/main.yml  
roles/my-roles/…  
`

The playbook.yml looks like

`

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [July 6, 2017, 3:00am UTC](https://forum.ansible.com/t/handlers-and-include-files-with-variables-in-filenames/24679/3 "2017-07-06T03:00:06Z")

</div>

> I solved the problem by using vars\_files directive in playbook. The directory structure looks like
> 
> playbook.yml  
> vars/main.yml  
> roles/my-roles/...
> 
> The playbook.yml looks like
> 
> ---  
> &nbsp;&nbsp;hosts: all  
> &nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;&nbsp;&nbsp;- vars/main.yml  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;....  
> &nbsp;&nbsp;roles:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I hate to say but I still do not know how are you selecting  
which handlers to use.

---

<div class="post-metadata">

**Author:** ![Jeff\_Li](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@Jeff\_Li](https://forum.ansible.com/u/Jeff_Li)\
**Post date:** [July 6, 2017, 12:59pm UTC](https://forum.ansible.com/t/handlers-and-include-files-with-variables-in-filenames/24679/4 "2017-07-06T12:59:58Z")

</div>

Hope it is clear.

The directory structure looks like this  
`

playbook.yml  
vars/main.yml  
roles/my-roles/handlers/main.yml  
roles/my-roles/handlers/ocata.yml  
roles/my-roles/handlers/kilo.yml

`

The **roles/my-roles/handlers/main.yml** looks like this

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [July 8, 2017, 11:11am UTC](https://forum.ansible.com/t/handlers-and-include-files-with-variables-in-filenames/24679/5 "2017-07-08T11:11:12Z")

</div>

That looks very similar to what I am doing (test.yml is my playbook):

raub@desktop:~/dev/ansible$ cat test.yml

---

<div class="post-metadata">

**Author:** ![Jeff\_Li](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@Jeff\_Li](https://forum.ansible.com/u/Jeff_Li)\
**Post date:** [July 18, 2017, 12:23am UTC](https://forum.ansible.com/t/handlers-and-include-files-with-variables-in-filenames/24679/6 "2017-07-18T00:23:06Z")

</div>

Filename variables in static inclusion could only be defined in 3 places , any one is ok.

1. extra vars from command line
2. in _vars_ section of the playbook, not the _vars_ or _defaults_ directory of the role
3. in _vars\_file_ section of the playbook

I don’t see non used from your description.

I have published[a repo in github](https://github.com/rhops/ansible-static-include-demo) to show the usage of all the ways to define the filename variables. Basically, you should pay attention to the 2 commits

1 shows how to [define variables in _vars_ sessions of the playbook](https://github.com/rhops/ansible-static-include-demo/commit/594aca1ddef3f3e888588cd0990fa7fd97e0054b)

2 shows how to [define variables in _vars\_file_ session of the playbook](https://github.com/rhops/ansible-static-include-demo/commit/fa29542da30eb98ecf22c6b55d54ecddc1a867cb)
