# chaining modules

**URL:** <https://forum.ansible.com/t/chaining-modules/15355>\
**Category:** Ansible Developer\
**Created:** [May 28, 2014, 2:04pm UTC](https://forum.ansible.com/t/chaining-modules/15355 "2014-05-28T14:04:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans\_Donner](https://avatars.discourse-cdn.com/v4/letter/h/c89c15/32.png) [@Hans\_Donner](https://forum.ansible.com/u/Hans_Donner)\
**Post date:** [May 28, 2014, 2:04pm UTC](https://forum.ansible.com/t/chaining-modules/15355/1 "2014-05-28T14:04:45Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 28, 2014, 3:02pm UTC](https://forum.ansible.com/t/chaining-modules/15355/2 "2014-05-28T15:02:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hans\_Donner](https://avatars.discourse-cdn.com/v4/letter/h/c89c15/32.png) [@Hans\_Donner](https://forum.ansible.com/u/Hans_Donner)\
**Post date:** [May 28, 2014, 3:20pm UTC](https://forum.ansible.com/t/chaining-modules/15355/3 "2014-05-28T15:20:42Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Hans\_Donner](https://avatars.discourse-cdn.com/v4/letter/h/c89c15/32.png) [@Hans\_Donner](https://forum.ansible.com/u/Hans_Donner)\
**Post date:** [May 28, 2014, 3:28pm UTC](https://forum.ansible.com/t/chaining-modules/15355/4 "2014-05-28T15:28:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 28, 2014, 3:45pm UTC](https://forum.ansible.com/t/chaining-modules/15355/5 "2014-05-28T15:45:08Z")

</div>

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.
