DEV Community

Cover image for A dive into Ansible Modules and Module Utilities
Sunish Surendran K
Sunish Surendran K

Posted on

A dive into Ansible Modules and Module Utilities

I have been tasked with developing a custom script to be executed on a target machine via Ansible. While aware of Ansible's capability to integrate custom scripts, I am uncertain whether such integrations are accomplished via Ansible Modules or Plugins ?

Anibsle module vs Ansible plugin

After quick research in google I understood that Module is designed to handle a specific task and provides a standardized interface for Ansible to communicate with the remote systems.

Plugins, on the other hand, are used to extend the functionality of Ansible itself, rather than defining the desired state of a system. Plugins can be used to add new functionality to Ansible, such as custom inventory scripts, custom connection types, and custom callback plugins. Plugins are generally written in Python and can be loaded by Ansible at runtime.

ansible meme

So now I know I need to create module not plugin, now the question is in which language I need to write the module Python or PowerShell? It's depending upon the machine you are targeting. If you are planning to run the module against a Linux machine, then python is the best choice. In my case I am targeting a windows machine, so I need write down a windows module.

Suddenly a question popped up on my mind why I can't write a python module and use it with windows and Linux, that is the best way right?

It is not possible. Ansible team explained the reason in below link.

https://docs.ansible.com/ansible/latest/os_guide/windows_faq.html#can-i-run-python-modules-on-windows-hosts

Ansible overview

ok, then let's create ansible PowerShell module.

When I read through the documentation I understood we should use ansible inbuild **Module_Utils **while creating custom ansible modules.

Image description

Why to use ansible Module_Utils?

Module_utils can be used by Ansible module developers to simplify their code and reduce duplication. By using module_utils functions, module developers can avoid writing boilerplate code and instead focus on implementing the core functionality of their module.

For example, the ansible.module_utils.basic module provides functions for handling input and output, while the ansible.module_utils.urls module provides functions for making HTTP requests.

Where I can find this ansible Module_Utils?
You can find it under the path where ansible is installed.

Ansible Module Utils

How to import Module_Utils to my custom module?

Like I shown below you can import Module_Utils for python and PowerShell module

#!/usr/bin/python
from ansible.module_utils.basic import AnsibleModule
Enter fullscreen mode Exit fullscreen mode
#!powershell
#AnsibleRequires -CSharpUtil Ansible.Basic
Enter fullscreen mode Exit fullscreen mode

Ansible python and powershell module

#python
module = AnsibleModule(argument_spec=spec)
Enter fullscreen mode Exit fullscreen mode
#powershell
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
Enter fullscreen mode Exit fullscreen mode

Note: spec parameter is a hashtable/dictionary that defines the argument specification for the module.

In both Python and PowerShell ansible module you can see that I created an object called module for the class AnsibleModule.

The AnsibleModule class is a core component of Ansible that provides a standard interface for writing Ansible modules.

AnsibleModule class can be used to access the input parameters and the output results of the module. The object module created provides several methods and properties, including:

  • Params: A hashtable that contains the input parameters passed to the module.

  • ExitJson($result): A method that exits the module and returns the output result in JSON format.

  • FailJson($msg): A method that exits the module with an error message in JSON format.

  • Warn($msg): A method that writes a warning message to the console.

  • Fail($msg): A method that writes an error message to the console and exits the module.

Using all these methods I created a nice PowerShell module for ansible.
I don't want to explain how I written the module because it is already mentioned in the ansible documentation https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general_windows.html

So now start writing your own ansible modules. :) enjoy!!

Top comments (0)