Ansible’s module development page describes how to create your own Ansible modules in Python, and makes a mention of using shell scripts to do so. As I’m “into” Ansible at the moment, I thought I’d give it a try. I’ll also mention now that Ansible 0.5 has been released: one of the nice new features is it supports native SSH connections, so I can use Kerberos authentication.
Consider the following playbook which calls a module named m01:
The module is contained in a file of the name name which lives in a directory
library/ alongside the playbook file. Ansible transports the shell
script to nodes on which it is to be executed at the moment the action is
invoked; I don’t have to pre-install the module on nodes!
The shell script parses arguments passed it in the playbook (read the comments
below carefully) does things and returns arguments in the form
I execute the playbook just as I execute any other, here using Kerberos authentication as an example:
- This can be useful for prototyping, but creating Python modules is doubtless the better way.
- All programs invoked from the script must already exist on the node. If necessary, you can use the copy module to transport them to the node before calling your module. The same goes for files required by the module, which can also be created with the template module.
- The way I “parse” the arguments using
sourcecan be pretty deadly for the remote node; think about that!
Modules don’t have access to Ansible “facts”. They could parse the
setupfile but there’s been a bit of discussion on the Ansible mailing-list whether that file will remain; I hope it does in some form or other.