Ansible Event Driven Automation

Traditional playbooks start when a person or a schedule triggers them. Event Driven Ansible (EDA) reverses the idea: the automation listens for signals and reacts on its own. A smoke detector offers a good comparison. A person does not press a button to start the alarm, because the detector senses smoke and responds immediately. EDA watches monitoring tools, webhooks, and message queues, and launches playbooks the moment a matching event arrives.

Core Concepts

TermMeaning
SourcePlugin that receives events, such as a webhook or a Kafka topic
EventA piece of data describing something that happened
ConditionA test that decides whether an event matters
ActionThe response, such as running a playbook or a job template
RuleA pairing of conditions with actions
RulebookA YAML file that holds sources and rules

Diagram: From Event to Fix

  ┌───────────────┐   alert    ┌─────────────────┐
  │ Monitoring    │──────────▶│     Source      │
  │ tool          │            │ (webhook plugin)│
  └───────────────┘            └────────┬────────┘
                                        │ event
                                        ▼
                               ┌─────────────────┐
                               │   Rulebook      │
                               │ condition match?│
                               └───┬─────────┬───┘
                                yes│         │no
                                   ▼         ▼
                          ┌──────────────┐  ignore
                          │ run_playbook │
                          │ restart_app  │
                          └──────┬───────┘
                                 ▼
                          ┌──────────────┐
                          │ Server fixed │
                          └──────────────┘

Install Ansible Rulebook

python3 -m pip install ansible-rulebook ansible-runner
ansible-rulebook --version

The tool needs Java 17 or later, because the rules engine runs on the Drools library.

Write a Rulebook

This rulebook listens on a web address and restarts a service when an alert reports high memory.

---
- name: React to monitoring alerts
  hosts: all
  sources:
    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 5000

  rules:
    - name: Restart app on high memory
      condition: event.payload.alert == "high_memory" and event.payload.service == "shop"
      action:
        run_playbook:
          name: restart_shop.yml

    - name: Log every other alert
      condition: event.payload.alert is defined
      action:
        debug:
          msg: "Alert received: {{ event.payload.alert }}"

The Response Playbook

# restart_shop.yml
- hosts: webservers
  become: true
  tasks:
    - name: Restart the shop service
      ansible.builtin.service:
        name: shop
        state: restarted

Start the Rulebook and Send a Test Event

ansible-rulebook --rulebook alerts.yml -i inventory.ini --verbose

curl -X POST http://localhost:5000/endpoint \
  -H "Content-Type: application/json" \
  -d '{"alert": "high_memory", "service": "shop"}'

The terminal running the rulebook prints a message that the rule matched, and the playbook starts.

Popular Event Sources

  • ansible.eda.webhook accepts HTTP posts from any tool
  • ansible.eda.alertmanager receives Prometheus alerts
  • ansible.eda.kafka reads messages from a Kafka topic
  • ansible.eda.url_check polls a web address and reports status changes
  • ansible.eda.file_watch reports file changes

Available Actions

ActionResult
run_playbookRuns a local playbook
run_job_templateStarts a template in AWX or Automation Controller
run_moduleRuns one module
set_factStores data for later rules
post_eventCreates a new event for other rules
debugPrints information

Design Tips

  • Begin with low risk responses such as ticket creation and notifications
  • Add throttling so repeated alerts do not trigger endless restarts
  • Keep every response playbook idempotent
  • Log each action for later review
  • Require human approval for destructive actions

Try This

Build the sample rulebook with only the debug action. Start it and send three test events with different alert names through curl. Watch which rules fire. Add a throttle section to the main rule and send the same event twice within a minute to observe the filtering.

Summary

Event Driven Ansible listens for events and responds through rulebooks. Sources receive data from monitoring tools and message systems, conditions filter events, and actions run playbooks or modules. Start with safe responses, add throttling, and keep playbooks idempotent to build self-healing infrastructure that reacts in seconds.