So, you have started using Ansible and you love it. It is easy, effective, and very powerful. You see now that everything you have done with scripts so far could be done with Ansible, only more elegant and faster since there are a lot of available modules that cover most of your needs. But you also feel nostalgic about writing your own scripts. You have invested a lot of time in perfecting your scripting skills, and now that you have started using Ansible, it seems as there is no need to write scripts anymore. This can be true, but it doesn’t have to.
When you are building your “scripts” (let’s say that playbooks are a kind of scripts) using Ansible modules, sometimes you need to bypass some steps because there aren’t appropriate modules that could cover your needs in those steps. Since you are great in scripting, you could write a script that would satisfy your needs. But you could also write your own Ansible module. Why not? Just create a new script called “new_ansible_module” because Ansible module is nothing else than a script.
How to use scripting skills to build Ansible module?
You need to follow some rules, but with existing knowledge that won’t be a problem. I will show here one very simple Ansible module that will be used to put some content to a file. Additionally, there will be an option to uppercase or lowercase that content.
So, in our main function we will use AnsibleModule to handle incoming parameters and terminate the program. First you need to instantiate the module class like this:
# Read arguments module = AnsibleModule( argument_spec = dict( file_to_fill = dict(required=False,default="/tmp/file2fill"), file_content = dict(required=True), letter_case = dict(required=False,choices=['uppercase', 'lowercase']) ) ) |
We have three parameters, and you can see that parameters can be defined as required or not. Also default value can be set as well as possible choices where applicable.
After defining parameters, you need to parse them using module.params function.
file_to_fill = module.params['file_to_fill'] file_content = module.params['file_content'] letter_case = module.params['letter_case'] |
Then you need to implement some logic, this is very simple example with given parameters.
f=open(file_to_fill,"w") if letter_case == "uppercase": f.writelines(file_content.upper()) elif letter_case == "lowercase": f.writelines(file_content.lower()) else: f.writelines(file_content) |
So the whole code would be
#!/usr/bin/python import os import platform def main(): # Read arguments module = AnsibleModule( argument_spec = dict( file_to_fill = dict(required=False,default="/tmp/file2fill"), file_content = dict(required=True), letter_case = dict(required=False,choices=['uppercase', 'lowercase']) ) ) file_to_fill = module.params['file_to_fill'] file_content = module.params['file_content'] letter_case = module.params['letter_case'] f=open(file_to_fill,"w") if letter_case == "uppercase": f.writelines(file_content.upper()) elif letter_case == "lowercase": f.writelines(file_content.lower()) else: f.writelines(file_content) module.exit_json(changed=True, msg="File" + file_to_fill +" filled with following content" + file_content +"!") from ansible.module_utils.basic import AnsibleModule if __name__ == '__main__': main() |
If you save this simple code to /etc/ansible/library/fillFile.py, you will get your first Ansible module which gives you ability to fill a file with content you want. Also, here is an example of using this module from another task/playbook:
--- - name: Fill some file with your own module hosts: my_node1 gather_facts: no tasks: - fillFile: file_to_fill: /home/username/some_file.txt file_content: "Fill WITH THis..." letter_case: lowercase |
And you are done with your first module!
In this short example you have seen how to use scripting skills to build Ansible module. We have used Python since it’s the most appropriate for writing Ansible modules, but you could write modules using bash or powershell). Ansible proved to be very useful tool capable of many wonderful things. Instead of giving up your scripting career, extend current portfolio of available Ansible modules and help yourself and others.
— Photo by Markus Spiske on Unsplash
Falls Sie Fragen haben, sind wir nur einen Klick entfernt.