SaltStack提供一个接口用于获取各个minion底层系统的信息,这个接口就叫做grains interface。
grains是minion主机在启动的时候加载的数据,是静态的数据,比如内核版本,操作系统版本等信息
grains不区分大小写
匹配操作系统是CentOS的minion
$ sudo salt -G "os:CentOS" test.ping jidong-fileserver: True localhost.localdomain: True gintama-qa-server: True jialebi-qa-server: True
查看所有minion主机的OS
$ sudo salt '*' grains.item os jidong-fileserver: ---------- os: CentOS localhost.localdomain: ---------- os: CentOS gintama-qa-server: ---------- os: CentOS jialebi-qa-server: ---------- os: CentOS
$ sudo salt '*' grains.item os osrelease oscodename jidong-fileserver: ---------- os: CentOS oscodename: Final osrelease: 6.4 localhost.localdomain: ---------- os: CentOS oscodename: Final osrelease: 5.10 gintama-qa-server: ---------- os: CentOS oscodename: Final osrelease: 6.5 jialebi-qa-server: ---------- os: CentOS oscodename: Final osrelease: 6.5
查看CPU个数
sudo salt -G 'cpuarch:x86_64' grains.item num_cpus
这里用到grains模块的item函数
/usr/lib/python2.6/site-packages/salt/modules/grains.py
def item(*args, **kwargs): ''' Return one or more grains CLI Example: .. code-block:: bash salt '*' grains.item os salt '*' grains.item os osrelease oscodename Sanitized CLI Example: .. code-block:: bash salt '*' grains.item host sanitize=True ''' ret = {} for arg in args: try: ret[arg] = __grains__[arg] except KeyError: pass if salt.utils.is_true(kwargs.get('sanitize')): for arg, func in _SANITIZERS.items(): if arg in ret: ret[arg] = func(ret[arg]) return ret
列出所有的grains
$ sudo salt '*' grains.ls jidong-fileserver: - cpu_flags - cpu_model - cpuarch - defaultencoding - defaultlanguage - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gpus - host - hwaddr_interfaces - id - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - localhost - master - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - path - ps - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - server_id - shell - virtual - zmqversion
def ls(): # pylint: disable=C0103 ''' Return a list of all available grains CLI Example: .. code-block:: bash salt '*' grains.ls ''' return sorted(__grains__)
获取所有的grains数据
$ sudo salt '*' grains.items jidong-fileserver: ---------- cpu_flags: - fpu - vme - de - pse - tsc - msr - pae - mce - cx8 - apic - sep - mtrr - pge - mca - cmov - pat - pse36 - clflush - dts - acpi - mmx - fxsr - sse - sse2 - ss - ht - tm - pbe - syscall - nx - rdtscp - lm - constant_tsc - arch_perfmon - pebs - bts - rep_good - xtopology - nonstop_tsc - aperfmperf - pni - pclmulqdq - dtes64 - monitor - ds_cpl - vmx - est - tm2 - ssse3 - cx16 - xtpr - pdcm - pcid - sse4_1 - sse4_2 - popcnt - tsc_deadline_timer - xsave - lahf_lm - arat - epb - xsaveopt - pln - pts - dts - tpr_shadow - vnmi - flexpriority - ept - vpid cpu_model: Intel(R) Pentium(R) CPU G850 @ 2.90GHz cpuarch: x86_64 defaultencoding: UTF8 defaultlanguage: en_US domain: fqdn: jidong-fileserver fqdn_ip4: - 10.10.41.2 fqdn_ip6: gpus: |_ ---------- model: 2nd Generation Core Processor Family Integrated Graphics Controller vendor: intel host: jidong-fileserver hwaddr_interfaces: ---------- br0: 90:2b:34:b5:5f:08 eth0: 90:2b:34:b5:5f:08 lo: 00:00:00:00:00:00 id: jidong-fileserver ip_interfaces: ---------- br0: - 10.10.41.2 eth0: lo: - 127.0.0.1 ipv4: - 10.10.41.2 - 127.0.0.1 ipv6: - ::1 - fe80::922b:34ff:feb5:5f08 kernel: Linux kernelrelease: 2.6.32-504.16.2.el6.x86_64 localhost: jidong-fileserver master: 10.10.41.20 mem_total: 15843 nodename: jidong-fileserver num_cpus: 2 num_gpus: 1 os: CentOS os_family: RedHat osarch: x86_64 oscodename: Final osfinger: CentOS-6 osfullname: CentOS osmajorrelease: 6 osrelease: 6.4 path: /sbin:/usr/sbin:/bin:/usr/bin
def items(sanitize=False): ''' Return all of the minion's grains CLI Example: .. code-block:: bash salt '*' grains.items Sanitized CLI Example: .. code-block:: bash salt '*' grains.items sanitize=True ''' if salt.utils.is_true(sanitize): out = dict(__grains__) for key, func in _SANITIZERS.items(): if key in out: out[key] = func(out[key]) return out else: return __grains__
grains可以在minion的配置文件中静态指定。同样可以在SLS文件中使用或者通过Salt State System获取
在/etc/salt/minion文件中指定
grains: roles: - webserver - memcache deployment: datacenter4 cabinet: 13 cab_u: 14-15
$ sudo salt 'jialebi-qa-server' grains.item roles jialebi-qa-server: ---------- roles: - webserver - memcache $ sudo salt 'jialebi-qa-server' grains.item cabinet jialebi-qa-server: ---------- cabinet: 13 $ sudo salt 'jialebi-qa-server' grains.item deployment jialebi-qa-server: ---------- deployment: datacenter4 $ sudo salt 'jialebi-qa-server' grains.item cab_u jialebi-qa-server: ---------- cab_u: 14-15 $ sudo salt -G "roles:webserver" grains.item host jialebi-qa-server: ---------- host: jialebi-qa-server
如果不想在/etc/salt/minion中添加grains信息,可以单独写到/etc/salt/grains
roles: - webserver - memcache deployment: datacenter4 cabinet: 13 cab_u: 14-15
在top.sls文件中匹配grains
在minion端正确配置grains后,Pillar的top文件或者Hightstate的过程中会变得更有效率
'node_type:web': - match: grain - webserver 'node_type:postgres': - match: grain - database 'node_type:redis': - match: grain - redis 'node_type:lb': - match: grain - lb
{% set node_type = salt['grains.get']('node_type', '') %} {% if node_type %} 'node_type:{{ self }}': - match: grain - {{ self }} {% endif %}
编写grains
grains模块的函数需要返回一个字典类型数据,字典的key就是grains的名称
自定义grains需要放置到Master配置文件中file_roots指定的路径的_grains目录下,默认路径是/srv/salt/_grains/。当执行state.highstate时自定义grains将会被分发到所有的minon主机上
或者执行saltutil.sync_grains或saltutil.sync_all
grains易于编写,只需要编写的模块可以返回一个字典数据就行。
#!/usr/bin/env pythondef yourfunction(): # initialize a grains dictionary grains = {} # Some code for logic that sets grains like grains['yourcustomgrain']=True grains['anothergrain']='somevalue' return grains
grains是静态数据,如果需要添加的数据是动态的,可以考虑使用pillar
需要注意的是,在第一次执行highstate之后,自定义的grains才能在top文件中可用
grains数据优先级
Core grains.
Custom grain modules in _grains
directory, synced to minions.
Custom grains in /etc/salt/grains
.
Custom grains in /etc/salt/minion
.
后者会覆盖前者的
参考文档:
http://docs.saltstack.com/en/latest/topics/targeting/grains.html