Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Ansible Difficulty: Intermediate ClaudeChatGPTCursor

Ansible Custom Filter Plugin Authoring Prompt

Design a custom Jinja2 filter plugin that encapsulates repeated data transformations so playbooks stay readable and the logic is unit-testable.

Target user
Engineers copy-pasting the same gnarly Jinja2 expression across roles and wanting one tested, named filter instead
Difficulty
Intermediate
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior Ansible engineer who moves repeated, unreadable Jinja2 expressions into a proper filter plugin, because a named filter is testable, reusable, and self-documenting where an inline `{{ ... | complex | chain }}` is none of those.

I will describe a transformation my playbooks keep doing inline. Draft a filter plugin that encapsulates it.

Steps:

1. **FilterModule structure**: implement the `filters()` method returning a dict mapping filter names to callables, placed under `filter_plugins/` or a collection's `plugins/filter/`.
2. **Naming**: give the filter a clear, verb-like name and, in a collection, document the FQCN (`namespace.collection.filter_name`).
3. **Input validation**: check argument types up front and raise `AnsibleFilterError` with an actionable message on bad input instead of allowing an opaque Python exception.
4. **Purity**: keep the function deterministic and side-effect-free so templating stays predictable and idempotent.
5. **Edge cases**: handle empty, None, and unexpected-shape inputs explicitly.
6. **Tests**: provide pytest cases (or `ansible-test units`) covering the happy path and each edge case, plus a tiny playbook that exercises the filter with `debug`.

Fill in:
- The inline expression you keep repeating: [PASTE]
- Input shape and output shape: [DESCRIBE]
- Edge cases that matter: [LIST]

Output format: the filter plugin Python, the FQCN/usage snippet, unit tests, and a debug-task playbook proving the filter's output.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

Inline Jinja2 chains are where playbooks quietly rot: the same fragile expression gets copied into five roles, and none of it is tested. Promoting it to a filter plugin gives the transformation a name, a home, and a test suite. This prompt drives that migration deliberately, starting from the exact expression you keep repeating and ending with pytest cases that pin the behavior down.

The safety framing matters more than it looks. Filters execute at template time against whatever variable data flows in, so unvalidated input surfaces as a raw traceback in the middle of a run instead of a clear error. Insisting on AnsibleFilterError for bad input and strict purity — no network calls, no file writes — keeps the filter a transformation rather than a hidden, non-idempotent side effect masquerading as templating.

Related prompts

More Ansible prompts & error guides

Browse every Ansible prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.