| 1 | |
| 2 | # How to Configure EDA to use Azure servicebus as a source |
| 3 | |
| 4 | ## First - See the Ansible Rulebook Documentation |
| 5 | |
| 6 | Documentation about Ansbile Rulebooks and EDA are available [here](https://ansible.readthedocs.io/projects/rulebook/en/latest/). See the Getting started and Installation sections before continuing. |
| 7 | |
| 8 | ## Example azure-rulebook.yml file: |
| 9 | |
| 10 | ```yaml |
| 11 | --- |
| 12 | - name: Listen for events from azure |
| 13 | hosts: all |
| 14 | sources: |
| 15 | - azure.azcollection.azure_service_bus: |
| 16 | conn_str: **CONNECTION_STRING** |
| 17 | queue_name: **QUEUE_NAME** |
| 18 | rules: |
| 19 | - name: Say Hello |
| 20 | condition: event.body.message == "Azure and Ansible is super cool" |
| 21 | action: |
| 22 | run_playbook: |
| 23 | name: hello_playbook.yml |
| 24 | ``` |
| 25 | |
| 26 | Rulebooks are run via ansible-rulebook or from AAP: |
| 27 | |
| 28 | ansible-rulebook -r azure-rulebook.yml -v -i inventory |
| 29 | |
| 30 | ## Here is a crude example of sending a message to servicebus with python: |
| 31 | |
| 32 | ```python |
| 33 | import os |
| 34 | import threading |
| 35 | import json |
| 36 | from azure.servicebus import ServiceBusClient, ServiceBusMessage |
| 37 | |
| 38 | |
| 39 | CONNECTION_STRING = os.environ["SERVICEBUS_CONNECTION_STRING"] |
| 40 | QUEUE_NAME = os.environ["SERVICEBUS_QUEUE_NAME"] |
| 41 | |
| 42 | |
| 43 | def send_single_message(sender, data): |
| 44 | message = ServiceBusMessage(json.dumps(data)) |
| 45 | sender.send_messages(message) |
| 46 | |
| 47 | |
| 48 | servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STRING, |
| 49 | logging_enable=True) |
| 50 | data = dict(message="Azure and Ansible is super cool") |
| 51 | with servicebus_client: |
| 52 | sender = servicebus_client.get_queue_sender(queue_name=QUEUE_NAME) |
| 53 | with sender: |
| 54 | send_single_message(sender, data) |
| 55 | |
| 56 | print("Send message is done.") |
| 57 | ``` |
| 58 | |
| 59 | ## Example debug output from running the above rulebook and receiving the example message |
| 60 | |
| 61 | ``` |
| 62 | 2025-06-23 14:46:44,667 - azure.servicebus._pyamqp.receiver - DEBUG - <- TransferFrame(handle=2, delivery_id=1, delivery_tag=b'5\x14\xda\xf8\xe7CKN\xa3\x0b\x9a\x16yx\xe5\xa2', message_format=0, settled=None, more=False, rcv_settle_mode=None, state=None, resume=None, aborted=None, batchable=True, payload=b'***') |
| 63 | 2025-06-23 14:46:44,667 - azure.servicebus._transport._pyamqp_transport - DEBUG - Received message: seq-num: 15, enqd-utc: datetime.datetime(2025, 6, 23, 18, 46, 44, 637000, tzinfo=datetime.timezone.utc), lockd-til-utc: datetime.datetime(2025, 6, 23, 18, 47, 44, 652000, tzinfo=datetime.timezone.utc), ttl: datetime.timedelta(days=14), dlvry-cnt: 0 |
| 64 | 2025-06-23 14:46:44,667 - azure.servicebus._pyamqp.receiver - DEBUG - -> DispositionFrame(role=True, first=1, last=None, settled=True, state=Accepted(), batchable=None) |
| 65 | |
| 66 | ** 2025-06-23 14:46:44.668293 [received event] ********************************************************************** |
| 67 | 2025-06-23 14:46:44,668 - azure.servicebus._pyamqp.cbs - DEBUG - CBS status check: state == <CbsAuthState.OK: 0>, expired == False, refresh required == False |
| 68 | Ruleset: Listen for events from azure |
| 69 | 2025-06-23 14:46:44,668 - azure.servicebus._pyamqp.session - DEBUG - -> FlowFrame(next_incoming_id=3, incoming_window=65534, next_outgoing_id=1, outgoing_window=65535, handle=3, delivery_count=1, link_credit=1, available=None, drain=None, echo=None, properties=None) |
| 70 | Event: |
| 71 | {'body': {'message': 'Azure and Ansible is super cool'}, |
| 72 | 'meta': {'message_id': '80a59815-3c23-41f2-9889-9893a4b9c2da', |
| 73 | 'received_at': '2025-06-23T18:46:44.668093Z', |
| 74 | 'source': {'name': 'azure.azcollection.azure_service_bus', |
| 75 | 'type': 'azure.azcollection.azure_service_bus'}, |
| 76 | 'uuid': '07f5c3b7-ebf5-4204-9151-864e44a4829e'}} |
| 77 | ********************************************************************************************************************* |
| 78 | 2025-06-23 14:46:44,668 - ansible_rulebook.rule_set_runner - DEBUG - Posting data to ruleset Listen for events from azure => {'body': {'message': 'Azure and Ansible is super cool'}, 'meta': {'message_id': '80a59815-3c23-41f2-9889-9893a4b9c2da', 'source': {'name': 'azure.azcollection.azure_service_bus', 'type': 'azure.azcollection.azure_service_bus'}, 'received_at': '2025-06-23T18:46:44.668093Z', 'uuid': '07f5c3b7-ebf5-4204-9151-864e44a4829e'}} |
| 79 | 2025-06-23 14:46:44 669 [main] INFO org.drools.ansible.rulebook.integration.api.rulesengine.MemoryMonitorUtil - Memory occupation threshold set to 90% |
| 80 | 2025-06-23 14:46:44 670 [main] INFO org.drools.ansible.rulebook.integration.api.rulesengine.MemoryMonitorUtil - Memory check event count threshold set to 64 |
| 81 | 2025-06-23 14:46:44 670 [main] INFO org.drools.ansible.rulebook.integration.api.rulesengine.MemoryMonitorUtil - Exit above memory occupation threshold set to false |
| 82 | 2025-06-23 14:46:44 711 [main] DEBUG org.drools.ansible.rulebook.integration.api.rulesengine.RegisterOnlyAgendaFilter - Activation of effective rule "Say Hello" with facts: {m={meta={message_id=80a59815-3c23-41f2-9889-9893a4b9c2da, source={name=azure.azcollection.azure_service_bus, type=azure.azcollection.azure_service_bus}, received_at=2025-06-23T18:46:44.668093Z, uuid=07f5c3b7-ebf5-4204-9151-864e44a4829e}, body={message=Azure and Ansible is super cool}}} |
| 83 | 2025-06-23 14:46:44,718 - drools.ruleset - DEBUG - Calling rule : Say Hello in session: 1 |
| 84 | 2025-06-23 14:46:44,718 - ansible_rulebook.rule_generator - DEBUG - callback calling Say Hello |
| 85 | 2025-06-23 14:46:44,718 - ansible_rulebook.rule_set_runner - DEBUG - None |
| 86 | 2025-06-23 14:46:44,718 - ansible_rulebook.rule_set_runner - DEBUG - Creating action task action::run_playbook::Listen for events from azure::Say Hello |
| 87 | 2025-06-23 14:46:44,718 - ansible_rulebook.rule_set_runner - DEBUG - call_action run_playbook |
| 88 | 2025-06-23 14:46:44,719 - ansible_rulebook.action.run_playbook - DEBUG - ruleset: Listen for events from azure, rule: Say Hello |
| 89 | 2025-06-23 14:46:44,719 - ansible_rulebook.action.run_playbook - DEBUG - private data dir /tmp/edap29wpkf2 |
| 90 | 2025-06-23 14:46:44,721 - ansible_rulebook.action.run_playbook - DEBUG - project_data_file: None |
| 91 | ``` |