Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Extending SaltStack
Extending SaltStack

Extending SaltStack: Build and write salt modules

eBook
₹799.99 ₹2919.99
Paperback
₹3649.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Extending SaltStack

Chapter 2. Writing Execution Modules

Execution modules form the backbone of the workload that Salt performs. They're also easy to write, and the techniques used in writing them form the foundation for writing every other type of Salt module. With a solid understanding of how execution modules work, the functionality of other module types will also be opened up.

In this chapter, we'll talk about:

  • The basics of writing Salt modules
  • Making use of Salt built-ins
  • Using good practices
  • Troubleshooting execution modules

Writing Salt modules

There are a few items that are consistent across all Salt modules. These pieces generally work the same way across all module types, though there are a handful of places where you can expect at least a little deviation. We'll cover those in other chapters as we get to them. For now, let's talk about the things that are generally the same.

Hidden objects

It has long been common for programmers to preface functions, variables, and the like with an underscore, if they are only intended to be used internally in the same module. In many languages, objects that are used like this are said to be private objects.

Some environments enforce private behavior by not allowing external code to reference those things directly. Other environments allow it, but its use is discouraged. Salt modules fall into the list of environments that enforce private function behavior; if a function inside a Salt module begins with an underscore, it will not even be exposed to other modules...

The final module

When we put all of the preceding code together, we end up with the following module:

'''
This module should be saved as salt/modules/mysqltest.py
'''
import salt.utils

try:
    import MySQLdb
    HAS_LIBS = True
except ImportError:
    HAS_LIBS = False

import logging
log = logging.getLogger(__name__)

__func_alias__ = {
    'list_': 'list'
}


__virtualname__ = 'mysqltest'


def __virtual__():
    '''
    Check dependencies, using both methods from the chapter
    '''
    if not salt.utils.which('mysql'):
        return False

    if HAS_LIBS:
        return __virtualname__

    return False


def ping():
    '''
    Returns True

    CLI Example:
        salt '*' mysqltest.ping
    '''
    return True


def check_mysqld():
    '''
    Check to see if sshd is running and listening

    CLI Example:
        salt &apos...

Troubleshooting execution modules

As with any programming, the more time you spend writing execution modules, the more likely you are to encounter issues. Let's take a moment to talk about how to troubleshoot and debug your code.

Using salt-call

The salt-call command has always been a valuable tool for testing and troubleshooting code. Without it, you would need to restart the salt-minion service each time you wanted to test new code; believe me, that gets old fast.

Because salt-call doesn't start up a service, it will always run the latest copy of the Salt code. It does do most of the things that the salt-minion service does: it loads grains, connects to the Master (unless told not to) to obtain pillar data, goes through the loader process to decide which modules to load, and then performs the requested command. Pretty much the only thing it doesn't do is keep running.

Using salt-call to issue a command is also the same as using the salt command, except that a target is not...

Summary

Learning how to write execution modules creates an excellent foundation for writing other Salt modules. Salt contains a number of built-ins, many of which are available across all module types. A number of libraries also ship with Salt inside the salt/utils/ directory. And troubleshooting Salt modules is easiest when using the salt-call command, particularly in local mode.

Next up, we'll talk about various types of Salt module that can be used to handle configuration.

Left arrow icon Right arrow icon

Key benefits

  • Get the most up-to-date practical resource on writing new Salt modules and extending Salt
  • Learn through use cases and encounter both commonly-used modules as well as advanced ones
  • Effectively troubleshoot problems and hiccups encountered while building and putting modules to work

Description

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!

Who is this book for?

This book is for both new and existing Salt developers who are looking to build and write new Salt modules. Some prior Python development experience is expected.

What you will learn

  • Understand the working of Salt s Loader system
  • Write several of the most common types of Salt modules
  • Interact between different kinds of modules and build new ones
  • Submit open source modules upstream to the Salt project
  • Make Salt interact with third-party services and applications

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 22, 2016
Length: 240 pages
Edition : 1st
Language : English
ISBN-13 : 9781785888168
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Mar 22, 2016
Length: 240 pages
Edition : 1st
Language : English
ISBN-13 : 9781785888168
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
₹4500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts
₹5000 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 10,651.97
Learning SaltStack
₹2904.99
Extending SaltStack
₹3649.99
Mastering SaltStack
₹4096.99
Total 10,651.97 Stars icon
Banner background image

Table of Contents

14 Chapters
1. Starting with the Basics Chevron down icon Chevron up icon
2. Writing Execution Modules Chevron down icon Chevron up icon
3. Extending Salt Configuration Chevron down icon Chevron up icon
4. Wrapping States Around Execution Modules Chevron down icon Chevron up icon
5. Rendering Data Chevron down icon Chevron up icon
6. Handling Return Data Chevron down icon Chevron up icon
7. Scripting with Runners Chevron down icon Chevron up icon
8. Adding External File Servers Chevron down icon Chevron up icon
9. Connecting to the Cloud Chevron down icon Chevron up icon
10. Monitoring with Beacons Chevron down icon Chevron up icon
11. Extending the Master Chevron down icon Chevron up icon
A. Connecting Different Modules Chevron down icon Chevron up icon
B. Contributing Code Upstream Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8
(6 Ratings)
5 star 83.3%
4 star 16.7%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Mike S Jul 08, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I was provided a free copy of this book in exchange for a truthful and honest review. I am a SaltStack Certified engineer and have been working with Salt for about 2 years. I’m a sys admin by trade, not a programmer, and haven't previously tried writing my own Salt modules beyond the simple “hello world” type tutorial in the Salt documentation. That said, this book provides excellent guidance on how to approach creating your own modules of nearly all types. As a relative Python novice, at no point was I confused about conventions or what the example code was doing. This will be a very useful reference as I continue to develop our configuration management infrastructure and further bend Salt to my will and needs. Every environment is different and having the ability to add functionality to your tools is important.The book has a few minor rough edges where examples appear to have been revised and are obviously inconsistent (reference to sshd vs mysqld and vice versa in one of the first examples) but it doesn't detract from the point. There are also some stray pylint references that I assume are leftover from a proofreading process. Overall, this is a solid reference that speaks from experience, provides relevant examples, warns of pitfalls, and provides troubleshooting tips for when you get started on your own.
Amazon Verified review Amazon
Innocent Bystander Aug 07, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Extending SaltStack by Joseph Hall is a fantastic resource if you have gotten your feet wet with SaltStack and would like to do more complex management, and extend the power of SaltStack to fully automate management of your infrastructure and manage the more complex use cases possible with SaltStack.Building on is previous work which will give you a strong basis in the overall platform, SaltStack covers the more complex use cases and capabilities that can help you fully realize the extensible power of this flexible management platform. From building custom modules, to using runners to move data or using beacons to build a custom monitoring system, Hall ties the various functions of Salt together and explains very well how they all work together to build the exact functionality you need.The book is intended for people with moderate to advanced knowledge of the Salt System but also does a good job of reminding you of the basics where necessary to illustrate how a particular function works or should work to accomplish your end goal.If you are looking to increase your knowledge of the Salt system or if you are looking to build a customized, automated management and automation platform, this book is perfect, and will serve as both a teacher and a reference as you move forward. The examples for each section provide real world code and use cases to help you learn the system's intricacies and master the Salt Management platform.As someone who's worked with Salt for the last several years, I was asked to review this book and provided a copy of the book for the review.
Amazon Verified review Amazon
Megan Wilhite Aug 11, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book really helped me to understand some of the more complicated aspects of saltstack that the documentation only covers on the surface. The exercises and examples were great cases to help me to understand how to write modules in salt and the book was approached with someone has very little python experience which was very useful.The book focuses on writing modules and helping the user to understand how to write modules and troubleshoot them when it is not working. This has helped me to further understand salt and to troubleshoot problems within salt by diving into the code. All around great book which is a great way to learn how to write modules and understand more of the complicated aspects of salt.
Amazon Verified review Amazon
Shane Lee Jul 21, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I found this book to be an excellent resource for writing your own custom modules and extensions for Salt. This book explains how Salt functions under the hood. It provides clear, concise examples and code snippets that demonstrate how to setup your environment and write Salt code.The book begins by explaining that Salt is so much more than just a configuration management platform. Then it goes through many of Salt's functionalities and demonstrates how to custom extensions for each one. It provides tips on how to approach writing custom extensions. It explains the reasoning behind design decisions that affect the way you write your code.This book covers everything you need to write custom extensions for Salt. I have worked with Salt for over a year. This book made me realize I still have a lot to learn.
Amazon Verified review Amazon
Janne Aug 06, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
One of the key points about Salt is it being much more than Puppet, Chef or Ansible. Any book written about Salt should - in my opinion - express this clearly and make the reader understand the possibilities Salt has. I think Joseph does this well in Extending SaltStack. It is not purely a book written by a developer for developers, but contains vast amount of good information for less coding users as well, like myself.To be really efficient with Salt in the long run, you either need to dive deep in to the actual codebase, navigate through the sometimes complex/vague documentation or read books like this one. For me personally, I learned a lot even about the most basic features like runners, beacons (which I have not yet used at all), debugging and the mind of salt-cloud (which I know Joseph has contributed a lot to).What I would've liked to see more of are practical and more complex examples of Salt usecases, although it might not be the subject of this book. But maybe the book could've been about writing your own modules which come together in the end. Maybe in the next book!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.