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
-
Ansible Custom Lookup Plugin Authoring Prompt
Draft a lookup plugin that pulls values from an external source at template time with correct terms/variables handling, error behavior, and caching.
-
Ansible Custom Action Plugin Authoring Prompt
Draft a custom action plugin that runs controller-side logic and dispatches to a module safely, with check-mode support and no secret leakage.
-
Ansible Dynamic Inventory Plugin Authoring Prompt
Draft a custom dynamic inventory plugin that builds hosts, groups, and vars from an external source, with caching and a verifiable graph output.
-
Ansible Custom Vars Plugin Authoring Prompt
Draft a custom Ansible vars plugin that injects variables from an external source at the right precedence, with caching and clear failure behavior.
More Ansible prompts & error guides
Browse every Ansible prompt and troubleshooting guide in one place.
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.