saltstack初窥——grains介绍

官方文档:

http://docs.saltstack.com/topics/targeting/grains.html
http://docs.saltstack.com/ref/modules/all/salt.modules.grains.html
http://docs.saltstack.com/ref/states/all/salt.states.grains.html

介绍:

Static bits of information that a minion collects about the system when the minion first

starts. (收集minion的系统信息)
基本用法:
grains.ls:

Return a list of all available grains

salt ‘*’ grains.ls

grains.items:

Return the grains data

salt ‘*’ grains.items

salt ‘*’ grains.items sanitize=True

grains.item:

Return a single component of the grains data

salt ‘*’ grains.item os

Return multiple components of the grains data

salt ‘*’ grains.item os osrelease oscodename

salt ‘*’ grains.item host sanitize=True

grains.get:

Attempt to retrive the named value from grains, if the named value is not

for the dict. This means that if a dict in grains looks like this:

salt ‘*’ grains.get pkg:apache

grains.setval:

Set a grains value in the grains config file

salt ‘*’ grains.setval key val

saltutil.sync_grains:

Sync the grains from the _grains directory on the salt master file

to grab the contents of the _grains directory, base is the default

salt ‘*’ saltutil.sync_grains
自定义grains:

1. 在master上通过_grains目录下的py文件定义
mkir /srv/salt/_grains
cd /srv/salt/_grains
/srv/salt/_grains/my.py

def haha():
grains = {}
grains[‘who’] = ‘salt’
return grains

同步到minions
salt ‘‘ saltutil.sync_grains 或者 salt ‘‘ saltutil.sync_all
查看
salt ‘*’ grains.get who 或者 salt ‘web5-89’ grains.item who

2. 通过grains模块的setval方法
salt ‘*’ grains.setval who salt

3. 通过states的grains方法
/srv/salt/gr.sls

cheese:
grains.present:

- value: edam</pre>

salt ‘‘ state.sls gr
salt ‘
‘ grains.item cheese
4. 在minion上定义

sed -i ‘/default_include/s/#//‘ /etc/salt/minion
/etc/init.d/salt-minion restart

/etc/salt/minion.d/test.conf

grains:
me:

- name: netcat
- site:
  - houzi.com
  - houzi.net</pre>

需要重启salt-minion使配置生效

在master上执行:
salt ‘*’ grains.item me

 

注:0.17.0以前的版本不支持remove grains
salt.modules.grains.remove(key, val)
New in version 0.17.0.