Monday, January 4, 2016

Salt Grains and Pillars

Grains

Grains are minion data calculated when minion starts. It contains static data like OS version, CPU cores, etc. This data is generated in minion and presented to master.

List the default grains configured on a minion.


  • salt  vagrant-centos65.vagrantup.com grains.ls   # This just lists the keys
  • salt vagrant-centos65.vagrantup.com grains.items  # This lists all the keys & values
  • salt vagrant-centos65.vagrantup.com grains.item os  --out=txt  # single value

 Setting Grains

  • salt vagrant-centos65.vagrantup.com grains.setval myenv prod 
  • salt centos65-minion grains.setval myenv stage
  • salt ubuntu-14.04-amd64-vbox grains.setval myenv dev
  • salt vagrant-centos65.vagrantup.com grains.setval roles '[webserver,appserver]'
  • salt centos65-minion grains.setval roles '[webserver,appserver,database]'
  • salt ubuntu-14.04-amd64-vbox grains.setval roles '[webserver,appserver,database]'
  • salt \* grains.item myenv roles   # query all of these roles

Pillars

Pillars are data from the Master. Pillar data is stored on the master. But the data is available only for the given minion.

Querying pillar data

  • salt \* pillar.items

Configuring pillar root in masters configuration

--------------------------------------------
# cat /etc/salt/master.d/pillar.conf
pillar_roots:
  base:
    - /srv/salt/pillar/base
--------------------------------------------
  • service salt-master restart

Pillar Top file

--------------------------------------------
# cat /srv/salt/pillar/base/top.sls
base:
  '*':
    - default
--------------------------------------------

--------------------------------------------
# cat /srv/salt/pillar/base/default.sls
my_data: some data for stuff
--------------------------------------------

Pillar data for user management

--------------------------------------------
# cat /srv/salt/pillar/base/users/all.sls
users:
  wilma: 2001
  fred: 2002
  barney: 2003
  betty: 2004
--------------------------------------------

--------------------------------------------
# cat /srv/salt/pillar/base/users/stage.sls
users:
  wilma: 2001
  barney: 2003
  betty: 2004
--------------------------------------------

--------------------------------------------
# cat /srv/salt/pillar/base/users/dba.sls
users:
  wilma: 2001

--------------------------------------------

Re-touch Top file

--------------------------------------------
# cat /srv/salt/pillar/base/top.sls
base:
  '*':
    - default


'G@myenv:prod and G@roles:database':
  - match: compound
  - users.dba

'myenv:stage':
  - match: grain
  - users.stage

'myenv:dev':
  - match: grain
  - users.all

--------------------------------------------

Validate the top file

[root@vagrant-centos65 base]# salt \* pillar.item users

Sunday, January 3, 2016

Salt states - Quick Start

Set up the file server

  • As per the master configuration file /etc/salt/master, the  master will automatically include all config files from /etc/salt/master.d/*.conf
  • Create the below file in master
# cat /etc/salt/master.d/file-roots.conf
file_roots:
    base:
    - /srv/salt/file/base
  • Restart the Salt master
# service salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
  • This sets up our Salt master with our file_roots directory.

SLS file

  • Create the below SLS (Salt State) file