Book Image

Extending SaltStack

Book Image

Extending SaltStack

Overview of this book

Salt already ships with a very powerful set of tools, but that doesn't mean that they all suit your needs perfectly. By adding your own modules and enhancing existing ones, you can bring the functionality that you need to increase your productivity. Extending SaltStack follows a tutorial-based approach to explain different types of modules, from fundamentals to complete and full-functioning modules. Starting with the Loader system that drives Salt, this book will guide you through the most common types of modules. First you will learn how to write execution modules. Then you will extend the configuration using the grain, pillar, and SDB modules. Next up will be state modules and then the renderers that can be used with them. This will be followed with returner and output modules, which increase your options to manage return data. After that, there will be modules for external file servers, clouds, beacons, and finally external authentication and wheel modules to manage the master. With this guide in hand, you will be prepared to create, troubleshoot, and manage the most common types of Salt modules and take your infrastructure to new heights!
Table of Contents (21 chapters)
Extending SaltStack
About the Author
About the Reviewer

Firing events

You can fire events both from the Minion-side modules (such as execution and state modules) and Master-side modules (such as runners). From a Minion-side module, you need nothing more than to call out to the event execution module as follows:

__salt__['event.fire_master'](data_dict, some_tag)

But in Master-side modules, you need to do a little more work, since __salt__ isn't available. You need to import salt.utils.event, then use it to fire the event. This isn't much more work, but you do have to do some setup. It looks like:

import os.path
import salt.utils.event
import salt.syspaths
sock_dir = os.path.join(salt.syspaths.SOCK_DIR, 'master')
transport = __opts__.get('transport', 'zeromq')
event = salt.utils.event.get_event(
event.fire_event(data_dict, some_tag)

Let's go over what happened here. First, we set up our imports. The salt.syspaths library contains information about where standard files and directories will be located on this system. In our case, we need to connect to a socket called master. We use this information to set up a variable called sock_dir, which tells Salt where to find the event bus to connect to.

We also find out which transport mechanism is configured for this system. This will usually be zeromq, but it can also be another protocol such as raet or tcp. Then we set up an object using the get_event() function. The first argument says which bus we're dealing with, then the sock_dir, transport, and finally we say that we're not going to be listening for events' we'll be sending them.


What do we mean by which bus we're dealing with? Both the Master and the Minion have their own event bus. A Minion can either fire a message to itself using the minion bus, or to the Master using the master bus. The Minion event bus is rarely used except by the internal Salt code, but the Master bus is used extensively.

Once we have the event object set up, we can fire the event. The data (which can be a list or a dictionary) is specified first, and then the event tag. If you like, you can set up a listener on the Master to see those events come in:

# salt-run state.event pretty=True

One of the most useful things that events are used in is reactors. As mentioned earlier, for more information on writing reactors, check out Mastering SaltStack, Joseph Hal l, Packt Publishing.