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?
so you just want to run playbooks as modules?
shell: ansible-playbook …
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…
soo … what will this do that you cannot do already by running a role?
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.
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.
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.