Creating Ansible Modules In BASH

Vince Sesto
Splunk User Developer Administrator
4 min readMar 3, 2021

Create Custom Ansible Modules Using BASH and Linux Shell

Image Courtesy of pexels.com

If you’ve used Ansible for a while, you probably know there is a module for pretty much everything. As Ansible is built using Python, its super easy for any company to provide supported modules for their application or service. It’s pretty rare you will ever need to create your own library but if you are a company wanting to provide support for your new application or service, you may be the one needing to create the Ansible module.

If your looking a great way to learn more about Ansible, follow the link to our latest book, Practical Ansible

A while ago we created a post showing you how you can create your own modules using Python, but this isn’t the only way you can create your own modules. Python allows you to create modules using BASH or Linux Shell scripts as well, which could be a great way to quickly set up a module without needing to know Python. You may already have a BASH script that is ready and working and setting up your module in Ansible this way, will mean its an easier way to transfer your BASH script to an Ansible Module.

In the following example, we will quickly run through how you can create a simply module in Ansible using BASH script. You should have Ansible running on your system, but if you need to go back and look over some of the basics, feel free to look through one of our previous posts.

1.Start by accessing your work environment and within the directory you are working in create a library directory:

mkdir library

2.Within in the library directory, we are going to create a simple module that will print the OS type of your system. We will call this module os_type, so in the library directory create the file named os_type.sh:

touch library/os_type.sh

3.Using your favourite text editor, open the new file and add in the following code(The numbers on the left are just there for reference and should not be added to the file):

  1 #!/bin/bash 
2
3 OS="$(uname)"
4
5 echo "{ \"changed\": false, \"operating_system\": \"$OS\" }"

The code is pretty simply, line 1 to 3 is a basic script that loads the operating system type into a variable called OS. The final line of the script is where all the magic happens. This provides a JSON output of what the script has achieved and the output of our script. As you can see there are two values that are being presented to the “changed” which is equal to false in this case. We are only printing a value, so we don’t need to set this to true, but if we were doing something different like installing an application, and our script reached this point, we would have this value set to true. The second item being output is the value of our operating system variable. We could put together a more customised script and add in error checking or providing different output depending on weather the script passes or fails, or errors.

4.Save the module we created and we now need to be able to run this module, so create a Ansible playbook called module_test.yml in your working directory:

touch module_test.yml

5.Open the module_test.yml file with your text editor and enter in the following details to run the os_type.sh module we have just created:

  1 --- 
2 - hosts: localhost
3 tasks:
4 - name: testing our new module
5 os_type:
6 register: result
7
8 - debug: var=result

6.It’s now time to run our playbook. You should have a hosts file set up already and you can run the playbook with the following command and you should get a similar result as the one we have:

ansible-playbook -i hosts module_test.ymlPLAY [127.0.0.1] ***************************************
TASK [Gathering Facts] *********************************
ok: [127.0.0.1]
TASK [testing our new module] **************************
ok: [127.0.0.1]
TASK [debug] *******************************************
ok: [127.0.0.1] => {
"result": {
"changed": false,
"failed": false,
"meta": {
"operating_system": "Linux"
}
}
}
PLAY RECAP **********************************************
127.0.0.1: ok=3 changed=0 unreachable=0 failed=0

We’ve been really quick to show you how to set up a module using BASH or Linux Shell. It gives you an easy option to create your modules especially if your coding skills are more biased towards the Linux Shell.

Found this post useful? Kindly tap the clap button below! :)

About The Author

DevOps Engineer, Endurance Athlete and Author. As a DevOps Engineer I specialize in Linux and Open Source Applications. Particularly interested in Search Marketing and Analytic’s, and is currently developing my skills in devops, continuous integration, security, Splunk(UI and Reporting) and development(Python).

--

--

Vince Sesto
Splunk User Developer Administrator

Vincent Sesto is a DevOps Engineer, Endurance Athlete, Coach and Author. One of his passion’s in life is endurance sports as both an athlete, coach and author.