Anatomy of a Salt State

Length: 00:11:14

Lesson Summary:

Way back when we first started this course, we noted that most of Salt is built upon the remote execution framework we just discussed. One of the features built off this is the state system, which allows us to create "infrastructure as code"-style blueprints for our minions. These let us describe our desired end state for a server, then, when run against a minion, brings the minion into compliance with that state.

Create a MySQL Server Installation State

Although we have two servers in our environment working as databases, we're going to start with our CentOS server as we begin to build out our MySQL states. If you're familiar with Debian or Ubuntu MySQL installs, you'll know that there's some extra configuration involved during the installation process, which we'll bring in as we learn about more advanced state creation.

If you downloaded the network state, then you're already in the /srv/salt folder. This folder is known as the file_roots and is the default location for storing states and formulas. This can be changed in the master config.

  1. Move into the mysql directory that we created and initialized as our Git repository:

     $ cd mysql
    

    This is where we'll store all out MySQL configuration files. Note that a collection of related states is called a formula.

  2. Create and open a file, server.sls, where we can write our state:

     $ $EDITOR server.sls
    
  3. We're going to use this file to create a state to download the MySQL server. Let's start by adding a descriptive state declaration:

     mysql_server_install:
    
  4. We now need to determine which state module to use. Most state modules share names with execution modules -- it's the functions that change. This means the module for installing packages, is the pkg module, only instead of using pkg.install, we use pkg.installed:

     mysql_server_install:
       pkg.installed:
    
  5. Finally, we need to set our parameters for the pkg.installed module; keep in mind that we can use the sys.state_list_functions function to view a list of functions. For pkg.installed we have to define the name of the package, but we also have the option to define a particular version (version), set whether or not to reinstall the package if it is already installed (reinstall), refresh the repository database (refresh), and more. Let's just set the name of the package:

     mysql_server_install:
       pkg.installed:
         - name: mariadb-server
    

CentOS servers use a community-maintained version of MySQL, called MariaDB.

  1. Save and exit.

  2. We can now go ahead and test this state:

     sudo salt 'minion2' state.sls mysql.server test=true
    

    Note that, because the state is found under a directory, we called the directory name, then the state, in a format that looks a lot like calling a module.

  3. Finally, go ahead and add and commit this first state to Git:

     git add .
     git commit -am "MySQL server installation state added (server.sls)"
     git push origin master
    

Note that, depending on your GitHub account security, you may need to generate a personal access token and use that to log in, instead of your usual password.


This lesson is only available to Linux Academy members.

Sign Up To View This Lesson
Or Log In

Looking For Team Training?

Learn More