Configure Ansible roles with dependent roles

Christian picture Christian · Jun 30, 2014 · Viewed 19.9k times · Source

The problem is best described with an example:

There are two roles:

  • mailserver: a basic mail server configuration
  • mailinglist: mailing list application

The mailing list software needs the mailserver to transport incoming mails to the mailing list software's "virtual inbox". This requires some configuration of the mail server. But the mailserver does not know about the mailing list role, nor other roles with similar configuration requirements.

What I would like to do is this:

  • mailinglist (and other similar roles) stores the transport configuration in a variable transport_config. This could be a "transport map" like $email => $spool.
  • mailinglist depends on the mailserver role.
  • mailserver configures it's "transport" using the variable transport_config.

Is there a way to do something like this in Ansible? Or another solution to this problem? It's not possible to use role variables like {role: mailserver, transport_config: ...}, as there may be more than one role depending on the mailserver.

What I can think of is a workaround: The mailserver reads/parses a configuration directory where transport maps are defined. mailinglist and other roles add files to this directory. The problem here is that this often requires a "configuration builder" which reads such configuration directories and generates the main configuration file.

Answer

Ben Whaley picture Ben Whaley · Jul 15, 2014

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.