Ansible Pilot

Custom hello-world Ansible Filter Plugin

Unveiling Ansible’s “hello-world” Filter Plugin in foo.bar: A Glimpse into Personalized Automation

December 4, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

Introduction

In the world of automation and configuration management, Ansible plays a pivotal role in simplifying complex tasks and orchestrating diverse systems. One of the key features that makes Ansible highly versatile is the ability to extend its functionality through plugins. In this article, we’ll dive into the “hello-world" filter plugin written in Python for Ansible within the foo.bar namespace.

Understanding the Basics

The “hello-world” filter plugin is a simple yet illustrative example of a filter plugin in Ansible. The purpose of this plugin is to generate a personalized greeting message, taking a name as input and returning a “Hello, [name]” message.

"""A hello-world filter plugin in foo.bar collection."""

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = r"""
  name: hello-world
  author: Luca Berton <[email protected]>
  version_added: "1.0.0"  # same as collection version
  short_description: hello world
  description:
      - This filter returns the hello from a name input.
  positional: _input
  options:
    _input:
      description: Name to show.
      type: raw
      required: true
"""

def _hello_world(name):
    """Returns Hello message."""
    return "Hello, " + name

class FilterModule:
    """filter plugin."""

    def filters(self):
        """filter plugin."""
        return {"hello_world": _hello_world}

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

Let’s break down the key components of the provided Python script, hello_world.py:

  1. Metadata Section:
  1. Filter Implementation:

Practical Use

To utilize this filter plugin in an Ansible playbook, you can invoke it using the hello_world filter with a specific name as input. Here’s an example:

---
- name: Example Playbook
  hosts: localhost
  tasks:
    - name: Display Hello World Message
      debug:
        msg: "{{ 'Your Name' | hello_world }}"

Replace ‘Your Name’ with the desired name, and when you run this playbook, it will output a message like:

TASK [Display Hello World Message] ********************************************
ok: [localhost] => {
    "msg": "Hello, Your Name"
}

Conclusion

Understanding filter plugins in Ansible is crucial for extending the capabilities of your automation scripts. The “hello-world” filter plugin we explored provides a foundation for creating more complex filters tailored to specific use cases. By customizing and expanding upon this example, you can enhance your Ansible playbooks with personalized and dynamic content. Happy automating!

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

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

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

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases