saltstack數據系統之Grains,pillar
Grains
grains 負責采集客戶端(minion端)一些基本信息 ,這個也完全可以自定義,可以在客戶端自定義,然後自動匯報上來,也可以從服務器端定義然後推下去,采集完後再匯報上來(重啟才收集),也可以使用saltutil.sync_grains進行刷新
1.grains之收集信息
[[email protected] salt]# salt ‘node1*‘ grains.ls
[[email protected] salt]# salt ‘node1‘ grains.items查看收集的所有信息
[[email protected] salt]# salt ‘node1‘ grains.get fqdn
node1:
node1
[[email protected] salt]# salt ‘node1‘ grains.item fqdn顯示單個
node1:
----------
fqdn:
node1
[[email protected] salt]# salt ‘node1*‘ grains.get ip_interfaces:eth2
node1:
- 192.168.10.129
- fe80::20c:29ff:feca:35bf
2.grains之在指定服務器執行命令(-G匹配grains信息)
[[email protected]
node2:
CentOS
node1:
CentOS
[[email protected] salt]# salt -G os:CentOS cmd.run ‘w‘ #與grains裏存儲的os信息匹配,匹配到的機器執行 w
node2:
14:46:45 up 15:04, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 12:27 2:19m 0.06s 0.06s -bash
root pts/0 192.168.10.1 12:38 28:32 0.40s 0.29s bash
node1:
06:48:31 up 15:04, 2 users, load average: 0.08, 0.04, 0.04
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - Wed15 2:19m 0.33s 0.01s bash
root pts/0 192.168.10.1 04:40 1.00s 1.06s 0.39s /usr/bin/python
3.自定義信息
minion端自定義1:
[[email protected] ~]# vim /etc/salt/minion ###在node2自定義角色-webserver,memcache
grains:
roles:
- webserver
- memcache
[[email protected] ~]# /etc/init.d/salt-minion restart
測試:
[[email protected] salt]# salt -G ‘roles:memcache‘ cmd.run ‘echo hehe‘
node2:
hehe
註:在master端收集與grains裏自定義的角色匹配的機器執行echo命令
minion端自定義2:
[[email protected] ~]# cat /etc/salt/grains ###也可以在/etc/salt/grains自定義
web: nginx
[[email protected] ~]# /etc/init.d/salt-minion restart
測試
[[email protected] salt]# salt -G web:nginx cmd.run ‘w‘
node2:
14:59:37 up 15:17, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 12:27 2:32m 0.06s 0.06s -bash
root pts/0 192.168.10.1 12:38 7.00s 0.47s 0.36s bash
將minion端自定義的grains信息與top.sls聯系:
[[email protected] salt]# cat /srv/salt/apache.sls
apache-install:
pkg.installed:
- names:
- httpd
- httpd-devel
apache-service:
service.running:
- name: httpd
- enable: True
- reload: True
[[email protected] salt]# cat /srv/salt/top.sls
base:
‘web:nginx‘ :
- match: grain
- apache
[[email protected] salt]# salt ‘*‘ state.highstate
註:匹配web:nginx,使用grains匹配,匹配成功的執行apache.sls這個狀態
Pillar
數據存儲在master端,在master端定義,指定給對應的minion,可以使用saltutil.refresh_pillar刷新,常用於存儲master指定的數據,只有指定的minion可以看到。用於敏感數據保存
1.系統默認pillar
[[email protected] ~]# vim /etc/salt/master
pillar_opts: Ture #這裏只是查看一下,查看之後將其關閉
[[email protected] ~]# /etc/init.d/salt-master restart
[[email protected] ~]# salt ‘*‘ pillar.items #查看系統默認的pillar,key value的形式
[[email protected] ~]# salt ‘*‘ pillar.items
node1:
----------
master:
----------
__role:
master
auth_mode:
1
auto_accept:
False
cache_sreqs:
True
cachedir:
/var/cache/salt/master
cli_summary:
False
.......................................
2.自定義pillar
#編輯配置文件
[[email protected] ~]# vim /etc/salt/master
pillar_roots:
base:
- /srv/pillar
[[email protected] ~]# mkdir /srv/pillar
#寫一個apache的狀態
[[email protected] ~]# cat /srv/pillar/apache.sls
{% if grains[‘os‘] == ‘CentOS‘ %}
apache: httpd
{% elif grains[‘os‘] == ‘Debian‘ %}
apache: apache2
{% endif %}
註:使用grains收集信息,如果系統是centos,那麽apache的名字就叫httpd。系統是Debian的時候,apache叫apache2
#寫一個tops.sls
[[email protected] ~]# cat /srv/pillar/top.sls #將定義的apache狀態指定給所有minion
base:
‘*‘:
- apache
[[email protected] ~]# salt ‘*‘ saltutil.refresh_pillar刷新一下
node2:
True
node1:
True
[[email protected] ~]# salt ‘*‘ pillar.items
node1:
----------
apache:
httpd
node2:
----------
apache:
httpd
[[email protected] ~]# salt -I ‘apache:httpd‘ test.ping
node1:
True
node2:
True
本文出自 “feng” 博客,請務必保留此出處http://fengxiaoli.blog.51cto.com/12104465/1958010
saltstack數據系統之Grains,pillar