Improved bash modules support

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:

  1. Validates and parses the module’s parameters into environment variables, so the bash script can just jump in and start working.
  2. Captures stdout and stderr and returns them to ansible in JSON format when the script ends.
  3. Traps script errors and reports failures to ansible with an explanatory message, instead of just letting the script die without much explanation.
  4. 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

Hi Guy, replies inline below!

Hi Michael,

I like it. It makes a lot of sense, and much cleaner. I’ll play on this later this weekend (I’m EDT-zone). Knowing that the helper script might end up officially in ansible, I’ll also polish it a bit more to make sure it’s complete.

Thanks!

Excellent – yeah just to be clear, don’t do a new helper script if you can avoid it.

Upgrade the ModuleReplacer class in Python to replace the code inside the bash module at that point – where it says #INSERT_HELPER_BASH_FUNCTIONS_HERE or whatever.

Then, if you can, modify ./hacking/test-module so it can help test both python and bash modules. That might be somewhat harder, but it would be nice if possible.

And EDT is the best. Go EDT! Beat CST!

–Michael

Hi Michael,

Both changes are ready for final comments. I’ve put them in my fork at https://github.com/guysab/ansible.git and split the commits in two, helper and checkmode support. I’ve included a complete example, added doc and improved the helper module (added support complex JSON structures, debug options plus some hardening). The helper already works with hacking/test-module, so no more changes were required there. However I couldn’t figure out exactly what doc was missing for ruby/other, so doc changes only relate to my contribution.

I had tons of problems running make/webdocs on my two platforms (CentOS-6 and Mac OS X). I finally tested my doc on the relase1.5.3 branch and everything worked, so it may be related to some other stuff in devel. Could you check if your webdocs works fine on devel? If so, I’ll investigate more.

Since this is my first contribution, and I have the intention of making more, please point out anything you would like to see different! When everything’s peachy, I’ll send a pull request.

Thanks!

Guy

Hi, thanks for this.

Since we’ve got a big backlog, it helps to have a standard way to review these, so would you mind submitting a pull request via GitHub?

Thanks!

Hi Michael, no prob. Will do!