Custom Ansible Plugins and Modules for Automation

Task Overview

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.

Task Objectives

Task Architecture

The solution consists of the following components:

  1. Custom Ansible Module:
  2. Custom Ansible Plugin:
  3. Playbooks & Roles:
  4. Testing Framework:
  5. Publishing & Documentation:

Technology Stack

Implementation Steps

Step 1: Setup Ansible Environment

Install 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

Step 2: Develop Custom Ansible Module

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

Step 3: Develop Custom Ansible Plugin

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
        }

Step 4: Create an Ansible Playbook

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 }}"

Step 5: Testing the Module & Plugin

Run the module locally:

ansible-playbook test_custom_module.yml

Run unit tests:

ansible-test sanity --python 3.8

Test with Molecule:

molecule test

Step 6: Publish to Ansible Galaxy

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

Step 7: Documentation & Maintenance

Expected Outcomes

Future Enhancements

This project will showcase expertise in Ansible automation and provide a valuable contribution to the DevOps community.