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
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Cyprus

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

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 : 9781785888618
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Cyprus

Premium delivery 7 - 10 business days

€32.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 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
€189.99 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 €5 each
Feature tick icon Exclusive print discounts
€264.99 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 €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 108.97
Learning SaltStack
€29.99
Extending SaltStack
€36.99
Mastering SaltStack
€41.99
Total 108.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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela