Event-Driven Ansible: Automating Incident Response Without Polling
Event-Driven Ansible automates incident response by reacting to events in real time.
20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.
- ✓Ansible 2.15+ installed. Basic familiarity with the monitoring tool you want to integrate (Prometheus, webhooks, etc.).
Event-Driven Ansible lets you define rules that listen for events (e.g., a service goes down, a file changes) and automatically run Ansible playbooks. You set up an event source (like a webhook or message queue), write a rulebook that maps events to actions, and the engine handles the rest.
Imagine you have a butler who only acts when you ring a bell. Instead of checking every hour if you need something (polling), the butler waits by the bell and springs into action the moment it rings. Event-Driven Ansible is that butler for your infrastructure: it listens for specific signals (events) and runs the appropriate playbook immediately.
Most infrastructure automation is reactive polling. You set a cron job to run every 5 minutes, check if something is wrong, and fix it. That's wasteful and slow. Event-Driven Ansible flips the model: it listens for events and reacts instantly. No more wasted cycles checking for problems that don't exist. By the end of this article, you'll be able to set up an event-driven rule that automatically restarts a failed service, scales a deployment based on load, or even triggers a rollback when a deployment fails — all without a single cron job.
Why Polling Is a Waste of Resources
Cron jobs are the duct tape of automation. They run regardless of whether anything needs to happen. In production, that means thousands of unnecessary API calls, wasted CPU cycles, and delayed responses. Event-Driven Ansible eliminates the polling tax. Instead of asking 'Is it broken yet?' every 5 minutes, it waits for the answer to come to it. The result: faster reaction times, lower resource usage, and simpler code. No more managing cron schedules or worrying about overlapping runs.
Setting Up Your First Event Source
Event sources are the ears of Event-Driven Ansible. They can be webhooks, message queues, cloud events, or monitoring alerts. The most common production setup is a webhook endpoint that receives POST requests from your monitoring system or CI/CD pipeline. Here's how to configure a simple webhook source that listens for JSON payloads on port 5000.
{{ lookup('env', 'WEBHOOK_SECRET') }}.Writing Rulebooks: The Brain of the Operation
Rulebooks define the logic: when an event arrives, what condition must be true, and what action to take. Conditions can check event fields, nested values, or even run Jinja2 expressions. Actions typically run a playbook, but can also run a module directly or send a notification. Keep rulebooks simple — one rule per logical action. Complex conditions lead to debugging nightmares.
enabled: false to disable rules without deleting them.Running the EDA Controller in Production
The EDA controller is a long-running process that listens for events. In production, run it as a systemd service or in a container with restart policies. Never run it as a background job in a terminal — it will die when you log out. Use a dedicated user with minimal permissions. Monitor the controller itself: if it goes down, your automation goes deaf.
Handling Event Deduplication and Idempotency
Events can arrive multiple times (e.g., retries from monitoring). Your playbooks must be idempotent — running them twice should have the same effect as running them once. Use Ansible's state parameters and creates/removes to ensure idempotency. For deduplication, track event IDs in a Redis cache and skip already-processed events.
Scaling Event-Driven Ansible Horizontally
A single EDA controller can handle hundreds of events per second. Beyond that, run multiple controllers behind a load balancer, each with the same rulebook. Use a shared event source (like Kafka or RabbitMQ) that partitions events by a key (e.g., hostname) to ensure related events go to the same controller. This avoids race conditions on the same host.
When Not to Use Event-Driven Ansible
Event-Driven Ansible is overkill for simple scheduled tasks like daily backups or log rotation. Stick with cron for those. Also avoid it for high-frequency events (thousands per second) — the rulebook evaluation overhead adds latency. Use a stream processor like Flink for that. Finally, don't use it for critical safety systems (e.g., emergency shutdown) where you need guaranteed delivery and ordering — use a dedicated industrial controller.
The 3AM Database Failover That Never Happened
- Always validate the event delivery path end-to-end, including certificate expiry and network timeouts.
journalctl -u eda-controller -f. 2. Verify event source is reachable: curl -X POST http://localhost:5000/endpoint -d '{"test":true}'. 3. Check rulebook syntax: ansible-rulebook --rulebook rules.yml --syntax-check.ssh user@host. 3. Ensure the event payload includes the correct hostname.run_playbook with run_once: true if applicable.pip3 list | grep ansible-rulebookwhich ansible-rulebook| File | Command / Code | Purpose |
|---|---|---|
| polling_vs_event.yml | - name: React to service failure | Why Polling Is a Waste of Resources |
| webhook_source.yml | - name: Webhook listener for deployment events | Setting Up Your First Event Source |
| rulebook_example.yml | - name: Production incident response | Writing Rulebooks |
| eda_service_setup.sh | sudo cat > /etc/systemd/system/eda-controller.service << 'EOF' | Running the EDA Controller in Production |
| dedup_playbook.yml | - name: Restart service with dedup | Handling Event Deduplication and Idempotency |
| kafka_source.yml | - name: Kafka event source for scaled setup | Scaling Event-Driven Ansible Horizontally |
Key takeaways
Common mistakes to avoid
3 patternsUsing hosts: all in EDA-triggered playbooks
hosts: "{{ event.payload.ansible_host }}". Broad playbooks can impact the entire fleet.Enabling auto-remediation without dry-run testing
dry_run mode for at least a week. Validate behavior before enabling active remediation.Running EDA across environments without isolation
Interview Questions on This Topic
How does Event-Driven Ansible handle duplicate events? What's your strategy to prevent duplicate actions?
Frequently Asked Questions
20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.
That's Ansible. Mark it forged?
3 min read · try the examples if you haven't