How To Create Your First Salt Formula

Introduction

A novel method for infrastructure management, Salt is a completely open source configuration management and remote execution tool. It is not only simple enough to be operational within minutes, but is also scalable enough for managing a large number of servers and is speedier enough for communicating with all of them within seconds.

Salt is a product of an amazing and open company called SaltStack. All features and code of Salt are released under Apache 2.0 license and it can be downloaded from https://github.com/saltstack/salt and any favored package manager of the OS.

We will be accomplishing a number of tasks in this guide, including:

  • Creating your foremost Salt Formula which will be used for configuring your server through the software given below:
    • Vim
    • Apache web server
    • PHP
    • git

We presume the following for this tutorial:

  • The previous guide Installation of Salt on Ubuntu 12.04 has been looked into.
  • You have logged in as the default root user
  • In this guide, we are making an assumption that the name of your server is ‘minion01’. Wherever you see ‘minion01’, replace it with the name of your server that you can find through salt-key -L

In our previous tutorial of Salt installation a few easy commands were run, like Nginx installation command. This is a convenient method of executing ad-hoc commands, however you should not go on configuring your infrastructure in this way.

By generating a number of Salt Formulas any configuration can be reproduced reliably again and again. Also, these Salt Formulas provide a good documentation of whatever is deployed on your servers. Now no more pondering over what is installed on those 6year old servers!

Salt Formulas are easy yaml text files that exist on the Salt Master in /srv/salt/ by default.

We will begin by developing a Salt Formula for ensuring that Vim is deployed.

vi /srv/salt/vim.sls

The text given below should be added to your vim.sls:

vim:
  pkg:
    - installed

The description of what is happening through each line is given below.

ID Declaration is being done through the first line, importantly this stanza’s  “label”. vim will be utilized for the name of our package. It is necessary that the name used here and the real package name utilized by your package manager should match up, like  “vim” for Ubuntu and “vim-core” for CentOS. (Actually, any random text can be used for ID Declaration and the real package name can be specified below but it has been done here itself for the sake of simplicity.)

Second line is known as State Declaration. This pertains to that particular Salt State which we are going to utilize. This example makes use of the “pkg” state. The Salt offers a number of states, all of which are listed here:

http://docs.saltstack.com/ref/states/all/index.html.

It is also possible to develop your very own states!

Function Declaration is being done through the third line. This pertains to the function’s name within the state module that is being executed. It is “installed” in this particular example . This tense is being used as Salt States are idempotent by nature i.e. they can be executed again and again and and not even a single thing on the server will change if the server is already conforming to the intended state.

That’s it! Now this state can be applied to our server as given below:

salt 'minion01' state.sls vim

Suppose we want to ensure that nano is eliminated from our system in the meantime?
Your vim.sls should be made to look as given below:

vim:
  pkg:
    - installed

nano:
  pkg:
    - removed

Can you follow the pattern here?

In the subsequent example we will be installing PHP and Apache web server and PHP simultaneously.

Make a file called “webserver.sls”:

vi /srv/salt/webserver.sls

To “webserver.sls” add the following:

webserver_stuff:
  pkg:
    - installed
    - pkgs:
      - apache2
      - php5
      - php5-mysql

In this instance, observe the argument “- pkgs:” . Every  item below “-
pkgs:” would together be passed to the package manager of your OS  to be deployed
together. This translates to the occurrence of only a single call to “yum” or “apt”. In case you need to deploy a long list of packages then this method is the most effective way of deploying them.

Again, this Formula can be applied to your server as given below:

salt 'minion01' state.sls webserver

Now, we will take the last example of deploying git. You can now easily guess as to how we will do it.

vi /srv/salt/git.sls

The contents:

git:
  pkg:
    - installed

Again, for applying this new configuration, the following needs to be done:

salt 'minion01' state.sls git

In order to apply all these configs simultaneously, following command can be executed:

salt 'minion01' state.sls vim,webserver,git

Coming to this tutorial’s last section, we will understand about the “highstate” concept. A “highstate” is a method utilized by Salt for determining dynamically which Salt Formulas needs to be applied to a specific minion.

Begin by executing a “highstate” as given below:

salt 'minion01' state.highstate

With this command the Minion gets downloaded and examines a file named the “top file” from the Salt Master. This file can be located on the Salt Master at /srv/salt/top.sls by default. A primary top file looks as follows:

base:
  '*':
    - vim
  'minion*':
    - git
    - webserver
  'minion02':
    - mongodb

In top.sls instance we possess a “base” environment. Meanwhile we will be sticking with only a default “base” environment. As specified before, when the minion is asked for executing a highstate, the minion calls for the top.sls from the master and looks for the matching formulas. The wildcard-‘*’ translates to ALL the minions applying the complete list of formulas that are there below it; which is only the “vim” formula in our case. Also, ‘minion*’ in the fourth line is similar to our minion. This means that our minion would apply the “webserver” and “git”  formulas. Our minion is NOT matching with ‘minion02’ so it would not apply the “mongodb” formula.

You can observe that in this exampleonly the minion’s name or ID are being matched with standard globbing. However, it is also possible to match on IP address and its range, pcre, grains and different other things. A list of ways to match can be found here.

Conclusion

We have developed only some key Salt formulas that utilizes in-built states of Salt for configuring our server. Yu can do a lot more with Salt!

A fabulous way of learning more about Salt is the Official SaltStack Walkthrough :
http://docs.saltstack.com/topics/tutorials/walkthrough.html

You can also read comprehensive documentation of Salt to know about all the methods through which it can aid you in managing your infrastructure:
http://docs.saltstack.com

Salt Development takes place at: https://github.com/saltstack/salt

You can also join the Salt community where anyone can post questions about SaltStack products and get the answers:
https://groups.google.com/forum/#!forum/salt-users

KB Admin has written 28 articles

Leave a Reply