Ok so we’ve learned how to install salt , targeting and sending commands to minions let’s continue with salt states. Salt states are used for configuration management.  We don’t have to install anything else to  use state system because it is already available with salt setup.

Salt states are built on sls formulas.

( Sls means SaLt State ). These state files are kept on Salt’s file server. ( You can check it in master configuration , in “file  file server setting” section. By default it is like below if you uncomment and make change  )

file_roots:
base:
- /srv/salt

Ok before talking about top file we can make a basic sls formula and create our first sls file. In our example I will install nginx to our minions.

First we will create a file nginx.sls in our /srv/salt folder.

nginx:
pkg:
- installed

And now we can run  salt ‘*’ state.sls nginx on our master server. And by running this command nginx.sls file will run on minions and nginx will be installed on our minions.

root@salt:/srv/salt# <strong>salt "minion-*" state.sls nginx</strong>
minion-0.wekanban.com:
----------
State: - pkg
Name: nginx
Function: installed
Result: True
Comment: Package nginx is already installed
Changes:

Summary
------------
Succeeded: 1
Failed: 0
------------
Total: 1
minion-1.wekanban.com:
----------
State: - pkg
Name: nginx
Function: installed
Result: True
Comment: The following packages were installed/updated: nginx.
Changes: nginx-common: { new : 1.1.19-1ubuntu0.5
old :
}
nginx-full: { new : 1.1.19-1ubuntu0.5
old :
}
nginx: { new : 1.1.19-1ubuntu0.5
old :
}
libxslt1.1: { new : 1.1.26-8ubuntu1.3
old :
}
libjpeg-turbo8: { new : 1.1.90+svn733-0ubuntu4.3
old :
}
libgd2-noxpm: { new : 2.0.36~rc1~dfsg-6ubuntu2
old :
}
libjpeg8: { new : 8c-2ubuntu7
old :
}

Summary
------------
Succeeded: 1
Failed: 0
------------
Total: 1

As you see nginx was already installed on minion-0. On minion-1 we successfully installed nginx. In our basic sls file you can see the definition of each line.

nginx: # ID declaration
pkg: # state declaration
- installed # function declaration

Now let’s add more definitions to our sls file.

nginx: # ID declaration
pkg: # state declaration
- installed # function declaration
service: # state declaration
- running # function declaration
- require: # require statement
- pkg: nginx

Here first we declare that nginxg should be installed. And next nginx service should be running. But as you see there is a requirement statement and it says that nginx service can run only if nginx successfully installed. As said in salt documentation, The require statement makes sure that the required component is executed before and that it results in success. For more information about requisites visit http://docs.saltstack.com/ref/states/requisites.html

In my next post we will see top file.

Saltstack Targeting part 6
Amazon EC2 Part 1

Onur SALK

IT Infrastructure Manager, PMP, AWS Certified Solutions Architect

Leave a Reply