A dive into Ansible Modules and Module Utilities
Tags: ansible, module, programming, python
Published: 2023-05-09 15:45:17

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wqc4e52vhygg9e68wbp.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oew4ljpmjkikee9av2t5.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/no6aiil2ve2f5yd219ws.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r0nxu1dl7jfus6t48n2c.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5scv83x3121qi1uj8ip.png)

**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
```

```
#!powershell
#AnsibleRequires -CSharpUtil Ansible.Basic
```

![Ansible python and powershell module](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gl309ryyqs4eet579j1d.png)
```
#python
module = AnsibleModule(argument_spec=spec)
```
```
#powershell
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
```

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!!