This task focuses on developing, testing, and publishing custom Ansible modules and plugins to address niche automation use cases. The goal is to create a reusable and well-documented module that extends Ansible’s functionality.
The solution consists of the following components:
ansible.module_utils.basic
framework.ansible-test
) for unit testing.ansible-test
, MoleculeInstall Ansible, Python3, and required libraries:
pip install ansible ansible-lint molecule docker
Set up a virtual environment:
python3 -m venv ansible_env
source ansible_env/bin/activate
Create a directory structure:
mkdir -p ansible_custom_modules/library
cd ansible_custom_modules
Write a Python module (e.g., custom_user.py):
# ansible_custom_modules/library/custom_user.py
from ansible.module_utils.basic import AnsibleModule
def main():
module_args = dict(
username=dict(type='str', required=True),
state=dict(type='str', choices=['present', 'absent'], default='present')
)
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
username = module.params['username']
state = module.params['state']
if state == 'present':
result = {"message": f"User {username} created successfully."}
else:
result = {"message": f"User {username} removed successfully."}
module.exit_json(changed=True, result=result)
if __name__ == '__main__':
main()
Add an executable permission:
chmod +x library/custom_user.py
Create a filter plugin (custom_filters.py):
# ansible_custom_modules/plugins/filter/custom_filters.py
from ansible.errors import AnsibleFilterError
def uppercase(value):
if not isinstance(value, str):
raise AnsibleFilterError("Input must be a string")
return value.upper()
class FilterModule(object):
def filters(self):
return {
'uppercase': uppercase
}
Playbook using the custom module:
- name: Test Custom Ansible Module
hosts: localhost
tasks:
- name: Create user
custom_user:
username: "john_doe"
state: present
register: result
- debug:
msg: "{{ result.result }}"
Playbook using the custom plugin:
- name: Test Custom Filter Plugin
hosts: localhost
tasks:
- name: Convert text to uppercase
debug:
msg: "{{ 'hello world' | uppercase }}"
Run the module locally:
ansible-playbook test_custom_module.yml
Run unit tests:
ansible-test sanity --python 3.8
Test with Molecule:
molecule test
Create a meta/main.yml file for the role:
galaxy_info:
author: Venu Annappureddy
description: Custom Ansible Module for Managing Users
license: MIT
min_ansible_version: "2.9"
platforms:
- name: Ubuntu
versions:
- all
Publish to Ansible Galaxy:
ansible-galaxy role import username/ansible_custom_modules
If using GitHub, push to a repository:
git init
git add .
git commit -m "Initial custom module commit"
git branch -M main
git remote add origin https://github.com/username/ansible_custom_modules.git
git push -u origin main
# Custom Ansible Module: custom_user ## Description: This module creates or removes users on a Linux system. ## Usage: ```yaml - name: Create a user custom_user: username: "john_doe" state: present
This project will showcase expertise in Ansible automation and provide a valuable contribution to the DevOps community.