Ansible Custom Filter Plugin Prompt
Design and review a custom Ansible Jinja2 filter plugin in Python — FilterModule contract, edge-case handling, and unit tests.
- Target user
- Ansible engineers extending templating with reusable Python filters
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior infrastructure-as-code engineer who has shipped reusable Ansible collections and writes filter plugins that survive real inventory data.
I will provide:
- The transformation the filter must perform (input shape to output shape)
- The current filter code or a plain-language description
- Where it lives (playbook-local `filter_plugins/` or a collection's `plugins/filter/`)
Your job:
1. **Confirm the contract** — verify the plugin exposes a `FilterModule` class with a `filters()` method returning a dict of name to callable, and that each callable is a pure function.
2. **Harden the implementation** — add type checks, raise `AnsibleFilterError` (not bare exceptions) on bad input, and avoid mutating the argument in place.
3. **Handle edge cases** — empty lists, `None`, non-ASCII strings, and undefined variables; decide explicit behavior for each rather than letting tracebacks leak.
4. **Name and namespace correctly** — recommend a collection-qualified name (`my_org.my_coll.my_filter`) and flag collisions with builtin filters.
5. **Write unit tests** — provide pytest cases covering happy path, empty input, and error path, asserting the `AnsibleFilterError` message.
6. **Show usage** — give a playbook snippet using the filter in a template and via `{{ var | filter }}`.
7. **Document** — add a short docstring and a usage example block.
Output as: (a) annotated Python plugin, (b) pytest file, (c) playbook usage snippet, (d) a bullet list of edge cases handled.
Flag any filter that swallows errors silently or returns a different type depending on input — those cause silent data corruption downstream.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Ansible Custom Lookup Plugin Prompt
Build a custom Ansible lookup plugin that fetches data from an external source at templating time, with caching and error handling.
-
Ansible Runner Python Embedding Prompt
Embed Ansible into a Python service using the ansible-runner library — private data dir, event callbacks, artifacts, and status handling.
-
Ansible Module Argument Spec Validation Prompt
Design and review the argument_spec for a custom Ansible module so inputs are validated, mutually exclusive options are enforced, no_log secrets are protected, and check mode behaves correctly.
-
Ansible NX-OS and IOS Network Modules Prompt
Write idempotent Ansible playbooks for Cisco NX-OS and IOS devices using resource modules, with safe config replace and rollback.
More Infrastructure as Code prompts & error guides
Browse every Infrastructure as Code 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.