# template not looking in templates directory for included roles

**URL:** <https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336>\
**Category:** Ansible Project\
**Created:** [August 21, 2016, 2:32pm UTC](https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336 "2016-08-21T14:32:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Watrous](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Daniel\_Watrous](https://forum.ansible.com/u/Daniel_Watrous)\
**Post date:** [August 21, 2016, 2:32pm UTC](https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336/1 "2016-08-21T14:32:33Z")

</div>

I have a playbook with several roles. For example, I have an nginx role and a php role. Each has the typical directory structure. The nginx role has a template and a task for a virtualhost file that appears like this in the tasks

`

- name: “Move virtual host file into place”  
template: “src={{item.src}} dest={{item.dest}} mode={{item.mode}} owner={{ nginx\_site\_user }} group={{ nginx\_site\_group }}”  
with\_items:
- dest: “{{ nginx\_sites\_available }}/{{ nginx\_site\_user }}”  
src: “{{ nginx\_vhost\_file\_template }}”  
mode: 640  
notify:
- restart nginx  
`

When I run the nginx role, the variable nginx\_vhost\_file\_template is set to “default” and the file “roles/nginx/templates/default” exists. This works fine.

For my PHP role, I simply want to override the nginx\_vhost\_file\_template variable to point to a file “phpsitevhost” in the directory “roles/php/templates/phpsitevhost”. The php role includes the file “meta/main.yml” with the following contents.

`

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [August 22, 2016, 9:14am UTC](https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336/2 "2016-08-22T09:14:25Z")

</div>

Roles look in their own templates/ directory for templates. The nginx  
role will be applied first due to the dependency, but it has its own  
templates/ directory.

---

<div class="post-metadata">

**Author:** ![Daniel\_Watrous](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Daniel\_Watrous](https://forum.ansible.com/u/Daniel_Watrous)\
**Post date:** [August 22, 2016, 2:47pm UTC](https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336/3 "2016-08-22T14:47:21Z")

</div>

It does have its own templates directory, but that directory doesn’t have a file of that name. I’m wondering how I can get the documented functionality that would have ansible look in the other templates directories until it finds that file.

Otherwise, is there some way to accomplish what I am trying to do?

Daniel

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [August 22, 2016, 5:55pm UTC](https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336/4 "2016-08-22T17:55:19Z")

</div>

I'm not seeing the documented feature you're describing at the link  
you posted, and to my knowledge  
Anisble has never searching other roles templates/ for templates.

If I was tackling this problem, I'd have the php role contain the  
templates it needs.  
You already know the nginx role has been applied, so you know where to put them.

Handlers and vars from the nginx dependency are visible when you apply  
the php role,  
so you can find the right place to put your configs and bounce nginx  
if you need to easily  
enough.

---

<div class="post-metadata">

**Author:** ![Daniel\_Watrous](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Daniel\_Watrous](https://forum.ansible.com/u/Daniel_Watrous)\
**Post date:** [August 22, 2016, 7:15pm UTC](https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336/5 "2016-08-22T19:15:58Z")

</div>

The documented feature is listed under behaviors:

“Any copy, script, template or include tasks (in the role) can reference files in roles/x/{files,templates,tasks}/ (dir depends on task) without having to path them relatively or absolutely”

Based on this I would expect a filename reference to look in “roles/x/templates” until it found the template file.

I can duplicate the file for lower roles, but I would rather parameterize it and have the stated functionality work to find it in the templates folder of the other role.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [August 22, 2016, 7:29pm UTC](https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336/6 "2016-08-22T19:29:29Z")

</div>

Ok I think you're misreading the docs.

"Any copy, script, template or include tasks (in the role) can  
reference files in roles/x/{files,templates,tasks}/ (dir depends on  
task) without having to path them relatively or absolutely"

(in the role) above refers to role x. roles can't see each others bits n pieces.

The php role needs nginx to be setup,it also needs a vhost.

Just have it set the nginx role as a dependency (like you already  
are), but put the vhost template in the php role  
e.g.

roles/  
├── nginx  
│ ...rest of role skipped...  
│ ├── defaults  
│ │ └── main.yml # \<- php role can see these vars  
│ └── handlers  
│ └── main.yml # \<- php role can run these handlers  
└── php  
│ ...rest of role skipped...  
&nbsp;&nbsp;&nbsp;&nbsp;├── meta  
&nbsp;&nbsp;&nbsp;&nbsp;│ └── main.yml # \<- if there's a dependency set here  
&nbsp;&nbsp;&nbsp;&nbsp;└── templates  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└── foo.vhost.j2 # \<- template lives here

---

<div class="post-metadata">

**Author:** ![Daniel\_Watrous](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@Daniel\_Watrous](https://forum.ansible.com/u/Daniel_Watrous)\
**Post date:** [August 23, 2016, 2:19am UTC](https://forum.ansible.com/t/template-not-looking-in-templates-directory-for-included-roles/22336/7 "2016-08-23T02:19:48Z")

</div>

I think you’re right that I read that wrong. However, I did find a way to reference the file from another role that I think should work for me.

{{ role\_path }}/…/php/templates/phpsitevhost

Since I know the name of the role that will be installed, and that it will be adjacent to other roles, I should be able to always use “{{ role\_path }}/…/” to access the files from another role.

This way I can prevent duplicating these files.

Thanks for your help.
