chaining modules

Hi,

Im looking for a way to invoke existing modules from another module, where the invoking module will provide the needed params and receives the results.

This is to use a generic module in roles:

  • without duplicating various logic all over the place when doing it in a normal playbook
  • without duplicating (copying) logic inside the generic module

Eg:

  • restarting a “service” is different when using the service directly, or when run under somethling like supervisor
  • system package manager, where in the playbook I don;t have to care if it is done using yum or apt (the module should work that out, like the current group module)

There are solutions to each scenario you’ve described without having to worry about complex invocations of modules calling other modules.

For the package scenario, this has been discussed many times in the past in regards to a generic package manager. Simply put, abstracting out the underlying system gets you only so far - each distribution names things quite differently (eg. apache2 vs httpd), so you still have to care about the package name. The best solution for this scenario is to use an include with a conditional “when” based on the ansible_distribution fact. There are many examples of this out there, including in our integration tests.

For the service scenario, we already abstract out the sub-system in use, however for supervisord and a few others there are specific modules for controlling processes through those mechanisms. Again, this is somewhat distro/setup specific so abstracting things out only gets you so far and you still have to write plays to handle different scenarios.

If you have questions regarding any other scenarios let us know, but in general we try to avoid building overly confusing logic like this.

Hi,

I have indeed seen many examples, and the package manager is indeed quite doable as you described.

The supervisor vs service is something i’m currently trying to find a good solution to support this. Thats why im also looking at possibilities to do module chaining…

Also, my aim is for a personal/local module, not directly for inclusion in the core (except perhaps some api to be able to this).

And then it things get a bit complexer when the following does not work (anymore):

  • include: ‘{{ service_mode }}.yml’

when service_mode is not provided as var at playbook level, but either derived from a default or set in another role

or

  • template: …
    notify: [ ‘restart {{ service_mode }}’ ]

same issue as with include above

or

  • include: ‘{{ item }}’

with_first_found:

  • ‘{{ service_mode }}.yml’
  • service.yml

(removed) deprecated feauture

Don’t overcomplicate it.

This is not the Ansible way.

Over-abstraction is ultimately a path towards madness. We’re talking about computers here, which are simple things, so keep it simple.

Feel free to assign more than one role to a system, and a little bit of a repeated task here or there isn’t world-ending.