Introduction
Salt is a configuration management and distributed remote execution system. It was created with the goal to improve, simplify, and further customize the best remote execution solutions. It can maintain the remote nodes in defined states and query and execute commands on various nodes.

In this blog, we will discuss the Event System module provided by Salt.
Event System
The salt event system is used to fire off events like the Start event, Key event, Job event, etc., using third-party apps or with the help of an external process.

There is a concept of an event bus in the Event system. Let’s discuss Event Bus.
Event Bus
The event bus is used for both network transport and inter-process communication in salt. The inter-process communication happens through Unix Domain Socket(UDX).
The Event Bus consists of two components:-
-
The event socket is used to publish events.
- The event library is used to listen events and send events into the salt system.
The salt master and salt minion have their own Event bus.
Let’s discuss the different types of events present in the salt system.
Event Types
The different types of events fired on the Salt master event bus are:-
-
Authentication Events: These events are fired when a minion tries for an authentication check with the master.
-
Start Events: These events are fired whenever a minion connects to the master.
-
Key Events: These events are fired when accepting and rejecting the minion's keys on the master.
-
Job Events: These events are fired when a minion creates a new job.
- Runner Events: These events are fired when a runner begins its execution or returns from a function.
Listening Events
There are various ways to listen to an event in the Salt system. Some of the ways to listen an event in salt are:-
1. Using CLI
The fastest way to interact with the event bus is by calling state.event runner.
salt-run state.event pretty=True
2. Using REST API
The salt.netapi.rest_cherrypy.app.Events are consumed as an HTTP stream from external tools or services with the help of the Salt event bus.
curl -SsNk https://salt-api.example.com:8000/events?token=05A3
3. Using Python
Python scripts can access the event bus when they are running on the same system in which salt is running. Only the system user where Salt is running can access the event system, which is accessed through the event library.
The following script will check for a single event.
import salt.config
import salt.utils.event
opts = salt.config.client_config("/etc/salt/master")
event = salt.utils.event.get_event("master", sock_dir=opts["sock_dir"], opts=opts)
data = event.get_event()
Let’s now discuss some of the firing events in the salt system.
Firing Events
The events can be fired either on the salt master or on the minion’s local bus.
There are various ways to fire an event in the Salt system. Some of the ways to fire an event in salt are:-
1. Using CLI
Call the event.fire on CLI to fire a local event.
salt-call event.fire '{"data": "message to be sent in the event"}' 'tag'
2. Using Python
Firing events from a custom python program is very easy. You just have to run the following code:
import salt.client
caller = salt.client.Caller()
ret = caller.cmd(
"event.send", "myco/event/success", {"success": True, "message": "It works!"}
)
if not ret:
# the event could not be sent, process the error here
...