Ansible plugins are modular pieces of code that extend and enhance Ansible’s core functionality. They allow users to customize and optimize workflows for specific requirements. This article explains what Ansible plugins are, their types, and how to use them effectively.

What Are Ansible Plugins?

Ansible plugins are Python-based extensions that modify or add capabilities to Ansible’s automation engine. They provide additional functionality without altering the core Ansible codebase, making them highly flexible and reusable.

Key Features:

  • Extensibility: Add new behaviors to Ansible.
  • Reusability: Share plugins across projects or teams.
  • Customization: Tailor functionality to specific needs.

Types of Ansible Plugins

Ansible supports several plugin types, each serving a distinct purpose:

1. Action Plugins

Modify or enhance the behavior of modules when executed.

  • Example: Add custom logic to module execution.

2. Lookup Plugins

Fetch data from external sources and pass it into playbooks.

  • Example: Retrieve secrets from a vault:
    vars:
      secret: "{{ lookup('aws_secretsmanager', 'my_secret') }}"
    

3. Filter Plugins

Transform data or variables in templates or playbooks.

  • Example: Convert text to uppercase:
    vars:
      uppercase_text: "{{ 'hello' | upper }}"
    

4. Connection Plugins

Define how Ansible connects to target nodes (e.g., SSH, WinRM).

  • Example: Use a custom connection method for specialized devices.

5. Strategy Plugins

Control the execution flow of tasks in a play.

  • Built-in strategies include linear, free, and debug.

6. Callback Plugins

Customize output or perform actions during playbook execution.

  • Example: Send notifications to Slack after task completion.

7. Inventory Plugins

Dynamically generate inventory from external sources (e.g., AWS, GCP).

  • Example:
    plugin: aws_ec2
    regions:
      - us-east-1
    

8. Vars Plugins

Inject additional variables into playbooks dynamically.

  • Example: Load variables based on host properties.

Where Are Plugins Stored?

Built-In Plugins

Ansible’s default plugins are stored in the system directory:

/usr/share/ansible/plugins/

Custom Plugins

Custom plugins can be stored in:

  1. Project-Specific Directory:

    ./plugins/<plugin_type>/
    
  2. Global Directory:

    ~/.ansible/plugins/<plugin_type>/
    
  3. Specified Paths: Define custom plugin paths in the ansible.cfg file:

    [defaults]
    plugin_paths = /path/to/custom/plugins
    

Creating Custom Plugins

Here’s an example of a custom filter plugin to reverse strings:

  1. Create a directory for custom plugins:

    ./plugins/filter/
    
  2. Write the plugin in Python:

    # plugins/filter/reverse.py
    def reverse_string(value):
        return value[::-1]
    
    class FilterModule(object):
        def filters(self):
            return {
                'reverse': reverse_string
            }
    
  3. Use the plugin in a playbook:

    vars:
      reversed_text: "{{ 'hello' | reverse }}"
    
  4. Execute the playbook to see the plugin in action.

Best Practices for Using Plugins

  1. Use Collections: Distribute and manage plugins using Ansible Collections for better organization.

  2. Test Custom Plugins: Validate plugins in a controlled environment to ensure reliability.

  3. Document Plugins: Provide clear documentation for custom plugins, including usage examples.

  4. Optimize for Performance: Ensure plugins do not introduce significant overhead to playbook execution.

Conclusion

Ansible plugins are a powerful way to extend Ansible’s functionality and customize workflows to meet unique requirements. With various built-in types and support for custom development, plugins empower users to optimize their automation tasks effectively.

Learn More About Ansible Plugins

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Explore more about creating and using plugins in Ansible by Examples.

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Support this project by contributing today.

Patreon Buy me a Pizza