top of page
  • Writer's pictureSuraj Dhakre

A Comprehensive Guide to Creating an Ansible Module

Introduction

Ansible is a powerful open-source automation tool that allows you to manage configurations, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates. One of the key features of Ansible is its extensibility, which is achieved through modules. Modules are standalone scripts that can be used inside an Ansible playbook. In this comprehensive guide, we will walk you through the process of creating your own Ansible module.



ansible module with python


Understanding Ansible Modules

Before we delve into creating an Ansible module, it's important to understand what they are and how they work. An Ansible module is essentially a script that gets executed on the target host machine. It can be written in any language that can return JSON, including Python, Perl, Ruby, Shell, etc. However, Python is the most commonly used language for writing Ansible modules due to its readability and simplicity.


Setting Up Your Environment

To start creating your own Ansible module, you need to have a suitable environment set up. This includes having Python installed on your system (preferably Python 3), as well as the latest version of Ansible. You also need a text editor or Integrated Development Environment (IDE) where you can write and edit your code.


Creating Your First Ansible Module

1. Create a new file: Start by creating a new file with a .py extension in your preferred text editor or IDE. The name of the file will be the name of your module.


2. Import necessary libraries: At the top of your file, import any necessary libraries. For most modules, you'll need to import at least the following:


#!/usr/bin/python
from ansible.module_utils.basic import AnsibleModule

3. Define your module: Next, define your module by creating a function that takes no arguments. This function will contain the main logic of your module.

def my_module():

4. Instantiate AnsibleModule: Inside your function, instantiate an AnsibleModule object. This object takes two arguments: a dictionary of input arguments and a boolean indicating whether the module supports check mode.


def my_module():
    fields = {
        "hostname": {"default": True, "type": "str"}
    }
     module = AnsibleModule( argument_spec=fields, supports_check_mode=True )

5. Implement functionality: After defining the input arguments, implement the functionality of your module. This could involve creating or deleting files, starting or stopping services, etc.


6. Return results: Finally, use the exit_json method of the AnsibleModule object to return the results of your module.

def my_module():
    ...
    result = dict( changed=False, original_message='', message='' )
    module.exit_json(**result)

7. Call your function: At the end of your file, call your function if the file is being run as a script.

if __name__ == '__main__':
    my_module()


Testing Your Module

After creating your module, it's important to test it to ensure it works as expected. You can do this by writing an Ansible playbook that uses your module and running it against a test host.


You can check out complete code here.

ansible custom module output


Conclusion

Creating an Ansible module may seem daunting at first, but with practice and understanding, it becomes a straightforward process. By following this guide, you should now have a basic understanding of how to create an Ansible module. Remember that creating modules is all about extending Ansible's functionality to suit your specific needs, so don't be afraid to experiment and try new things. Happy coding!

bottom of page