Developing Ansible Modules
Ansible provides features that make it relatively easy to write automation for large-scale server installation, application deployment, and service operations. It’s one of the methods enabling DevOps,
- Ansible runs over SSH and requires SSH access to remote machines. No separate daemons or agents are needed.
- Remote machines (for default Ansible Modules) only need Python 2.6 or higher installed. (Some modules may require additional Python modules.)
- Ansible Modules are recommended to guarantee idempotency. For modules that exceptionally don’t guarantee idempotency, be sure to document warnings.
Introduction
An Ansible Module can be thought of as a set of functions with a specific purpose in one Task of an Ansible Playbook. For example, if you need to “copy a file on the remote host from path A to path B”, you can use the “copy” module provided by default in Ansible.
| |
From a simple I/O perspective, an Ansible Module receives input through Attributes, performs a set of functions based on the input, and outputs JSON Format to STDOUT. Ansible Modules are not limited to simple I/O during function execution; they can also cause side-effects such as integration with external systems, which can be very useful when used appropriately.
Implementation
Let’s create a simple module as an example that takes a directory path as input and returns a list of files in that path. Written for Python 2.7 / Ansible 2.2.
Development Environment Setup
Before implementing, you first need to set up an environment for Ansible Module development. The Ansible project on Github provides tools for testing when developing modules. You can use these.
| |
Writing Module Specification
After completing development environment setup, open the Ansible Module Python script in an editor. It’s recommended to write the module specification first. Because defining the module’s I/O spec before implementation makes it clearer what you’ll be implementing.
| |
Module Development
Once module specification is complete, you can proceed with Ansible Module implementation. Before implementation, let me briefly explain a few things. Ansible Modules are of course written in Python and officially support Python 2. Python 3 support started from Ansible 2.2, but some modules may not be compatible. (Ansible Python 3 Support)
Also, Ansible Modules are recommended to use module utility libraries provided by Ansible by default. However, using external dependencies is also possible, in which case it must be mentioned in the documentation.
Ansible Modules are mostly implemented in the following order:
- Define module attributes and specify data types, required status, allowed values, default values, etc.
- Define all status codes the module can return. When returning any error code other than success codes, the environment before and after module execution should be identical. (If a failure leaves things half changed, you can’t be sure what happens when you run it again.)
- Implement business logic based on module input attributes and generate JSON Format results.
- When all module processing is complete, return either exit_json (success) or fail_json (failure).
| |
Module Testing
After module implementation is complete, you can test whether it works as intended.
| |