Custom hello-world Ansible Filter Plugin

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

Luca Berton

--

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 <luca@ansiblepilot.com>
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}

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

  1. Metadata Section:
  • name: Specifies the name of the filter plugin.
  • author: Indicates the author of the plugin along with their contact email.
  • version_added: Denotes the version in which the plugin was added.
  • short_description: Provides a brief overview of the plugin.
  • description: Offers more detailed information about the plugin, including its purpose.
  • positional: Defines the positional arguments that…

--

--

Luca Berton

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday