Developing Custom Modules and Plugins

Last updated: January 12, 2026

πŸ“š Reference: This guide aligns with the official Developing modulesarrow-up-right and Developing pluginsarrow-up-right documentation.

My Journey into Extending Ansible

After using Ansible for several years, I hit a wall: I needed to automate a proprietary API that had no existing Ansible module. I could have cobbled together shell scripts, but I knew Ansible was capable of more. That's when I learned to develop custom modules. Later, I discovered the power of plugins to extend Ansible's core functionality. This journey transformed me from an Ansible user to an Ansible developer.

Why Develop Custom Modules and Plugins?

While Ansible ships with thousands of modules and plugins, you may encounter scenarios where custom development makes sense:

When to Create a Custom Module

  • Interact with proprietary systems or APIs

  • Encapsulate complex logic into reusable components

  • Improve performance over shell/command modules

  • Ensure idempotence for specific operations

  • Provide better error handling and reporting

When to Create a Custom Plugin

  • Extend Ansible's core functionality

  • Create custom filters for data transformation

  • Implement specialized connection methods

  • Build custom inventory sources

  • Add application-specific callbacks

Understanding the Difference: Modules vs Plugins

Modules

  • Execute on managed nodes (remote systems)

  • Perform specific tasks (install package, create user, etc.)

  • Return JSON output

  • Can be called from playbooks or ad-hoc commands

  • Examples: apt, yum, copy, template

Plugins

  • Execute on the control node (where Ansible runs)

  • Extend Ansible's core functionality

  • Process data, connect to systems, display output

  • Examples: Filters (to_yaml), Callbacks (timer), Connection (ssh)

Developing Custom Ansible Modules

Environment Setup

First, set up your development environment:

Module Structure and Best Practices

Every Ansible module follows a standard structure:

Real-World Example: Custom API Module

Here's a practical example that interacts with a REST API:

Testing Your Module Locally

Create an arguments file for local testing:

Using Your Module in a Playbook

Developing Custom Plugins

Plugin Types

Ansible supports several plugin types:

  1. Filter Plugins - Transform data in templates

  2. Test Plugins - Validate data in conditionals

  3. Lookup Plugins - Retrieve data from external sources

  4. Callback Plugins - Respond to events and control output

  5. Connection Plugins - Connect to remote hosts

  6. Inventory Plugins - Load inventory from external sources

  7. Vars Plugins - Inject additional variables

  8. Action Plugins - Execute logic on control node before modules run

  9. Cache Plugins - Store and retrieve facts

Creating a Custom Filter Plugin

Filter plugins are the most commonly created custom plugins:

Using Custom Filters

Creating a Custom Callback Plugin

Callback plugins control output and respond to events:

Creating a Custom Lookup Plugin

Lookup plugins retrieve data from external sources:

Testing and Validation

Testing Modules with ansible-test

Creating Unit Tests

Integration Testing

Packaging and Distribution

Creating a Collection

Modern Ansible development uses collections:

Building and Publishing

Best Practices

  1. Documentation - Always include DOCUMENTATION, EXAMPLES, and RETURN

  2. Error Handling - Use proper exception handling and meaningful messages

  3. Idempotence - Ensure modules can run multiple times safely

  4. Check Mode - Support --check mode for dry runs

  5. Testing - Write unit and integration tests

  6. Python 3 - Use Python 3 compatibility headers

  7. No Logging - Mark sensitive parameters with no_log: true

  8. Type Hints - Use proper type specifications in argument_spec

  9. Dependencies - Document and check for required libraries

  10. Performance - Be mindful of API rate limits and performance

Conclusion

Developing custom Ansible modules and plugins opens up infinite possibilities for automation. Whether you're integrating with proprietary systems, creating specialized filters, or building custom callbacks, these skills transform you from an Ansible user to an Ansible developer.

Start small, test thoroughly, and contribute back to the community when possible!


Further Reading

Ready to extend Ansible with your own modules and plugins? Start building today! πŸ”§

Last updated