# Writing modules in yaml

**URL:** <https://forum.ansible.com/t/writing-modules-in-yaml/15476>\
**Category:** Ansible Developer\
**Created:** [June 16, 2014, 6:32pm UTC](https://forum.ansible.com/t/writing-modules-in-yaml/15476 "2014-06-16T18:32:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [June 16, 2014, 6:32pm UTC](https://forum.ansible.com/t/writing-modules-in-yaml/15476/1 "2014-06-16T18:32:43Z")

</div>

On more than one occasion I found myself encapsulating repeated sequences of tasks into a (parametrized) role to make them reusable. Adding nginx sites is one example. This has the disadvantage of losing the actions one can do on modules, such as loops. On the other hand, writing a module may require implementing functionality that’s already present in Ansible, with all the idempotency logic etc.

Making Ansible yaml a language one can write modules in can solve this pretty nicely I think. It will also preserve the “correct” use of roles, instead of using them as libraries.

Thoughts?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 16, 2014, 7:00pm UTC](https://forum.ansible.com/t/writing-modules-in-yaml/15476/2 "2014-06-16T19:00:51Z")

</div>

so you just want to run playbooks as modules?

shell: ansible-playbook …

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [June 16, 2014, 7:04pm UTC](https://forum.ansible.com/t/writing-modules-in-yaml/15476/3 "2014-06-16T19:04:28Z")

</div>

Roles as modules will be more accurate. I didnt outright said that though because I didn’t want to start a “you’re changing the meaning of roles” discussion… 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 16, 2014, 7:23pm UTC](https://forum.ansible.com/t/writing-modules-in-yaml/15476/4 "2014-06-16T19:23:13Z")

</div>

soo … what will this do that you cannot do already by running a role?​

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [June 16, 2014, 8:06pm UTC](https://forum.ansible.com/t/writing-modules-in-yaml/15476/5 "2014-06-16T20:06:03Z")

</div>

Using them in other roles, using them in loops, registering a return value, triggering them as handlers… any use for a module.

It’s just that roles aren’t enough when you need the above features used on them, and writing a module can duplicate functionality from existing modules.

---

<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:** [June 18, 2014, 3:31pm UTC](https://forum.ansible.com/t/writing-modules-in-yaml/15476/6 "2014-06-18T15:31:39Z")

</div>

Modules can be parameterized if you would like to use them for different things.

You can also use role dependencies to include modules with different parameters.

“Writing modules in YAML” doesn’t make sense based on the definition of what a module is in Ansible.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [June 18, 2014, 4:04pm UTC](https://forum.ansible.com/t/writing-modules-in-yaml/15476/7 "2014-06-18T16:04:39Z")

</div>

> Modules can be parameterized if you would like to use them for different  
> things.
> 
> You can also use role dependencies to include modules with different  
> parameters.

Yes, but roles and modules aren't the same thing: for example, you can't  
use a role as a handler, or run it in a loop.

"Writing modules in YAML" doesn't make sense based on the definition of

> what a module is in Ansible.
> 
> The definition of a module doesn't change. Being able to treat YAML files

as module just makes it easier to write good modules.

I'll stay with the nginx sites example. Creating and enabling an nginx site  
take more than one task - you need to copy (and template) the site file to  
sites-available and then link in to sites-enabled. If you want to be able  
to also remove sites, that's another task. You can put it in a role or  
include it as a tasks file, but then you lose capabilities such as loops  
and register variables. The "proper" option is writing this as a module (in  
bash, python or whatever). That's even more of a pain, because you now have  
to re-implement the template and file modules. If you could simply use your  
yaml file as a first-class module you'd get an idempotent and simple module  
that solves your problem.

Another suggestion is to be able to launch other modules from python  
modules, so you could re-use functionality. That still requires more  
boilerplate than YAML, though, and not as declarative.

Maybe an overdue disclaimer: I have no idea how hard it is to implement  
this. It just seems to me like a good way to reduce the barrier for writing  
modules.
