Configure non-existent roles with dependent roles

The problem is best described with an example:

There are two roles:

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

The mailing list software needs a mail server to transport incoming mail to the virtual mailbox mailing list software. This requires some configuration of the mail server. But the mail server does not know about the role of the mailing list and other roles with similar configuration requirements.

What I would like to do is:

  • mailinglist (and other similar roles) saves the transport configuration in the transport_config variable. It can be a transport card, for example $ email => $ spool.
  • mailinglist depends on the role of mailserver .
  • mailserver configures its transport using the transport_config variable.

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

What I can think of is a workaround: the mail server reads / analyzes the configuration directory in which the transport cards are defined. mailinglist and other roles add files to this directory. The problem here is that this often requires a “configuration constructor” that reads such configuration directories and generates the main configuration file.

+10
source share
5 answers

In response to your comment about the “configuration linker,” see build an indispensable module . The tricky part may be getting all the files in one place before starting the build module.

Otherwise, a reasonable suggestion for host_ and group_vars makes sense.

+3
source

You can do this using role-based dependencies .

In the mailinglist the role site by roles/mailinglist/meta/main.yml site roles/mailinglist/meta/main.yml , add something like this:

 --- dependencies: - { role: mailserver, transport_config: ... } 

Do the same for any other similar roles.

+19
source

Consider managing a dotdee configuration file with something like dotdee :

With dotdee you compile your final configuration file from a series of files placed in the .d directory.

In your case, the mailinglist role and others, depending on the mailserver will discard configuration fragments in the .d directory (created by the .d role) and run a command to update the dotdee --update /path/to/mailserver/conf configuration file, for example, dotdee --update /path/to/mailserver/conf .

+2
source

I know this is a long answer, but I just found a workable solution, and that is a little sneaky.

I use the third role settings , which has no tasks, only variables in defaults/main.yml . The docs are a bit vague in this matter, but the values ​​here become corrugated to all the dependent roles, so if both roles depend on settings through their meta/main.yml , both get a common set of values. They can be overridden in the usual way using the group_vars file.

The surprise for me was that, since, yes, the settings role is used more than once, it does not matter, because it has no tasks, and this data can flow from there into the dependency chain.

This is a completely new form of data flow in Ansible that I did not know was possible.

+2
source

I'm not familiar with your applications here enough, but I think you might be better off using host or group variables to pass your configuration into your roles. Roles may contain your default values, etc. And expect to get the values ​​of the host / group variables that you set on an individual basis. This may mean treating each instance of the application as a host, not a psychic host. How you model it depends on many of the subtle tasks of your workflow, configuration, and application.

This recent topic touches on some of these things: https://groups.google.com/forum/#!topic/ansible-project/yrnsx2Mw6rc

0
source

Source: https://habr.com/ru/post/971552/


All Articles