# Introduction

Before I get into the article, I’d like to thank Matt Jaynes for featuring my last article in issue 43 of Anisble Weekly. That was a great surprise and hopefully got me a few more readers. Now back to the post.

Once again, school has kept me busy. I’ve since finished taking CSC 373 - Algorithm Design, Analysis, and Complexity, completing a final assignment and subsequent exam. I’ve just gotten back to working on the Ansible script that I started a month ago. In case you missed my post from last time, Ansible is a tool used to deploy and update applications in an easy to use language, using SSH, with no agents to install on remote systems. The specifications for Ansible are all written in YAML, making them well structured and generally easy to follow. Back at Wave, Nathan wrote An Ansible primer blog post on the Wave Engineering blog. A few weeks ago, I wrote a post about using Ansible to setup my local machine: Ansible or: How I Learned to Stop Wasting Time Setting Up My Computer and Script It. Here, I’ll be taking you through the creation of custom modules, starting with the why.

While writing the YAML for my task files, I found there were a few common tasks I wanted to do that did not have a module wrapper. The easy way to deal with those tasks is to use the Command module. That way I can tell Ansible to execute an arbitrary command on the machine. There are some downsides to doing this, which depend on the specific commands in question. One thing I like about the output of Ansible is that it can tell me how many commands Ansible executed, previously executed and caused errors (if I allow them). But yet, arbitrary commands are always run because Ansible has no way to know if they have been executed.

There are two options that I saw to address this, as I wanted to lower the number of changed tasks on repeated runs. I also wanted to prevent some commands from running multiple times. The first is to add another task and use conditional execution. I did that for two of the tasks, however it still had to register a changed task in order to do the check. This led me to look into writing my own modules and I wasn’t able to find many great examples. I should have started with looking at the modules in the Ansible library, but that did not occur to me. In the end I wrote the modules with the help of the docs, the Ansible IRC channel and Ansible library modules. Below I’ll outline writing my own module as well as conditional execution. Note, I’ll be providing specific examples, but there’s a lot more Ansible can do so please check out the docs if you’re interested in using Ansible.

# Conditional Execution

There isn’t a whole lot to write here, the code is self explanatory. I wrote this to fix a screen dimming issue on my sager laptop. This particular set of tasks is also conditionally executed, covered by my last blog post.

# Module Writing

The first thing to work out for a new module is what arguments it will take and how to leverage Ansible to do most of the heavily lifting around it. Here’s the start of my Ansible module for gconftool-2:

Most of that is pretty easy to follow and it shows a few of the options Ansible handles. It handles mutual exclusions, requiring one of a set items as well as items which are required together. Additionally, in the argument_spec you can also define defaults (and more, although this was all that I needed). The supports_check_mode is a boolean which represents that the module supports dry runs. Now that I’ve defined my argument structure, it’s time to get onto the next piece of code.

# The Getter and Setter

The module set in the above code block contains some crucial functions, one of which is run_command. This wraps the native python system library for running commands. It has very good error handling and provides a plethora of input options. Most of those aren’t needed for simple tasks like running gconftool-2. All I’m doing in the code block is building the command that I’d previously supplied to the Command Module. I’ve already shown how to define the arguments, but now I’ll show how to access them.

# Argument Accessing

Ansible makes arguments extremly easy to access. Now I’ll show you a sample of how I worked with the input.

# Argument Parsing

There are a few interesting things to note in this code block. The type specifications works for individual items but there is no equivalent for lists. When I set pair to type list it does not support type checking of particular elements or the length of the list. As such, I have to do my own error checking. Fortunately, Ansible provides a module.fail_json(msg='blah') to use for error reporting back to the console. There is a lot more you can provide than just a message but for my purposes that was perfect. Otherwise I’m just formatting the code to send off to my getter and setter. Now I’ll provide you with the skeleton for the rest of the module.

# The Skeleton

As you can see, there isn’t much more to do. That conditional is how I support check mode. All I’m doing here is calling my getter and setter and then using Ansible’s module.exit_json() to handle to integration with Ansible. It requires the changed argument and the rest is custom and allows me to decide what gets printed on the screen when things change. It’s also important to note that at the bottom of the file I’m calling the main function, which is required to run the module. The only other section of the file, which I’ve done so that I may contribute to Ansible, is the documentation at the top of the file.

# Documentation

The documentation includes ussage information as well as examples, here is an exerpt from the file:

# Usage

Using your custom modules is easy to do. Once you create a custom_modules directory, you can use them within Ansible. All you need to do is provide the module-path argument like so --module-path custom_modules.

Once you’ve done that, you can use it like any other Ansible module in your task files. i.e.

# Testing

The Ansible repo provides an executable that allows for easy module testing from the command line. It can be invoked with a command like:

~/ansible/hacking/test-module -m ./gconftool-2 -a "key=/path/to/stuff pair-car-type=int pair-cdr-type=string pair=1,'Joseph Kahn'"

You can also use breakpoints if you provide the debugger used, i.e.

~/ansible/hacking/test-module -m ./gconftool-2 -a "key=2 list=1,1,true,1 list-type=bool" -D ipdb

Running the testing module provides to full generated source file, as well as the standard output information. The output file can be run manually without the use of additional arguments.

# Conclusion

Writing a simple Ansible module wasn’t very difficult and requires little code to get the basics up and running. Even with all the error handling and verification done above, the module code is only 117 lines. While I did this for convenience, it’s easy to see how making modules simple to write can help make great provisioning scripts. These modules are relatively easy to follow and integrate seamlessly with the rest of the modules.

# That’s It

That’s all you need to get a custom module up and running and reporting changes. I’ll leave you with a few relevant links: