Event Driven Ansible and target hosts in rulebooks

133 views
Skip to first unread message

Michael Schwartzkopff

unread,
Mar 6, 2024, 11:23:51 AM3/6/24
to ansible...@googlegroups.com
Hi,

I want to write a rulebook that triggers a playbook run on some specific event. Basically I want to configure an interface of a router. My event passes all the interface data in the event.payload.data.

The core part of the runbook looks like:

      action:
        run_playbook:
          name: set_interface.yml
          extra_vars:
            interface: "{{ event.payload.data }}"

The rule is triggered and the playbook starts to run. BUT the playbook does not know which host to target. The output is:

2024-03-06 15:42:52,146 - ansible_rulebook.action.run_playbook - DEBUG - Calling Ansible runner
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

PLAY [Test playbook] ***********************************************************
skipping: no hosts matched

How can I tell the playbook from the action, which host to target?

Michael.

Ramesh

unread,
Mar 8, 2024, 7:01:58 AM3/8/24
to ansible...@googlegroups.com
Ideally Payload Data should contain HOSTNAME of the Router. Recently we have implemented many projects for different scenarios, here is my understanding and experience.


If event source that triggers events does not contain any data related to the hostname for the playbook to execute on, the playbook runs on all inventory hosts, as it is written to run on all hosts, as shown below

---
hosts: all
.......

So, the EDA will look for path event.payload.hostname. however our event look like below 

2023-12-02 15:43:18,340 - ansible_rulebook.rule_set_runner - INFO - substitute_variables
[{'name': 'recieved-event.yml'}]
[{
  'events': {
    'm_0': {
      'payload': {
        'message': 'LowDiskSpace',
        'hostname': 'vbrhelhyb01',
        'architecture': 'x86_64',
      },

So, we need to use this filter ansible.eda.insert_hosts_to_meta to convert above hostname to required format and include in event.meta because the webhook plugin is putting server name with key hostname in the payload in a nested field in the event. ansible-rulebook expects to find meta at the first level. In event.meta not in event.payload.meta

---
- name: Listen for Events on a Webhook
  hosts: all

  sources:

    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 5000
      filters:
        - ansible.eda.insert_hosts_to_meta:
            host_path: payload.hostname
  rules:

    - name: Conditions amd Actions to Process Alerts from Manual Generated Events

now event is modified and  is posted along with hosts under meta

Now you can see the change in the event, with hosts key added

2023-12-02 17:24:57,505 - ansible_rulebook.rule_set_runner - INFO - substitute_variables
[{'name': 'recieved-event.yml'}]
[{
  'events': {
    'm_0': {
      'payload': {
        'message': 'LowDiskSpace',
        'hostname': 'vbrhelhyb01',
        'proessescount': '150'
      },
      'meta': {
        'endpoint': 'endpoint',
        'headers': {
          'Host': '10.10.4.217:5000',
          'Content-Length': '290'
        },
        'hosts': ['vbrhelhyb01'],
        'source': {
          'name': 'ansible.eda.webhook',
          'type': 'ansible.eda.webhook'
        },
        'received_at': '2023-12-02T09:24:57.307288Z',
        'uuid': '9e70cff0-22de-47af-a251-e93e57010831'
      }
    }
  }
}]

Just Assumption only, If the router is not sending any IP address/hostname in the event, at least it should be in the HTTP header. Therefore, you need to have a Python plugin that can extract that information for you.

Hope this is helpful


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFFTi_%2BV4xykLD%3DfSSX6-S4LjHQXzmLKMnQCK2P_fz_P_KkcHw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages