You are previewing Extending SaltStack.
O'Reilly logo
Extending SaltStack

Book Description

Extend the power of your infrastructure and applications with Salt modules

About This Book

  • 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

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

    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

  • In Detail

    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!

    Style and approach

    This book follows a step-by-step tutorial-based approach explaining the different types of modules, from fundamentals to complete and full-functioning modules.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Extending SaltStack
      1. Table of Contents
      2. Extending SaltStack
      3. Credits
      4. Foreword
      5. About the Author
      6. About the Reviewer
        1. eBooks, discount offers, and more
          1. Why subscribe?
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      9. 1. Starting with the Basics
        1. Using plugins
          1. Loading modules
          2. Standard modules
          3. Virtual modules
          4. Lazy loading modules
        2. Extending the loader system
        3. Loading modules with Python
          1. Detecting grains
          2. Using other detection methods
        4. Summary
      10. 2. Writing Execution Modules
        1. Writing Salt modules
          1. Hidden objects
          2. The __virtual__() function
          3. Formatting your code
          4. Virtual modules
          5. Using the salt.utils library
          6. Cross-calling with the __salt__ dictionary
          7. Getting configuration parameters
          8. Handling imports
          9. Reusing code
          10. Logging messages
          11. Using the __func_alias__ dictionary
          12. Validating data
          13. Formatting strings
        2. The final module
        3. Troubleshooting execution modules
          1. Using salt-call
          2. <function> is not available
        4. Summary
      11. 3. Extending Salt Configuration
        1. Setting grains dynamically
          1. Setting some basic grains
          2. (Not) cross-calling execution modules
          3. The final grains module
        2. Creating external pillars
          1. Configuring external pillars
          2. Adding an external pillar
          3. Another external pillar
        3. Troubleshooting grains and pillars
          1. Dynamic grains not showing up
          2. External pillars not showing up
        4. Writing SDB modules
          1. Getting SDB data
          2. Setting SDB data
          3. Using a descriptive docstring
          4. Using more complex configuration
          5. The final SDB module
        5. Using SDB modules
        6. Troubleshooting SDB modules
          1. SDB data not showing up
        7. Summary
      12. 4. Wrapping States Around Execution Modules
        1. Forming a state module
          1. Determining state
            1. The __virtual__() function
            2. Setting up defaults
            3. Checking for truth
            4. Checking for test mode
            5. Attempting to configure the resource
            6. Notifying about False
          2. Example: checking an HTTP service
            1. Checking credentials
            2. The first stateful function
            3. Another stateful function
        2. Troubleshooting state modules
          1. Step 1: test for truth
          2. Step 2: test mode
          3. Step 3: applying changes
          4. Testing opposites
        3. Summary
      13. 5. Rendering Data
        1. Understanding file formats
          1. Serializing data
          2. Working with templates
          3. Using render pipes
        2. Building a serializing renderer
          1. The basic structure
        3. Building a templating renderer
          1. Templating with Tenjin
          2. Using a templating renderer
        4. Troubleshooting renderers
        5. Summary
      14. 6. Handling Return Data
        1. Returning data to external destinations
        2. Returning data to the master
        3. Listening to event data
          1. When returners listen to Minions
        4. Your first returner
          1. Using job caches
        5. The final module
        6. Troubleshooting returners
          1. Testing with salt-call
          2. Testing with the Master running
          3. Testing with runners
        7. Writing outputter modules
          1. Pickling our output
        8. Troubleshooting outputters
        9. Summary
      15. 7. Scripting with Runners
        1. Using Salt's local client
          1. Scripting with the local client
          2. Using different targets
          3. Combining jobs to add more logic
        2. The final module
        3. Troubleshooting runners
          1. Working with the salt-master service
          2. Timeout issues
        4. Summary
      16. 8. Adding External File Servers
        1. How Salt uses files
          1. Mimicking a filesystem
        2. Looking at each function
          1. Setting up our module
          2. envs()
          3. file_list() and dir_list()
          4. find_file()
          5. serve_file()
          6. update()
          7. file_hash()
        3. The final module
        4. Troubleshooting file servers
          1. Start small
          2. Test on a Minion
        5. Summary
      17. 9. Connecting to the Cloud
        1. Understanding cloud components
          1. Looking at the puzzle pieces
            1. Connection mechanism
            2. Listing resources
            3. Creating virtual machines
            4. Managing other resources
          2. Libcloud versus SDK versus direct REST API
        2. Writing a generic cloud module
          1. Checking for required configuration
          2. Using http.query()
          3. A common REST API
            1. GET
            2. POST
            3. PATCH
            4. DELETE
          4. Setting up a _query() function
          5. Getting profile details
            1. Listing images
            2. Listing sizes
            3. Listing locations
          6. Listing nodes
            1. Querying standard node data
            2. Querying full node data
          7. Creating a VM
          8. Destroying VMs
          9. Using actions and functions
            1. Using actions
            2. Using functions
        3. The final cloud module
        4. Troubleshooting cloud modules
          1. Write avail_sizes() or avail_images() first
          2. Use shortcuts
        5. Summary
      18. 10. Monitoring with Beacons
        1. Watching for data
          1. Keeping an eye on things
          2. Validating configuration
          3. The beacon() function
          4. Watching for beacons
        2. The final beacon module
        3. Troubleshooting beacons
        4. Summary
      19. 11. Extending the Master
        1. Using external authentication
          1. Authenticating credentials
        2. Troubleshooting external authentication
          1. Setting auth parameters
            1. Testing with the salt command
            2. Testing with Salt API
        3. Managing the Master with the wheel modules
          1. Wrapping a wheel around runners
          2. The final wheel module
        4. Troubleshooting wheel modules
        5. Summary
      20. A. Connecting Different Modules
        1. Separating Master and Minion functionality
        2. Working with dunders
        3. Using the event bus
        4. Firing events
      21. B. Contributing Code Upstream
        1. How the community works
        2. Asking questions and reporting issues
          1. Using the mailing list
          2. Using IRC
          3. Using the issue tracker
          4. Using GitHub markdown
        3. Understanding the Salt Style Guide
          1. Using Pylint
        4. Creating pull requests on GitHub
          1. Using other branches
        5. Understanding test errors in pull requests
      22. Index