Creating You’re Own Ansible Modules

Vince Sesto
Splunk User Developer Administrator
4 min readMar 20, 2019

Customising Ansible For Your Needs Is So Easy

Each day the number of Ansible modules continues to grow with extra support added as the usage of the application continues. So why would you need to create your own Ansible modules. Well there are a number of reasons, but I think the most valid one is, so you can gain an understanding on how the modules work. Even though, this new found skill may not come up very often, there is more of a likelihood of you needing to troubleshoot errors or issues with existing Ansible modules, so knowing how everything works can be a valuable skill.

You also need to remember there are a lot of older legacy applications or in house applications your organisation may be using which haven’t got their own modules. There are other options for deploying these applications through Ansible and providing templates to configure them, but it may be easier to create your own module for these applications. Either way, the following document will provide you with the details to get you started and hopefully allow you to extend it further.

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

This is a pretty basic example, but I hope you’ll see that all you need is a little bit of knowledge of Python, combine with your Ansible skills and you’ll be able to start creating your own Ansible Modules…Don’t worry if your knowledge is a little deficient on either, everything will be explained as we go through it together.

We’re just going to cover a basic “Hello World” module, to show you how easy this is. This post is not going to cover the installation and basics of Ansible, so feel free to check out our previous posts for more details.

1.Start by accessing your work environment, which already has Ansible installed on it. Within the directory, you are working in create a library directory:

mkdir library

2.Within in the library directory, create your new module file name hello_module.py:

touch library/hello_module.py

3. With 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 #!/usr/bin/python
2
3 from ansible.module_utils.basic import *
4
5 def main():
6 module = AnsibleModule(argument_spec={})
7 response = {"hello": "world!"}
8 module.exit_json(changed=False, meta=response)
9
10 if __name__ == "__main__":
11 main()

There’s nothing too complicated here, but if your not familiar with Python, here is a break down of each of the lines. Line 1 specifies that it needs to use the locally installed version of Python. Line 3 imports the specific modules which will run with Ansible. Lines 5 to 8 is the function which will run as part of your module, specifically line 6 provides the AnsibleModule class which allows us to handle incoming parameters and allows us to exit the program, with the response being “hello” and then “world!” in line 7. Line 10 and 11 then run the function when the module is called.

4.We can now create a playbook to run the module. In your working directory, create the file newmodule_test.yml:

touch newmodule_test.yml

5.You can now fire up your text editor and add in the following details so your playbook will run

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

If you haven’t worked with Ansible before, the above code is able to assign the host in line 2 to be the one you are working on. Line 3 to 6 run the new module, with line 4 naming the task, line 5 calling the module, and line 6 registering the result as a variable named result. Then finally line 8 uses the debug function to output the results to show you everything worked.

6.We can now run the new playbook with the following command:

ansible-playbook newmodule_test.yml

Which gets you the following output:

PLAY [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": {
"hello": "world!"
}
}
}
PLAY RECAP **********************************************
127.0.0.1: ok=3 changed=0 unreachable=0 failed=0

If all went well, you should see the above mentioned output giving you a nice “hello”: “world!” as the output.

Ok, we’ve been pretty quick and dirty here and that was the aim of the post, but all you need is to extend your knowledge of Python and you will also be able extend what your modules can do.

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(Java).

--

--

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.