Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Ansible roles with dependent roles

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.

like image 801
Christian Avatar asked Jun 30 '14 18:06

Christian


2 Answers

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.

like image 57
Ben Whaley Avatar answered Sep 22 '22 02:09

Ben Whaley


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.

like image 35
encoded Avatar answered Sep 22 '22 02:09

encoded