grains
收集被控主机获取信息,通常静态数据,CPU,操作系统,内核,虚拟化等....
简单函数匹配server2
[[email protected] salt]# cat top.slsbase: ‘roles:nginx‘: - match: grain - nginx.service
[[email protected] salt]# pwd/etc/salt[[email protected] salt]# cat grains roles: nginxunit: 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 rolesserver2.lalala.com: ---------- roles: nginx[[email protected] salt]# salt -G ‘roles:nginx‘ test.pingserver2.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_grainsserver3.lalala.com: - grains.my_grains[[email protected] salt]# salt server3.lalala.com grains.item saltserver3.lalala.com: ---------- salt: saltstack [[email protected] ~]# salt-call --local grains.item saltlocal: ---------- 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_pillarserver3.lalala.com: Trueserver2.lalala.com: True[[email protected] pillar]# salt ‘*‘ pillar.item apacheserver3.lalala.com: ---------- apache: apache2server2.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: Trueserver3.lalala.com: True
[[email protected] pillar]# salt -C ‘[email protected]:saltstack or [email protected]:apache2‘ test.ping server3.lalala.com: Trueserver2.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.
时间: 2024-11-05 20:45:32