Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Event-Driven Ansible rulebook for automation

April 12, 2024
Nagesh Rathod
Related topics:
Automation and management
Related products:
Red Hat Ansible Automation Platform

    Event-Driven Ansible is an open source developer preview designed to flexibly enable teams to create event-driven automation scenarios across various IT domains (network, DevOps, security, CloudOps, infrastructure, and more).

    This article aims to teach you how to install the ansible-rulebook command-line interface (CLI) tools and use them when executing the rulebooks. Our rulebook will be used to create a conditional event, so whenever that event occurs, it will trigger and execute the playbook that we defined in action. This is the first step to event-driven development.

    ansible-rulebook CLI setup

    To install the ansible-rulebook CLI on your local system. Make sure you have installed all the components listed in the requirements section before starting the installation.

    Requirements

    • Python >= 3.8
    • Python 3 pip

    The following instructions will guide you through the installation of Java and other components. Depending on your Linux distribution, you can follow the following commands.

    On Red Hat Enterprise Linux (RHEL), CentOS, Fedora, or Rocky Linux-like systems:

    ​​​​​​​dnf --assumeyes install java-17-openjdk python3-pip
    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
    pip3 install ansible ansible-rulebook ansible-runner

    On Ubuntu or Debian systems:

    ​​​​​​​apt-get --assume-yes install openjdk-17-jdk python3-pip
    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
    export PATH=$PATH:~/.local/bin
    pip3 install ansible ansible-rulebook ansible-runner

    After installation of ansible, ansible-runner and ansible-rulebook, we have to install the required collection so we can execute the rulebook successfully. To install the following collection by using ansible-galaxy:

    ​​​​​​​ansible-galaxy collection install ansible.eda

    Once the collection is installed, a couple more dependencies are still left. For those, use the below command to install them:

    ansible-playbook -i localhost, -c local ansible.eda.install_rulebook_cli

    Check ansible-rulebook CLI successfully installed.

    ​​​​​​​$ ansible-rulebook
    usage: ansible-rulebook [-h] [--rulebook RULEBOOK] [--vars VARS] [--env-vars ENV_VARS] [--debug] [--verbose] [--version] [--redis-host-name REDIS_HOST_NAME] [--redis-port REDIS_PORT] [-S SOURCE_DIR]  [-i INVENTORY] [--websocket-address WEBSOCKET_ADDRESS] [--id ID] [--worker] [--project-tarball PROJECT_TARBALL] [--controller-url CONTROLLER_URL] [--controller-token CONTROLLER_TOKEN]  [--print-events]

    Event-driven rulebook

    The rulebook, shown below, is a simple example of webhook based event-driven. In this rulebook we are going to listen on port 5000. When a request comes on port 5000, the rulebook gets triggered and runs as per logic written in the rule.

    This GitHub repository contains the following rulebooks and other files.

    ---
    - name: Listen for events on a webhook
      hosts: localhost
      ## Define our source for events
      sources:
        - ansible.eda.webhook:
            host: 0.0.0.0
            port: 5000
      ## Define the conditions we are looking for
      rules:
        - name: Say Hello
          condition: event.payload.message == "Ansible is super cool"
      ## Define the action we should take should the condition be met
          action:
            run_playbook:
              name: say-what.yml

    Create an Inventory.yml inventory file where we can define the host where we can launch the rulebook.

    localhost

    In the above rulebook, you might notice that we have defined one playbook in the action section. The event condition must be true. In that playbook,  you can define any task which you want to run on. 

    say-what.yml
    ---
    - name: say thanks
      hosts: localhost
      gather_facts: false
      tasks:
        - debug:
            msg: "Thank you, {{ event.sender | default('my friend') }}!"

    We are ready to execute the rulebook using ansible-rulebook.

    ansible-rulebook --rulebook hit_try.yaml -i inventory.yaml --verbose
    2023-02-22 17:39:02,332 - ansible_rulebook.app - INFO - Starting sources
    2023-02-22 17:39:02,332 - ansible_rulebook.app - INFO - Starting rules
    2023-02-22 17:39:02,332 - ansible_rulebook.engine - INFO - run_ruleset
    2023-02-22 17:39:02,744 - ansible_rulebook.engine - INFO - ruleset define: {"name": "Listen for events on a webhook", "hosts": ["localhost"], "sources": [{"EventSource": {"name": "ansible.eda.webhook", "source_name": "ansible.eda.webhook", "source_args": {"host": "0.0.0.0", "port": 5000},say-what.yml

    Now the rulebook started listening on port 5000. 

    NOTE: After execution of the rulebook, make sure you don’t interrupt the terminal. Switch to the second tab.

    Testing

    Open the other terminal and execute the following command.

    curl -H 'Content-Type: application/json' -d "{\"message\": \"Ansible is alright\"}" 127.0.0.1:5000/endpoint

    After executing the first command check back the terminal 1 you will notice nothing is happening. The reason is the conditions defined in the playbook are not matched. 

    Go back to terminal 2 again, hit the curl command mentioned below and check the logs or results of the rulebook on terminal 1.

    curl -H 'Content-Type: application/json' -d "{\"message\": \"Ansible is super cool\"}" 127.0.0.1:5000/endpoint
    2023-02-22 17:39:36,818 - ansible_rulebook.builtin - INFO - ruleset: Listen for events on a webhook, rule: Say Hello
    2023-02-22 17:39:36,818 - ansible_rulebook.builtin - INFO - Calling Ansible runner
    
    PLAY [say thanks] **************************************************************
    
    TASK [debug] *******************************************************************
    ok: [localhost] => {
        "msg": "Thank you, my friend!"
    }
    
    PLAY RECAP *********************************************************************
    localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

    Summary

    In this article, we learned about how to install the ansible-rulebook CLI on a local machine and executed one sample rulebook that listens to the events on port 5000. When someone hits a request on that port, the rulebook will trigger but unless the condition is not true it will not execute the playbook from the action.

    Get started with the Ansible Automation Platform by exploring interactive labs. You can download Ansible Automation Platform for personal use at no cost. ​​​​​​​

    Related Posts

    • How to install Ansible Tower on Red Hat OpenShift

    • How to install Red Hat Ansible Automation Platform on RHEL 9

    • Automate your SSO with Ansible and Keycloak

    • Introducing Ansible Molecule with Ansible Automation Platform

    • 5 examples of security automation with Ansible

    • Enhance Ansible development experience with Lightspeed

    Recent Posts

    • Introducing virtualization platform autopilot

    • Integrate zero trust workload identity manager with Red Hat OpenShift GitOps

    • Best Practice Configuration and Tuning for Linux and Windows VMs

    • Red Hat UBI 8 builders have been promoted to the Paketo Buildpacks organization

    • Using eBPF in Red Hat products

    What’s up next?

    Read An IT executive's guide to automation to discover the benefits of a long-term transformative automation strategy, explore automation adoption best practices, and more.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility