Event-Driven Ansible with Zabbix

Zabbix supports Event-Driven Ansible. This makes it possible to launch job_templates in Ansible Automation Platform based on triggers in Zabbix.

One use case is to update host events in Zabbix with facts from Ansible, which we will take a look at in this post.

Ansible Automation Platform

You will need various credentials, a decision environment container, a Git repository containing rulebooks, an Event-Stream with token authentication, a rulebook activation and a job_template.

The complete configuration needed for Event-Driven Ansible in Ansible Automation Platform is located here.

Decision Environment

This is the execution-environment.yml file I use to build the decision-environment container with ansible-builder:

---
version: 3
dependencies:
  galaxy:
    collections:
      - ansible.eda
  system:
    - "gcc [platform:rpm]"
    - "python3.11-devel [platform:rpm]"
    - "systemd-devel [platform:rpm]"
  python_interpreter:
     python_path: "/usr/bin/python3.11"

images:
  base_image:
    name: registry.redhat.io/ansible-automation-platform-25/de-minimal-rhel9:latest

options:
  package_manager_path: "/usr/bin/microdnf"

additional_build_steps:
  prepend_base:
    - "RUN $PYCMD -m pip install --upgrade pip setuptools wheel"
  append_final:
    - "RUN $PKGMGR clean all"

Rulebook

Rulebook used by Event-Stream:

---
- name: Zabbix rulebook for AAP with Event Stream.
  hosts: all
  sources:
    - ansible.eda.webhook:
        port: 5000

  rules:
    - name: Updates event in Zabbix with facts.
      condition: event.payload is defined
      action:
        run_job_template:
          name: Zabbix Event Update
          organization: Example
          job_args:
            extra_vars:
              target: "{{ event['payload']['host_host'] }}"
              event_id: "{{ event['payload']['event_id'] }}"

The host parameter is required, but not used for controller actions (eg: run_job_template, run_workflow_template).

The Event Payload used for extra_vars target and event_id is provided by the Event-Driven Ansible media type in Zabbix.

The host name in Zabbix must match the inventory_hostname in your Ansible inventory used by AAP.

Playbook

Playbook used by the job_template in AAP:

---
- name: Gather facts and update event in Zabbix.
  hosts: "{{ target }}"
  gather_facts: true
  tasks:

    - name: Update event with message.
      community.zabbix.zabbix_host_events_update:
        params:
          eventids: "{{ event_id }}"
          action: message
          msg: "{{ ansible_facts | ansible.builtin.to_yaml }}"
      delegate_to: zabbix-server
      vars:
        ansible_network_os: community.zabbix.zabbix
        ansible_connection: ansible.netcommon.httpapi
        ansible_httpapi_use_ssl: true
        ansible_httpapi_validate_certs: false

For extra_vars to work, you must select the “Prompt on launch” option for extra_vars in the job_template.

Event-stream

Use token authentication for the event-stream. Once the even-stream is created, it will generate a URL. This is the URL, along with the token, that Zabbix will use:

https://example.com/eda-event-streams/api/eda/v1/external_event_stream/1234/post/

Zabbix

You will need a media type, a service user and a trigger action.

Event-Driven Ansible integration with Zabbix is documented here.

Media type

I have altered the Event-Driven Ansible webhook provided by Zabbix to support token authentication with Event-Streams in Ansible Automation Platform:

https://github.com/jiholland/zabbix_event_driven_ansible

Go to Alerts, Media Types and import the Event-Driven Ansible webhook from the link above.

The EDA_WEBHOOK_TOKEN parameter provided by the Event-Driven Ansible media type can be configured as a macro at the host level in Zabbix.

Service User

Create a new user called EDA. Go to the Media tab and select type Event-Driven Ansible. The “Send to” parameter should be the URL generated by the Event-stream in AAP:

https://example.com/eda-event-streams/api/eda/v1/external_event_stream/1234/post/

Trigger actions

Go to Alerts, Actions and Trigger actions. Create a new action called Event-Driven Ansible. Select the EDA user under the Operations tab for the “Send to users” parameter. Add conditions of choice.

Conclusion

Host events in Zabbix should now be updated with facts gathered by Ansible in AAP. This essentially converts Ansible from a push to a pull architecture, which opens up a realm of new possibilities.


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *