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
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

Arrow left icon
Profile Icon Joseph Hall
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8 (6 Ratings)
Paperback Mar 2016 240 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Joseph Hall
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8 (6 Ratings)
Paperback Mar 2016 240 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Extending SaltStack

Chapter 1. Starting with the Basics

The vast majority of Salt users see it as a configuration management platform. And in truth, it handles that very well. But it did not start off with that as a design goal. In its early days, Salt was a communication framework that was designed to be useful even to those who did not write code. But for those who were willing, it was also designed to be heavily extensible to those users who had some Python in their toolbelt.

Before we get into writing modules, it will help to have a basic understanding of how the Salt module system works. In this chapter, you'll learn the following:

  • How the loader system works
  • How Salt uses Python

Using plugins

As Salt was originally designed as a backbone that other software could use to communicate, its earliest purpose was to collect information from a large cluster of both physical and virtual machines, and return that data either to the user or to a database. Various programs, such as ps, du, and netstat, were used to collect that information. Because of that, each program was wrapped with a plugin, which contained various functions to call those programs, and parse the return data.

Those plugins were originally called modules. Later, when other types of module were added to Salt, the original modules began to be referred to as execution modules. This is because the execution modules would do the heavy lifting, and other types of module would generally wrap around them and extend their functionality.

Loading modules

Like many data centers, the one that Salt was created in had various servers that used different software packages to perform their work. One server would be running Nginx, while another would be running DNSMasq. It wouldn't make sense to enable the nginx module on the DHCP server, or a dnsmasq module on the web server. A number of popular programs solve this by allowing the user to configure which plugins will be loaded before starting the service.

Salt had a different way of handling plugins. In a large infrastructure, individual configuration of servers can be costly in terms of time. And as configuration management was added to Salt, a core belief grew that configuration management platforms should require as little configuration themselves as possible. What is the point of using such a suite to save time if so much time is required to get it going in the first place?

This is how the loader system came to be. Salt would always ship with a full set of modules, and Salt would automatically detect modules that would be available, and dynamically load them.

Execution modules are a type of plugin that performs most of the heavy lifting inside of Salt. These were the first to use the loader system, and for a short time there was no other type of module. As the functionality of Salt increased, it quickly became evident that other types of module would be needed. For instance, return output was originally just printed to the console. Then the output was changed to be easier to handle from shell scripts. Then the outputter system was added, so that output could be displayed in JSON, YAML, Python's pprint, and any other format that might be useful.

Standard modules

In the beginning, there were some types of module that would always be loaded. The first of these was the test module, which required nothing more than Salt's own dependencies; in particular, it would only require Python.

Other modules were also designed for general use, requiring no more than Salt's own dependencies. The file module would perform various file-based operations. The useradd module would wrap the standard Unix useradd program. This was fine, so long as Salt was only used on Unix-like platforms. When users started running Salt on Windows, where those utilities were not readily available, things changed. This is where virtual modules really started to shine.

Virtual modules

Supporting Salt on various platforms, such as both Unix-like and Windows, presents the same problem as whether or not to make the nginx module available: if that platform is installed and available, make the module available. Otherwise, don't. Salt handles the availability problem by implementing virtual modules.

The idea behind a virtual module is that it will contain a piece of code that will detect whether or not its dependencies are met, and if so, the module will be loaded and made available to Salt on that system. We'll get into the details of actually doing this in Chapter 2, Writing Execution Modules.

Lazy loading modules

In the beginning, if a module was detected as being loadable, then it would be loaded as the Salt service was started. A number of modules may be loaded for a particular system, which the administrator never intends to use. It may be nice to have them, but in some cases it's better to only load them when they're needed.

When the Salt service starts, the lazy loader will detect which modules may be used on a particular system, but it won't immediately load them into memory. Once a particular module is called, Salt will load it on demand, and then keep it in memory. On a system that typically only uses a small handful of modules, this can result in a much smaller footprint than before.

Extending the loader system

As we said before, the loader system was originally designed for one type of module: what we now call execution modules. Before long, other types of module were added, and that number continues to grow even today.

This book does not include every type of module, but it does cover quite a few. The following list is not comprehensive, but it will tell you much of what is available now, and possibly give you an idea of what other types of module to look at after you finish this book:

  • Execution modules do much of the heavy lifting inside of Salt. When a program needs to be called, an execution module will be written for it. When other modules need to use that program, they will call out to that module.
  • Grain modules are used to report information about Minions. Virtual modules often rely heavily on these. Configuration can also be defined in grains.
  • Runner modules were designed to add an element of scripting to Salt. Whereas execution modules run on Minions, a runner module would run on the Master, and call out to the Minions.
  • Returner modules give Minions a way to return data to something besides the Master, such as a database configured to store log data.
  • State modules transform Salt from a remote execution framework into a configuration management engine.
  • Renderer modules allow Salt States to be defined using different file formats, as appropriate.
  • Pillar modules extend grains, by providing a more centralized system of defining configuration.
  • SDB modules provide a simple database lookup. They are usually referenced from configuration areas (including grains and pillars) to keep sensitive data from appearing in plaintext.
  • Outputter modules affect how command-line data output is shown to the user.
  • External file server modules allow the files that Salt serves to be stored somewhere besides locally on the Master.
  • Cloud modules are used to manage virtual machines across different compute cloud providers.
  • Beacons allow various pieces of software, from other Salt components to third-party applications, to report data to Salt.
  • External authentication modules allow users to access the Master without having to have a local account on it.
  • Wheel modules provide an API for managing Master-side configuration files.
  • Proxy minion modules allow devices that cannot run the Salt platform itself to be able to be treated as if they were still full-fledged Minions.
  • Engines allow Salt to provide internal information and services to long-running external processes. In fact, it may be best to think of engines as programs in their own right, with a special connection to Salt.
  • The Master Tops system allows States to be targeted without having to use the top.sls file.
  • Roster modules allow Salt SSH to target Minions without having to use the /etc/salt/roster file.
  • Queue modules provide a means of organizing function calls.
  • The pkgdb and pkgfile modules allow the Salt Package Manager to store its local database and install Salt formulas into a location outside of the local hard drive.

These modules were generally created as necessity dictated. All of them are written in Python. And while some can be pretty extensive, most are pretty simple to create. In fact, a number of modules that now ship with Salt were actually provided by users who had no previous Python experience.

Loading modules with Python

Python is well suited to building a loader system. Despite being classified as a very high-level language (and not a mid-level language like C), Python has a lot of control over how it manages its own internals. The existence of robust module introspection built into Python was very useful for Salt, as it made the arbitrary loading of virtual modules at runtime a very smooth operation.

Each Salt module can support a function called __virtual__(). This is the function that detects whether or not a module will be made available to Salt on that system.

When the salt-minion service loads, it will go through each module, looking for a __virtual__() function. If none is found, then the module is assumed to have all of its requirements already met, and it can be made available. If that function is found, then it will be used to detect whether the requirements for that module are met.

If a module type uses the lazy loader, then modules that can be loaded will be set aside to be loaded when needed. Modules that do not meet the requirements will be discarded.

Detecting grains

On a Minion, the most important things to load are probably the grains. Although grain modules are important (and are discussed in Chapter 3, Extending Salt Configuration), there are in fact a number of core grains that are loaded by Salt itself.

A number of these grains describe the hardware on the system. Others describe the operating system that Salt is running on. Grains such as os and os _family are set, and used later to determine which of the core modules will be loaded.

For example, if the os_family grain is set to redhat, then the execution module located at salt/modules/yumpkg.py will be loaded as the pkg module. If the os_family grain is set to debian, then salt/modules/aptpkg.py will be loaded as the pkg module.

Using other detection methods

Grains aren't the only mechanism used for determining whether a module should be loaded. Salt also ships with a number of utilities that can be used. The salt.utils library contains a number of functions that are often faster than grains, or have more functionality than a simple name=value (also known as a key-value pair) configuration can provide.

One example is the salt.utils.is_windows() function that, as the name implies, reports whether Salt is being run inside of Windows. If Windows is detected, then salt/modules/win_file.py will be loaded as the file module. Otherwise, salt/modules/file.py will be loaded as the file module.

Another very common example is the salt.utils.which() function, which reports whether a necessary shell command is available. For instance, this is used by salt/modules/nginx.py to detect whether the nginx command is available to Salt. If so, then the nginx module will be made available.

There are a number of other examples that we could get into, but there is not nearly enough room in this book for all of them. As it is, the most common ones are best demonstrated by example. Starting with Chapter 2, Writing Execution Modules, we will begin writing Salt modules that make use of the examples that we've already gone over, plus a wealth of others.

Summary

Salt is made possible by the existence of the loader system, which detects which modules are able to load, and then only what is available. Types of module that make use of the lazy loader will only be loaded on demand.

Python is an integral part of Salt, allowing modules to be easily written and maintained. Salt ships with a library of functions that help support the loader system, and the modules that are loaded with it. These files live in various directories under the salt/ directory in Salt's code base. For example, execution modules live in salt/modules/.

This chapter barely brushed the surface of what is possible with Salt, but it got some necessary concepts out of the way. From here on in, the focus will be all about writing and maintaining modules in Python.

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

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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
$19.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
$199.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
$279.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 $ 142.97
Learning SaltStack
$38.99
Extending SaltStack
$48.99
Mastering SaltStack
$54.99
Total $ 142.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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.