Length: 00:08:39

Lesson Summary:

One thing we want to consider is the order that our states work in. Generally, we want to ensure that any packages are installed first, then our configurations are set, and then any additional work that needs to be done, such as creating a user or database, is completed.

This is where Salt's requisite system comes in. Salt's requisite systems allow us to define the relationships between our states and include any requirements -- such as necessary packages -- that we need for our state to work.

Create a Restart State

  1. Move into the MySQL formula directory, then create and open restart.sls:

     $ cd /srv/salt/mysql
     $ $EDITOR restart.sls
  2. Set the name declaration and function; to get this state to work, we'll use two functions: service.restart and module.wait. service.restart is what restarts our defined service, like module.wait prevents this state from running every highstate; instead, it waits for the provided requisite to occur.

         - name: service.restart
         - m_name: mariadb

    Here we can see that the module.wait function takes precedence -- it's the function we call after our name declaration. Then, the name value we need to provide is the actual function we want to work with. After this, we provide the service name, as we would were we just using service.restart -- only instead of setting it as name, we use the m_name option.

  3. Next, we want to set our requisite. We only want this state to run when there are changes to our configuration file, so let's use the watch option:

         - name: service.restart
         - m_name: mariadb
         - watch:
           - mysql_server_config

    We use the name declaration of our configuration state to denote what state we're waiting to change.

  4. Save and exit.

  5. Finally, as before, we can go ahead and test this:

     $ sudo salt 'minion2' state.sls mysql.server,mysql.config,mysql.restart test=true

    Note that now, as we test, we have to include all of our required states for the test (or highstate itself) to work.

    Unfortunately, because we're not running these state, only testing, we don't get to see our requisite in action. So, let's go ahead and remove the test parameter:

     $ sudo salt 'minion2' state.sls mysql.server,mysql.config,mysql.restart

    Instead of a comment denoting that there were no changes, our mysql_restart state instead just reports back as a true -- this means it ran successfully.

    And what happens if we run it again? Since our configuration file has updated, there will be no changes, and we get a comment about how the state was not run.

This lesson is only available to Linux Academy members.

Sign Up To View This Lesson
Or Log In

Looking For Team Training?

Learn More