Saltstack : Creating and Deploying Salt Formulas On Minions

Test if the minion is connected to the server using the ping command. It returns true if the master and the minion is in connection. Here am using my minion “jarvis”

salt Jarvis test.ping

The application configuration for the minion is written in a state file with .sls (salt state file) extension.
The state files should be present inside /srv/salt folder. Create salt folder inside /srv

mkdir –p  /srv/salt
states

In this tutorial am going to create and deploy a simple sate file for installing and starting apache webserver .
You can have the salt file inside the /srv/salt folder. But for good file organization, create a folder named webserver inside the salt folder.

mkdir  -p /srv/salt/webserver

Create a apache.sls file inside webserver folder.

vi apache.sls

Copy the following inside the apache.sls file.

Apache-webserver:
pkg.installed:
{% if grains['os'] == 'Ubuntu' %}
- name: apache2
{% endif %}
service.running:
{% if grains['os'] == 'Ubuntu' %}
- name: apache2
{% endif %}

Apache-webserver: It is an user defined name. It can by any name, which serves as a n id.
pkg.installed: This command ensures that the package defined under this command will be installed

{% if grains['os'] == 'Ubuntu' %}
- name: apache2
{% endif %}

Apache package name differs with OS family. So we use python templating to provide appropriate package name using salt grains. Grains is a module which collects the static information about a minion. So , in the above code it checks if the OS is Ubuntu. If yes then apache2 package will be installed. Got redhat system , you have to define grains[‘os’] == ‘redhat’ and the package name will be httpd.

service.running:
{% if grains['os'] == 'Ubuntu' %}
- name: apache2
{% endif %}

Service.running will ensure that the installed service is in running state. The service name is provided to the state using grains os check.

Now, we have a state file, which will install and start apache webserver. Let say , you have to deploy this state to one of minions in your infrastructure. Here is where top file comes in to play. Top.sls file should be present inside /srv/salt folder.

In top.sls file , we define the minions and states. Top file decides which state should be run on a particular minion or on all minions. You can define various environments inside the top file. So that , you can set specific states, which would run only in the specified environments.

Create a top.sls file inside /srv/salt folder. Top.sls for the created apache state is described below.

base:
'jarvis':
- webserver.apache

Base: is the default environment. Normally in base , we would define states that has to be applied on all the minions under the master.

‘jarvis’ is the minion name. If you mention * instead of Jarvis , it will include all the minions under the master.

Webserver.apache is the path to the salt state file. Webserver is the folder inside /srv/salt and apache is the identification for apache.sls file inside webserver folder.

Deploying the state on Minions:
You can deploy the state on a minion from both the master and minion. In other configuration management tools like chef and puppet , you need to configure extra features like mcollective to push a configuration to a node. Push feature in salt comes by default unlike other configuration management tools.

From master:
The following command deploys the state on all minions paralelly as described in the top file.

salt ‘*’ salt.highstate
highstate

If you use the minion name instead of  * , the state will be deployed on on that particular minion.

salt Jarvis salt.highstate
jarvis highstate

The following command installs ntp on all the minions without having a state file. You can check if a particular package is installed using this command.

salt '*' state.single pkg.installed name=ntp

From Minion:
Execute the following command to deploy the salt formulas mentioned in the top file from the minion.

salt-call state.highstate
0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like