Event-Driven automation for Ansible and a Kafka integration example
Alongside the general availability of the Ansible Automation Platform 2.3, Red Hat announced the Event Driven technology, a community project released by Red Hat.
Event-Driven Ansible, is now available as a developer preview.
Red Hat also announced plans to integrate Event-Driven Ansible into AAP (Ansible Automation Platform) as an integrated component in 2023. For more information on why event-driven automation matters for IT Teams, refer to:
https://www.ansible.com/use-cases/event-driven-automation
Event-Driven Ansible is designed for:
- Process events from external sources, like tools from partners.
- Determine the desired response to the event.
- Execute automated actions to address or remediate the event.
Event-Driven connects sources of events with desired actions using rulebooks.
Rulebooks define the event source and determine the action to take when a specific condition is detected. The detection is defined with a familiar “if-this-then-that” instruction, and the action can be like running a playbook or directly executing a module.
Like traditional Ansible Playbooks, rulebooks are also created in YAML.
The three main components of the rulebooks are:
- Source: the sources of events come from source plugins. These plugins define where to listen for events.
- Condition: the conditional statements in the rulebook allow matching criteria on which is necessary to have a response.
- Action: The action is the response once the condition has been met from the event source as trigger remediation, log a ticket for observation, or generate other events as the response.
In the example below, we will monitor kafka messages on a topic and respond to a specific one.
You can access and try this simple guided lab at the following link:
https://www.ansible.com/products/ansible-training#event-driven-automation
The figure shows the content of a rulebook called “kafka-example.yml”:
We can verify that the source is the kafka plugin. The source is pointing to a kafka host called “broker”
and to a topic called “eda-topic”.
The rule is looking for a message on that topic that matches the text “Ansible is cool” and the action is running the “say-what.yml” playbook.
So, the first step is to start the “ansible-rulebook” against the kafka rulebook with the following command:
ansible-rulebook –rules kafka-example.yml -i inventory.yml –verbose
After that, the “ansible-rulebook” is listening to the source:
Now, we can send a message using the running kafka client that will be a match for the conditions in the active rulebook:
{"message":"Ansible is cool"}
We can see that the message has been read from the topic, and the playbook is executed:
I hope this information was helpful.
References:
https://www.ansible.com/use-cases/event-driven-automation
https://www.ansible.com/blog/introducing-event-driven-ansible?hsLang=en-us
https://www.ansible.com/blog/getting-started-with-event-driven-ansible
https://github.com/ansible/ansible-rulebook
https://access.redhat.com/support/offerings/devpreview
https://www.redhat.com/en/topics/integration/what-is-apache-kafka