saltstack数据系统之Grains,pillar
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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] salt]# salt ‘node*‘ grains.get os
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 [email protected] 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 [email protected] 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 [email protected] 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]1 ~]# 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的主要内容,如果未能解决你的问题,请参考以下文章
saltstack自动化运维系列②之saltstack的数据系统