Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Extending SaltStack

You're reading from   Extending SaltStack Build and write salt modules

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher Packt
ISBN-13 9781785888618
Length 240 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Joseph Hall Joseph Hall
Author Profile Icon Joseph Hall
Joseph Hall
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Extending SaltStack
Credits
Foreword
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. Starting with the Basics FREE CHAPTER 2. Writing Execution Modules 3. Extending Salt Configuration 4. Wrapping States Around Execution Modules 5. Rendering Data 6. Handling Return Data 7. Scripting with Runners 8. Adding External File Servers 9. Connecting to the Cloud 10. Monitoring with Beacons 11. Extending the Master Connecting Different Modules Contributing Code Upstream Index

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(
    '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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images