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:
`
Did you look at 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.
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.
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.
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?