Table of contents
1.
Introduction
2.
Event System
2.1.
Event Bus
2.2.
Event Types
2.3.
Listening Events
2.3.1.
1. Using CLI
2.3.2.
2. Using REST API
2.3.3.
3. Using Python
2.4.
Firing Events
2.4.1.
1. Using CLI
2.4.2.
2. Using Python
3.
Frequently Asked Questions
3.1.
What is Salt?
3.2.
What is the Event System in Salt?
3.3.
What is an Event bus in salt?
3.4.
What are Authentication events in salt?
3.5.
What are Key events in salt?
4.
Conclusions
Last Updated: Mar 27, 2024
Medium

Salt Event System

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

saltstack

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. 

salt event system

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
You can also try this code with Online Python Compiler
Run Code


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
You can also try this code with Online Python Compiler
Run Code


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()
You can also try this code with Online Python Compiler
Run Code


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'
You can also try this code with Online Python Compiler
Run Code


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
    ...
You can also try this code with Online Python Compiler
Run Code

Frequently Asked Questions

What is Salt?

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. 

What is the Event System in Salt?

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.

What is an Event bus in salt?

The event bus is used for both network transport and inter-process communication in salt. It consists of two components, the event socket and the event library.

What are Authentication events in salt?

These are the salt master events ​​fired when a minion tries for an authentication check with the master.

What are Key events in salt?

These are the salt master events fired when accepting and rejecting the minion's keys on the salt master.

Conclusions

In this article, we have extensively discussed the Event System module provided by Salt. I hope you enjoyed this blog on Salt Event System. If you want to learn more, check out our articles on About Salt RunnersTarget Minions in SaltAbout Salt Engine, and Pillar Walkthrough.

Also, check out these exciting courses from coding ninjas to expand your knowledge, Coding CourseCode StudioInterview ExperienceGuided PathInterview ProblemsTest SeriesLibrary, and Resources

Happy Coding!

Live masterclass