Configure Ansible roles with dependent roles
I know this is long answered, but I only just found a workable solution, and it's a little sneaky.
I use a third settings
role, which has no tasks, only variables in a defaults/main.yml
. The docs are a bit vague on this, but values here get rippled to all dependent roles, so if both roles depend on settings
through their meta/main.yml
files, both get a common set of values. These are overridable in the usual ways, through a group_vars
file.
The surprise to me was that since, yes, the settings
role is used more than once, it doesn't matter because there are no tasks in it, and that data can flow from there up the chain of dependencies.
It's an entirely new form of data flow in Ansible that I didn't know was possible.
You can accomplish this using role dependencies.
In the mailinglist
role under roles/mailinglist/meta/main.yml
, add something like this:
---
dependencies:
- { role: mailserver, transport_config: ... }
Do the same for any other similar roles.
In response to your comment about a "configuration builder", see the assemble ansible module. The tricky part might be getting all the files into one place, before you run the assemble module.
Otherwise, tima's suggestion of host_ and group_vars makes sense.
Consider managing the mailserver
configuration file with something like dotdee
:
- Original Post by Dustin Kirkland describing the concept
- Dustin's
dotdee
repository - A simpler implementation with less features in Python
With dotdee
, you assemble your final configuration file from a series of files placed in a .d
directory.
In your case, the mailinglist
role and others depending on mailserver
would drop configuration snippets in a .d
directory (created by the mailserver
role) and run a command to update the mailserver
configuration file like dotdee --update /path/to/mailserver/conf
.