# Role templates to reduce boilerplate

**URL:** <https://forum.ansible.com/t/role-templates-to-reduce-boilerplate/14675>\
**Category:** Ansible Project\
**Created:** [February 28, 2014, 4:45pm UTC](https://forum.ansible.com/t/role-templates-to-reduce-boilerplate/14675 "2014-02-28T16:45:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [February 28, 2014, 4:45pm UTC](https://forum.ansible.com/t/role-templates-to-reduce-boilerplate/14675/1 "2014-02-28T16:45:59Z")

</div>

Hi,

TL;DR: I'd like to factor out the commonalities of my role definitions  
into re-useable templates and I was wondering what the best way to do  
this is.

My current setup follows the canonical role setup with a common role  
and a bunch of more specific roles. For example, I have a role to  
setup Cassandra, another role to setup Postgres, some other role to  
install a daemon, and so on. Each role is a cohesive unit of  
functionality and because of this, I can re-arrange the distribution  
of roles across different deployments.

That said, each role has bunch of common stuff in it that repeats  
itself. For example, making sure mount points exist, certain users  
exist, apply config templates, certain tarballs are sourced and  
unpacked, etc. There's a lot of non-role specific boilerplate.

I'd like to parameterize my role definitions. I've looked at includes  
and parameterized roles. Parameterized roles feels closest to my  
needs, but the problem I see is that they are only parameterized to  
extent of simple parameters - when jinja templates, vars, and handlers  
come into play, it feels like I need some kind of role inheritance(\*).

Includes feel more straight forward, but I guess a role would need to  
include a global task definition, i.e. it would include something from  
outside the scope of its role.

So I'm left wondering whether I've missed a trick somewhere, or maybe  
I've just missed some documentation somewhere.

Cheers,

Ben

(\*) Sounds complicated.

---

<div class="post-metadata">

**Author:** ![Ben\_Hood](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Ben\_Hood](https://forum.ansible.com/u/Ben_Hood)\
**Post date:** [February 28, 2014, 5:13pm UTC](https://forum.ansible.com/t/role-templates-to-reduce-boilerplate/14675/2 "2014-02-28T17:13:54Z")

</div>

So I'm playing with the following global include task definition that  
produces per-role include configs for syslog-ng:

- name: Template out the included syslog configuration file  
&nbsp;&nbsp;action: template src=roles/{{ role }}/templates/syslog/{{ role  
}}.conf.j2 dest={{ syslog\_include\_dir }}/{{ role }}.conf  
&nbsp;&nbsp;notify:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- reload syslog  
&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;- syslog

It seems to work. There are a couple of hacky aspects to this:

- The role that includes this task definition needs to define a  
variable called "role" - there might be a better way to do  
- The task definition needs to specify the full path to the per-role template

Does this seem reasonable or am I missing a trick?
