# Role Composition Suggestions

**URL:** <https://forum.ansible.com/t/role-composition-suggestions/15808>\
**Category:** Ansible Developer\
**Tags:** galaxy-ng\
**Created:** [July 30, 2014, 5:07am UTC](https://forum.ansible.com/t/role-composition-suggestions/15808 "2014-07-30T05:07:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Agrajag\_Petunia](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@Agrajag\_Petunia](https://forum.ansible.com/u/Agrajag_Petunia)\
**Post date:** [July 30, 2014, 5:07am UTC](https://forum.ansible.com/t/role-composition-suggestions/15808/1 "2014-07-30T05:07:16Z")

</div>

Hi,

This could be a stupid question, but what is the best approach to nested role construction (or role composition)? Here is an example:

I was needing to set up a postgres role, so rather than re-invent the wheel I went on ansible galaxy and found one (which is awesome!). The only issue is that I want to wrap calling it in another role, which also calls a raid\_setup\_role. I would like my resulting role to look something like this.

> my\_postgres\_role/  
> defaults/  
> vars/
> 
> # override some of the Ansibles.postgresql default vars
> 
> tasks/  
> main.yml

etc…

> templates/  
> etc…

’  
where my\_postgres\_role/tasks/main.yml looks kind of like:

`

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [July 30, 2014, 8:48am UTC](https://forum.ansible.com/t/role-composition-suggestions/15808/2 "2014-07-30T08:48:12Z")

</div>

Did you look at dependencies?

[http://docs.ansible.com/playbooks\_roles.html#role-dependencies](http://docs.ansible.com/playbooks_roles.html#role-dependencies)

Though I’d recommend you to just call you custom role, then the galaxy role sequentially from the playbook.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 30, 2014, 12:49pm UTC](https://forum.ansible.com/t/role-composition-suggestions/15808/3 "2014-07-30T12:49:47Z")

</div>

I would recommend just calling the raid\_setup role before the other role.

Role dependencies are useful in some cases, and quite useful in others, but it’s probably not true that postgres needs to know anything about RAID, more likely, it just takes a directory path as a parameter.

So those things are related in terms of order, but one does not really depend on the other as it could still work on a non-raided setup.

---

<div class="post-metadata">

**Author:** ![Agrajag\_Petunia](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@Agrajag\_Petunia](https://forum.ansible.com/u/Agrajag_Petunia)\
**Post date:** [July 30, 2014, 4:44pm UTC](https://forum.ansible.com/t/role-composition-suggestions/15808/4 "2014-07-30T16:44:20Z")

</div>

First off, thanks for the quick responses and suggestions.

Serge van Ginderachter, I did look at dependencies but from the documentation I couldn’t tell if I could override the Ansibles.postgresql defaults with variables in my vars directory. I guess I could pass each variable in manually, but that seems slightly less clean. I’m guessing my role default variables will have been loaded into the play by the time the dependencies file is read? The only other issue I see is that currently we have modules to automatically compute values like shared buffers sizes, edit kernel memory settings, etc. I guess I could always just fork the existing role add those tasks to it as an option since they aren’t really application specific.

Michael, the reason I didn’t want to call the roles directly from a playbook is that all of our postgres machines use a RAID volume and I would like my co-oworkers to be able to quickly build their application playbooks using some high level roles I’ve put together for them.

Thanks again,  
A.P.

---

<div class="post-metadata">

**Author:** ![Agrajag\_Petunia](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@Agrajag\_Petunia](https://forum.ansible.com/u/Agrajag_Petunia)\
**Post date:** [July 31, 2014, 7:11pm UTC](https://forum.ansible.com/t/role-composition-suggestions/15808/5 "2014-07-31T19:11:53Z")

</div>

Alright, so looks like dependencies work and overriding some of the Ansibles.postgresql variables in my\_postgres\_role/defaults/main.yml also works. It might be helpful to have a footnote in the documentation mentioning that this is possible. The one issue I still have is overriding the ‘ansible\_distribution\_release’ variable, which doesn’t seem to work in either the defaults/main.yml or directly in a playbook. Is there a particular reason I can’t override ansible\_fact variables like other kinds of variables?
