SaltStack examples

SaltStack platform or Salt is a Python-based open source configuration management software and remote execution engine. Supporting the “Infrastructure as Code” approach to deployment and cloud management, it competes primarily with Puppet, Chef, and Ansible.

We will use some of default functions., Please  considerate  the  Link  bellow about the installation of  Saltstack master and nodes. and  please  remember all these solutions are  tested in our Unixmen virtual machines, jut to be sure that every thing is working fine.

Lets  start :

 

 

Install and configure SaltStack server in Ubuntu x64

Show disk usage for all minions:
jamal@saltmaster:~$ sudo salt ‘*’ disk.usage

Show exist documentations for all minions:
jamal@saltmaster:~$ sudo salt ‘*’ sys.doc

Check network status to all minions:
jamal@saltmaster:~$ sudo salt ‘*’ test.ping

Look at the /etc file system for all minions:
jamal@saltmaster:~$ sudo salt ‘*’ cmd.run ‘ls -l /etc’

Get system information from all minion installed servers:
jamal@saltmaster:/srv/pillar$ sudo salt “*” grains.items

Check just FreeBSD minion:
jamal@saltmaster:/srv/pillar$ sudo salt -G ‘os:FreebSD’ test.ping
node4salt.opeensource.az:
True

pkg function automatically get minion internal functionality. This means, the pkg.install command automatically will use yum for RedHat/Centos, for apt Ubuntu/Debian and pkg for FreeBSD.

Install vim package to node4salt.opensource.az FreeBSD server as follows:
jamal@saltmaster:~$ sudo salt ‘node4salt.opensource.az’ pkg.install vim

Get information from all minions about network card names, IP address, subnets with masks and MAC address:
jamal@saltmaster:~$ sudo salt ‘*’ network.interfaces

Find python path’s from all minions:
jamal@saltmaster:~$ sudo salt ‘*’ grains.item pythonpath –out=pprint

With state.sls name have different execution module which needs argument as SLS file. In next steps we will use state.sls function in details.

Create top.sls file in the already created /srv/salt folder and add the following lines (Default environment is base. In this syntax we told, * symbol is for all minions. The content of install.sls(this file must be in the same folder where placed top.sls file) file will be executed for all minions. Same for apache.sls file content must be as apache:
jamal@saltmaster:~$ sudo cat /srv/salt/top.sls
base:
  ‘*’:
    – install
    – apache

jamal@saltmaster:~$ sudo cat /srv/salt/install.sls
utilitler:
  pkg.installed:
    {% if grains[‘os’] == ‘CentOS‘ %}
    – name: nload
    {% elif grains[‘os’] == ‘Ubuntu‘ %}
    – name: nload
    {% elif grains[‘os’] == ‘FreeBSD‘ %}
    – name: nload
    {% endif %}

The difference from configuration control utilities is SaltStack by default not execute state configurations. This is not by default but we can do this. With the following command we will apply all state configurations to all minions. This means state.sls call’s and execute top.sls file and top.sls calls and execute install.sls file. At the end in all minions will be installed nload package.
jamal@saltmaster:~$ sudo salt ‘*’ state.highstate

We can call and test any sls file with state.sls execution module. For example synchronize one file from saltmaster server to all minions. Add the following content to the /srv/salt/apache.sls file. In this lines we tell synchronize tesfile file from /srv/salt/fayllar folder to all minions /etc/testfile file path. The owner of file will be root and permissions will be 644:
jamal@saltmaster:/srv/salt$ sudo cat apache.sls
/etc/testfile:
 file.managed:
        – source: salt://fayllar/testfile
        – user: root
        – mode: 644

Create the folder and add some lines to file placed in this folder:
jamal@saltmaster:/srv/salt$ sudo mkdir /srv/salt/fayllar
jamal@saltmaster:/srv/salt$ sudo cat /srv/salt/fayllar/testfile
dbname= db
dbpass= user
dbpass= pass

Then send this file to all minions:
jamal@saltmaster:/srv/salt$ sudo salt ‘*’ state.sls apache

For install a lot of packages to all minions in the same time, create file with /etc/srv/mypack.sls name and add the following lines. In this file we tell install packages with names mercurial and git to all minions.
jamal@saltmaster:/srv/salt$ sudo cat /etc/srv/mypack.sls
mypack:
   pkg:
     – installed
     – pkgs:
       – mercurial
       – git

Install selected packages to all minions:
jamal@saltmaster:/srv/salt$ sudo salt ‘*’ state.sls mypack

For real-time debugging of minions we can use the following command:
jamal@node1salt:~$ sudo salt-minion -l debug

About Pillar
Pillar gives tree structure possibility for data defining. With pillar we can control send only selected and secure data from master server to the minions.

Sometimes you can be mistaken with grain and pillar. But remember grain’s saves data generated from minions. Information’s about CPU and OS places in grains. But pillar saves generated (on the SaltMaster server) information about minions.

We can see the pillar information about minions with the following command:
jamal@saltmaster:/srv/pillar$ sudo salt ‘*’ pillar.items

For test minions create some files and check. Firsly we create folder and needs SLS files.
jamal@saltmaster:/srv/pillar$ sudo mkdir -p /srv/pillar/{pkg,users}

Add the following content to the top.sls file:
jamal@saltmaster:/srv/pillar$ sudo cat /srv/pillar/top.sls
base:
  ‘*’:
    – data
    – users
    – pkg

Then add needed calls path’s to the data.sls file.
jamal@saltmaster:/srv/pillar$ sudo cat /srv/pillar/data.sls
info: some data

Set used data with UID:
jamal@saltmaster:/srv/pillar$ sudo cat /srv/pillar/users/init.sls
users:
  thatch: 1000
  shouse: 1001
  utahdave: 1002
  redbeard: 1003

With the following example we define selected packages to the corresponding Linux/UNIX distributives:
jamal@saltmaster:/srv/pillar$ sudo cat /srv/pillar/pkg/init.sls
pkgs:
  {% if grains[‘os_family’] == ‘CentOS’ %}
  apache: httpd
  vim: vim-enhanced
  {% elif grains[‘os_family’] == ‘Ubuntu’ %}
  apache: apache2
  vim: vim
  {% elif grains[‘os’] == ‘FreeBSD’ %}
  mysql: mysql55-server
  vim: vim
  {% endif %}

Send new pillar data’s to all minions:
jamal@saltmaster:/srv/pillar$ sudo salt ‘*’ saltutil.refresh_pillar
node1salt.opensource.az:
True
node4salt.opeensource.az:
True
node2salt.opensource.az:
True
node3salt.opensource.az:
True