# Override a role's templates?

**URL:** <https://forum.ansible.com/t/override-a-roles-templates/29579>\
**Category:** Ansible Project\
**Created:** [January 31, 2019, 12:44pm UTC](https://forum.ansible.com/t/override-a-roles-templates/29579 "2019-01-31T12:44:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 31, 2019, 12:44pm UTC](https://forum.ansible.com/t/override-a-roles-templates/29579/1 "2019-01-31T12:44:21Z")

</div>

Hi

I'm using roles extensively to separate functionality, and I'm trying  
to keep the roles as general as possible. The resulting playbooks are  
very minimal, containing not more than a set of include\_role tasks.

Many roles use templates, which are stored inside the role's templates  
directory.  
Those templates can sometimes be quite specific, and are not usable in  
every scenario.  
So I'm looking for a way to 'override' the templates.  
Putting my custom template next to my playbook doesn't work, but it  
comes close. That path is included in the searchpath - but has lower  
priority:

- role\_path/templates  
- role\_path  
- role\_path/tasks/templates  
- role\_path/tasks  
- playbook\_dir/templates  
- playbook\_dir

So I ended up working around this by renaming the template and  
changing the template task to:

- name: Ensure All-in-one Nagios configuration is available  
&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "/etc/nagios3/conf.d/local.cfg"  
&nbsp;&nbsp;with\_first\_found:  
&nbsp;&nbsp;&nbsp;&nbsp;- nagios\_local.j2 \<-- custom template  
&nbsp;&nbsp;&nbsp;&nbsp;- \_nagios\_local.j2 \<-- default template

This does actually work, but it feels a bit kludgy.  
Is there a way to change the ordering of the items in the template searchpath?

BTW - Ideally I would like to be able to use the same inheritance structure  
that is already in place for variables, in particular these concepts:

- group\_templates/all  
- group\_templates/mygroup  
- host\_templates/myhost

The same can be said for 'files'.

---

<div class="post-metadata">

**Author:** ![S\_C\_Rigler](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@S\_C\_Rigler](https://forum.ansible.com/u/S_C_Rigler)\
**Post date:** [January 31, 2019, 1:18pm UTC](https://forum.ansible.com/t/override-a-roles-templates/29579/2 "2019-01-31T13:18:58Z")

</div>

Maybe something like setting "NAGIOS\_TEMPLATE: nagios\_local.j2" in  
your role's defaults then override NAGIOS\_TEMPLATE (inventory variable  
or whatever) as needed with a path to an alternate template.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 31, 2019, 4:57pm UTC](https://forum.ansible.com/t/override-a-roles-templates/29579/3 "2019-01-31T16:57:10Z")

</div>

Instead of template module use the copy module with content instead of src.

The template you have in a variable and use content like so.

- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: '{{ my\_template }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/test
