In order to appeal to sysadmins that really want modules in bash instead of python, I made some local changes to ansible to improve bash support. Because these changes are working out well I’d like to share them with the community. I though of making a pull request, but I thought it would be a good idea to test out the idea in the development discussion group first.
Here’s a summary of the changes I made :
CHECKMODE SUPPORT: Added the ability for old-style modules to support check mode by including the tag <<ANSIBLE_MODULE_SUPPORTS_CHECKMODE>> somewhere in their source. If ansible finds that tag, it will run that module in --check mode even if it’s old-style. The module is of course responsible to check the value of CHECKMODE and behave accordingly. This should work with any module language, not just bash. Obviously, if the old-style module does not contain the tag, it will be skipped when running in check mode;
HELPER SCRIPT: This is a support script that can be source’d in bash modules and:
- Validates and parses the module’s parameters into environment variables, so the bash script can just jump in and start working.
- Captures stdout and stderr and returns them to ansible in JSON format when the script ends.
- Traps script errors and reports failures to ansible with an explanatory message, instead of just letting the script die without much explanation.
- Formats the module’s results in appropriate JSON format (quoting strings, formatting the dictionary, etc).
The helper script is currently external and I install it on each server in /usr/local/libexec using a playbook. I didn’t find a clean way to do this directly in ansible, so I’m not sure it could be a candidate for a pull request (or simply publish it as a new role?). Nevertheless, this script really simplifies bash modules and is worth sharing.
I’ve published the proposed changes in github at https://github.com/guysab/ansible_bash_support.git .
Does this look like ia good pull candidate?
Thanks!
Guy