Ansible Callback Plugin Authoring Prompt
Draft a custom callback plugin that hooks play/task events for profiling, structured logging, or notifications without changing any playbook.
- Target user
- Engineers who need run telemetry, timing, or external notifications from Ansible without editing every play
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior Ansible engineer who reaches for a callback plugin instead of hacking notifications into tasks, because a callback sees every play, task, and result centrally and stays out of the playbook logic. You know callbacks are the correct seam for timing, structured logs, and external notifications. I will describe what I need to observe or emit from Ansible runs. Draft a callback plugin (subclassing `CallbackBase`) that does it cleanly. Steps: 1. **Type and activation**: choose the callback type (`stdout`, `notification`, or `aggregate`), set `CALLBACK_VERSION`, `CALLBACK_TYPE`, and `CALLBACK_NAME`, and explain how it is enabled (`callbacks_enabled` in ansible.cfg for notification/aggregate types, or replacing the stdout callback). 2. **Event hooks**: implement only the hooks you need — `v2_playbook_on_start`, `v2_runner_on_ok`, `v2_runner_on_failed`, `v2_runner_on_unreachable`, `v2_playbook_on_stats` — and keep each one fast. 3. **Secret hygiene**: respect `no_log` and strip known-sensitive keys before emitting any result; never dump the raw `result._result`. 4. **Config via DOCUMENTATION**: declare options (endpoint, min-duration threshold, verbosity) in a `DOCUMENTATION` block and read them with `get_option` so config is validated. 5. **Non-blocking I/O**: for webhooks/APIs, batch or background the calls and swallow errors so a telemetry outage never fails the play. 6. **Verification**: show how to test it against a trivial playbook with `-vv` and confirm the emitted output/notification matches expectations. Fill in: - What you need to capture or emit: [timing / structured JSON logs / Slack / metrics] - Destination and auth: [DESCRIBE] - Threshold or filtering: [e.g. only tasks over 5s] Output format: the plugin Python with a documented options block, the ansible.cfg enablement snippet, and a verification plan using a sample playbook run.
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
A callback plugin is the right place for run telemetry precisely because it is decoupled from playbook logic: it observes every play and task from the outside instead of forcing you to bolt debug or notification tasks onto each role. This prompt anchors on choosing the correct callback type and enabling it properly, which is the step most home-grown callbacks get wrong — a notification callback silently does nothing until it is listed in callbacks_enabled.
The two hard rules — respect no_log and never block — are what separate a safe callback from a dangerous one. Callbacks see raw task results, which routinely contain tokens and passwords, so emitting result._result verbatim is a credential leak into your logging backend. And because a callback fires on every task event, any synchronous webhook turns a fast run into a crawl or a hang. By insisting on secret stripping and non-blocking, fail-open I/O, the prompt keeps observability from becoming an outage or a breach.
Related prompts
-
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.
-
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.
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.