Developing Modules


This section discusses how to develop, debug, review, and test modules.

Ansible modules are reusable, standalone scripts that can be used by the Ansible API, or by the ansible or ansible-playbook programs. They return information to ansible by printing a JSON string to stdout before exiting. They take arguments in one of several ways which we’ll go into as we work through this tutorial.

See About Modules for a list of existing modules.

Modules can be written in any language and are found in the path specified by ANSIBLE_LIBRARY or the --module-path command line option or in the library section of the Ansible configuration file.

Should You Develop A Module?

Before diving into the work of creating a new module, you should think about whether you actually should develop a module. Ask the following questions:

  1. Does a similar module already exist?

    There are a lot of existing modules available. You should check out the list of existing modules at About Modules

  2. Has someone already worked on a similar pull request?

    It’s possible that someone has already started developing a similar PR. There are a few ways to find open module pull requests:

    If you find an existing PR that looks like it addresses the issue you are trying to solve, please provide feedback on the PR - this will speed up getting the PR merged.

  3. Should you use or develop an action plugin instead?

    Action plugins get run on the master instead of on the target. For modules like file/copy/template, some of the work needs to be done on the master before the module executes on the target. Action plugins execute first on the master and can then execute the normal module on the target if necessary.

    For more information about action plugins, read the action plugins documentation here.

  4. Should you use a role instead?

    Check out the Roles documentation for details.

How To Develop A Module

The following topics will discuss how to develop and work with modules:

Building A Simple Module
A general overview of how to develop, debug, and test modules.
Documenting Your Module
How to include in-line documentation in your module.
Conventions, Best Practices, and Pitfalls
Best practices, recommendations, and things to avoid.
Contributing Your Module to Ansible
Checklist for contributing your module to Ansible.
Testing Ansible
Developing unit and integration tests.
Ansible and Python 3
Adding Python 3 support to modules (all new modules must be Python-2.6 and Python-3.5 compatible).
Information for submitting a group of modules
A guide for partners wanting to submit multiple modules.

See also

About Modules
Learn about available modules
Developing Plugins
Learn about developing plugins
Python API
Learn about the Python API for playbook and task execution
GitHub modules directory
Browse module source code
Mailing List
Development mailing list
#ansible IRC chat channel

Appendix: Module Utilities

Ansible provides a number of module utilities that provide helper functions that you can use when developing your own modules. The module utility provides the main entry point for accessing the Ansible library, and all Ansible modules must, at minimum, import from

from ansible.module_utils.basic import *

The following is a list of module_utils files and a general description. The module utility source code lives in the ./lib/module_utils directory under your main Ansible path - for more details on any specific module utility, please see the source code.

  • - Utilities used by the a10_server module to manage A10 Networks devices.
  • - Adds shared support for generic API modules.
  • - Module support utilities for managing Apstra AOS Server.
  • - Module support utilities for managing Cisco ASA network devices.
  • - Definitions and utilities for Microsoft Azure Resource Manager template deployments.
  • - General definitions and helper utilities for Ansible modules.
  • - Utilities for CloudStack modules.
  • - Miscellaneous helper functions for PostGRES and MySQL
  • - Definitions and helper utilities for modules working with Docker.
  • - Definitions and utilities for modules working with Amazon EC2
  • - Helper functions for modules working with EOS networking devices.
  • - Helper functions for modules working with F5 networking devices.
  • - Helper functions for modules that return facts.
  • - Definitions and helper functions for modules that work with Google Compute Engine resources.
  • - Definitions and helper functions for modules that manage Cisco IOS networking devices
  • - Definitions and helper functions for modules that manage Cisco IOS-XR networking devices
  • - Contains single helper function that fixes os.path.ismount
  • - Definitions and helper functions for modules that manage Junos networking devices
  • - utilities for working with known_hosts file
  • - Allows modules to connect to a MySQL instance
  • - Functions and utilities for modules that work with the NetApp storage platforms.
  • - Configuration utility functions for use by networking modules
  • - Defines commands and comparison operators for use in networking modules
  • - Functions for running commands on networking devices
  • - Contains definitions and helper functions specific to Cisco NXOS networking devices
  • - Utilities for modules that work with Openstack instances.
  • - Definitions and helper functions for modules that manage OpenSwitch devices
  • powershell.ps1 - Utilities for working with Microsoft Windows clients
  • - Exception workaround for Python 2.4
  • - Definitions and helper functions for modules that work with Rackspace resources.
  • - Functions for modules that manage Red Hat Network registration and subscriptions
  • - Contains utilities to enable modules to work with Linux services (placeholder, not in use).
  • - Functions to allow modules to create shells and work with shell commands
  • - Module utils for working with the Six Python 2 and 3 compatibility library
  • - String splitting and manipulation utilities for working with Jinja2 templates
  • - Utilities for working with http and https requests
  • - Contains utilities for modules that work with VMware vCloud Air
  • - Contains utilities for modules that work with VMware vSphere VMs
  • - Definitions and functions for working with VyOS networking