Ansible: is an open-source software for configuration management, provisioning and application deployment, it comes under the Infrastructure as a code, which means by writing a code we can create or deploy our infrastructure on any of the platforms whether its Cloud-like AWS, Azure or hypervisors etc.
Ansible Modules: is reusable & standalone code, its main thing is to perform a certain task on the managed node or target node. Ansible module can be written in any language which can output JSON to standard output but the most used and recommended way to create a custom module using python. Because Ansible is written in python have great support working with JSON data.
Steps to be involved in module creation using Python:
In this example, I have created a custom module to install the packages on Linux OS which uses yum as a package installer.
#!/usr/bin/python from ansible.module_utils.basic import AnsibleModule def install_package(module, package_name): cmd = "yum install -y " + package_name rc, stdout, stderr = module.run_command(cmd, check_rc=True) return stdout def main(): module = AnsibleModule( argument_spec=dict( package_name=dict(required=True, type='str'), ) ) package_name = module.params['package_name'] if package_name is None: module.fail_json(msg='Please provide package name') result = install_package(module, package_name) module.exit_json(changed=True, msg=result) if __name__ == '__main__': main()
module_utilsthe library is imported to provide access to the
AnsibleModuleclass & it uses to handle module arguments, inputs, outputs, and errors
install_packagethe function takes in the
moduleobject and the name of the package to install as arguments. It uses the
run_commandmethod to execute the
yumcommand and install the package.
AnsibleModuleobject is created with the required input parameters. The
install_packagefunction is called with the package name, and the output is returned in the
fail_jsonis used to handle module failures, when fail_json is called it throws an error in JSON format & exits the module. Like in the above example, we are checking whether the package name is present or not. If the user doesn’t provide the package name it will throw an error.
exit_jsonthe method is called to return the result of the module execution.
For example, use the above module to install the
httpd package, you would include the following task in your playbook:
- hosts: localhost tasks: - name: install httpd yum_install: package_name: httpd
Best Practices for custom ansible module creation:
Creating custom ansible modules rewarding and powerful way to automate your infrastructure & is the most powerful way to extend ansible functionality and automate more complex tasks in this way we can also contribute to open source as for some of the tasks there are no such modules are present. By following best practices of module creation you can create high-quality and easy-to-use ansible modules. With the right approach you can unlock the full potential of ansible & so go ahead and give it a try to create your own custom ansible modules.
Thank you for taking the time to read this blog till the end and learn more about creating custom Ansible modules. I hope that you found this information helpful and that it inspires you to explore the possibilities of Ansible automation. Don’t hesitate to reach out if you have any questions or feedback, and be sure to share your experiences and insights with the community.
Thanks again, and happy automating!
Disclaimer: The views expressed and the content shared are those of the author and do not reflect the views of the author’s employer or techbeatly platform.
Leave a Reply