saltstack之grains与pillar

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack之grains与pillar相关的知识,希望对你有一定的参考价值。

grains


收集被控主机获取信息,通常静态数据,CPU,操作系统,内核,虚拟化等....

简单函数匹配server2

[[email protected] salt]# cat top.sls
base:
  ‘roles:nginx‘:
    - match: grain
    - nginx.service
[[email protected] salt]# pwd
/etc/salt
[[email protected] salt]# cat grains 
roles: nginx
unit: miaomiao

查看信息

[[email protected] salt]# salt server2.lalala.com grains.items 
server2.lalala.com:
    ----------
    SSDs:
    biosreleasedate:
        01/01/2011
    biosversion:
        0.5.1
    cpu_flags:
        - fpu
        - de
        - pse
        - tsc

成功配置之后,还可以这样查看

[[email protected] salt]# salt server2.lalala.com grains.item roles
server2.lalala.com:
    ----------
    roles:
        nginx
[[email protected] salt]# salt -G ‘roles:nginx‘ test.ping
server2.lalala.com:
    True

grains的其他语法

[[email protected] salt]# salt -G ‘roles:nginx‘ cmd.run ‘uptime‘
server2.lalala.com:
     21:50:12 up  1:05,  1 user,  load average: 0.00, 0.02, 0.00
[[email protected] salt]# salt -C ‘* and not [email protected]:nginx‘ cmd.run ‘uptime‘
server3.lalala.com:

     21:51:00 up  1:03,  1 user,  load average: 0.00, 0.00, 0.00

自定义grains函数匹配server3

[[email protected] salt]# cat top.sls 
base:
  ‘roles:nginx‘:
    - match: grain
    - nginx.service

  ‘server3.lalala.com‘:
    - httpd.apache
[[email protected] _grains]# pwd
/srv/salt/_grains
[[email protected] _grains]# cat my_grains.py 
#!/usr/bin/env python
#coding: utf8

def my_grains():
    grains={}
    grains[‘salt‘]=‘saltstack‘
    grains[‘roles‘]=‘httpd‘
    return grains

同步自定义的grains函数

[[email protected] salt]# salt server3.lalala.com saltutil.sync_grains
server3.lalala.com:
    - grains.my_grains
[[email protected] salt]# salt server3.lalala.com grains.item salt
server3.lalala.com:
    ----------
    salt:
        saltstack

[[email protected] ~]#  salt-call --local grains.item salt
local:
    ----------
    salt:
        saltstack

pillar

敏感信息

[[email protected] web]# pwd
/srv/pillar/web
[[email protected] web]# cat init.sls 
{% if grains[‘roles‘] == ‘nginx‘ %}
apache: httpd
{% elif grains[‘roles‘] == ‘httpd‘ %}
apache: apache2
{% endif %}

[[email protected] web]# cd ..
[[email protected] pillar]# pwd
/srv/pillar
[[email protected] pillar]# cat top.sls 
base:
  ‘*‘:
    - web.init
[[email protected] pillar]# salt ‘*‘ saltutil.refresh_pillar
server3.lalala.com:
    True
server2.lalala.com:
    True
[[email protected] pillar]# salt ‘*‘ pillar.item apache
server3.lalala.com:
    ----------
    apache:
        apache2
server2.lalala.com:
    ----------
    apache:
        httpd


其他检测语法

[[email protected] pillar]# salt -I ‘apache:httpd‘ test.ping 
server2.lalala.com:
   True
[[email protected] pillar]# salt -I ‘apache:apache2‘ test.ping
server3.lalala.com:
   True
[[email protected] pillar]# salt -C ‘* and not [email protected]:apache2‘ test.ping
server2.lalala.com:
   True
[[email protected] pillar]# salt -C ‘* or not [email protected]:apache2‘ test.ping
server2.lalala.com:
   True
server3.lalala.com:
   True




[[email protected] pillar]# salt -C ‘[email protected]lt:saltstack or [email protected]:apache2‘ test.ping 
server3.lalala.com:
   True
server2.lalala.com:
   True
[[email protected] pillar]# salt -C ‘[email protected]:nginx or [email protected]:apache2‘ test.ping
server3.lalala.com:
   True
[[email protected] pillar]# salt -C ‘[email protected]/24 and not [email protected]:apache2‘ test.ping
server2.lalala.com:
   True

-I, --pillar

-G, --grain 

-S,--ipcidr        Match based on Subnet (CIDR notation) or IP address.

本文出自 “12049878” 博客,谢绝转载!

以上是关于saltstack之grains与pillar的主要内容,如果未能解决你的问题,请参考以下文章

SaltStack实战之数据系统 Grains VS Pillar

SaltStack之数据系统

saltstack grains与pillar使用和订制

saltstack 之pilla

saltstack grain pillar

Saltstack handbook:Grains and Pillar