Override a role's templates?

17 views
Skip to first unread message

Dick Visser

unread,
Jan 31, 2019, 7:44:41 AM1/31/19
to ansible...@googlegroups.com
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
template:
src: "{{ item }}"
dest: "/etc/nagios3/conf.d/local.cfg"
with_first_found:
- nagios_local.j2 <-- custom template
- _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'.

--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

S C Rigler

unread,
Jan 31, 2019, 8:19:18 AM1/31/19
to ansible...@googlegroups.com
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.
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAL8fbwN3uChckJrf%3DofYTHyGcHhAaCSsTrxr%3DzCNB3kdzQo%2BBA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Jan 31, 2019, 11:57:26 AM1/31/19
to ansible...@googlegroups.com
On 31.01.2019 13:44, Dick Visser wrote:
> 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

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:
content: '{{ my_template }}'
dest: /tmp/test

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages