Book Image

Extending SaltStack

Book Image

Extending SaltStack

Overview of this book

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!
Table of Contents (21 chapters)
Extending SaltStack
Credits
Foreword
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

You hold in your hands (or in your e-reader) the first book dedicated to writing code to be used with the SaltStack framework of tools.

What this book covers

Chapter 1, Starting with the Basics, starts with a discussion of the two core principles that this book focuses on: how Salt uses Python and how the Loader system works. These form the foundation of extending Salt.

Chapter 2, Writing Execution Modules, explains that the heavy lifting in most of Salt is performed by execution modules, which are often wrapped by other modules. A solid understanding of execution modules will also be key to understanding how other module types work.

Chapter 3, Extending Salt Configuration, explains that the ability to dynamically manage configuration can make some modules far more useful. Some modules won't even work without dynamic configuration. Here, we explore different ways to provide that.

Chapter 4, Wrapping States Around Modules, supports the fact that execution modules make things work, but state modules make that work persist. In this chapter, you will see how to manage execution modules using state modules.

Chapter 5, Rendering Data, shows that the renderer system allows you to add your own templating systems, increasing the power of states. Jinja and YAML are all well and good, but sometimes, you need something more.

Chapter 6, Handling Return Data, answers the query what happens to the data when a job finishes. There are many places it can go, and you can write modules to send it there.

Chapter 7, Scripting with Runners, shows that SaltStack knows that system administrators have used scripting languages for years, and they have provided a scripting environment that combines Python with the raw power of Salt.

Chapter 8, Adding External File Servers, advises not to just serve files from Salt Master. You can serve files from wherever you want with your own external file server module.

Chapter 9, Connecting to the Cloud, helps you find out how you can update existing cloud modules or add your own. Everyone uses the cloud now, and Salt Cloud connects it to Salt.

Chapter 10, Monitoring with Beacons, helps us to solve the problem that Salt isn't normally associated with monitoring, which is a shame. Beacons are one way to integrate Salt into your monitoring framework.

Chapter 11, Extending the Master, explains that Salt provides a way for you to serve the administrative needs of the Master programmatically. Bonus points for tying in your own authentication system to Salt.

Appendix A, Connecting Different Modules, gives solutions to how to fit the different components even if it is known that Salt modules are designed to play together. This appendix lays out how the different parts connect together.

Appendix B, Contributing Code Upstream, gives you tips to know where the project is screwed up or what features are missing. It doesn't have to be that way with Salt but going back to the community.

What you need for this book

This book assumes a reasonable amount of knowledge of both Salt and the Python programming language. While you may be able to slam out some code without much experience (indeed, that is how the author got started with both), you will find it much easier with these tools already under your belt.

While the examples in this book are tested and functional, they may not be applicable to your needs. They are designed to be simple and easily understood by one who is comfortable with both Python and Salt, while still showcasing a reasonably amount of functionality.

Salt currently has a baseline of Python 2.6, which means that many older Linux distributions are still supported. As of the first edition of this book, Salt does not currently run on the Python 3.x branch. Minion-side examples are expected to work in Windows as well, except when the dependencies that they rely on aren't available.

Because both the Master and the Minion can be run on the same machine, you only need one computer to perform the examples in this book. Many of the examples do refer to software that may not be preinstalled with your operating system. In those cases, the software should still be available for download.

Who this book is 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.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "If there is no __virtual__() function, then the module will always be available on every system."

A block of code is set as follows:

'''
This module should be saved as salt/modules/mysqltest.py
'''
__virtualname__ = 'mysqltest'


def __virtual__():
    '''
    For now, just return the __virtualname__
    '''
    return __virtualname__


def ping():
    '''
    Returns True

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

Any command-line input or output is written as follows:

#salt-call mymodule.test

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "When you visit your fork on GitHub again, you will see a link that says New Pull Request."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/ExtendingSaltStack_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.