Stage 4 · Provision
Testing, AWX & CI/CD
Event-Driven Ansible
Rulebooks reacting to alerts for self-healing automation.
What Is Event-Driven Ansible?
Event-Driven Ansible (EDA) watches for events — alerts, webhooks, log entries, API calls — and triggers automated responses based on rules. Instead of polling for problems, EDA reacts to them in real time.
EDA Rulebooks
---
- name: Restart service on alert
hosts: localhost
sources:
- name: webhook_alert
webhook:
host: 0.0.0.0
port: 5000
payload_events:
- key: alert_type
value: service_down
rules:
- name: Service down alert
condition: event.alert_type == "service_down"
action:
run_playbook:
name: playbooks/restart-service.yml
extra_vars:
service_name: "{{ event.service_name }}"
affected_host: "{{ event.host }}"Rulebooks define event sources, conditions, and actions. When an event matches a condition, the action executes automatically.
Event Sources
| Source | Type | Use Case |
|---|---|---|
| webhook | Pull | Prometheus Alertmanager, Grafana alerts |
| kafka | Pull | High-throughput event streams |
| prometheus | Pull | Prometheus alert rules |
| cloudwatch | Pull | AWS CloudWatch alarms |
| azure_service_bus | Pull | Azure event streams |
| file | Pull | Log file monitoring |
| range | Pull | Scheduled polling of APIs |
EDA Actions
# Run a playbook
rules:
- name: Restart on alert
condition: event.type == "service_down"
action:
run_playbook:
name: playbooks/restart.yml
# Run a module
rules:
- name: Notify on alert
condition: event.severity == "critical"
action:
run_module:
name: ansible.builtin.debug
module_args:
msg: "Critical alert: {{ event.message }}"
# Send to a webhook
rules:
- name: Forward alert
condition: event.type == "alert"
action:
post_webhook:
url: "https://slack.example.com/webhook"
headers:
Content-Type: "application/json"
payload:
text: "Alert: {{ event.message }}"EDA supports running playbooks, modules, or webhook calls as actions. Choose the right action for each use case.
Self-Healing Patterns
---
- name: Self-healing automation
hosts: localhost
sources:
- name: alertmanager
webhook:
host: 0.0.0.0
port: 5001
rules:
- name: Restart failed service
condition: event.alertname == "ServiceDown"
action:
run_playbook:
name: playbooks/restart-service.yml
extra_vars:
service_name: "{{ event.labels.service }}"
target_host: "{{ event.labels.instance }}"
- name: Scale up on high CPU
condition: event.alertname == "HighCPU" and event.labels.severity == "warning"
action:
run_playbook:
name: playbooks/scale-instance.yml
extra_vars:
instance_group: "{{ event.labels.group }}"
- name: Rollback on deploy failure
condition: event.alertname == "DeployFailed"
action:
run_playbook:
name: playbooks/rollback.yml
extra_vars:
version: "{{ event.annotations.previous_version }}"Self-healing automation detects problems and fixes them automatically. The system recovers without human intervention.
EDA transforms Ansible from a push-based tool into a reactive automation platform. Combined with monitoring, it creates self-healing infrastructure.
Automated remediation can cause cascading failures if the root cause is misidentified. Start with notifications, then add automated fixes for well-understood problems only.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.