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( 'master', sock_dir, transport, listen=False, ) 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.
Note
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.