SaltStack数据系统
saltstack有两种数据系统:grains与pillar
1、SaltStack数据系统之grains
grains可以收集minion端的静态数据(即机器启动时收集一次,机器运行时出现修该参数不会收集),可以做匹配(可以匹配操作系统等),可以用在配置管理操作。
1.1、grains的三种用法
- 可以收集基础信息
- 可以在minion客户端匹配Grains进行批量执行
- 在top.sls里面匹配Grains进行批量执行
1.2、列出所有grains的名称
# salt 'saltstack-41' grains.ls #列出所有grains的名称
saltstack-:
- SSDs
- biosreleasedate
- biosversion
- cpu_flags
- cpu_model
- cpuarch
- domain
- fqdn
- fqdn_ip4
- fqdn_ip6
- gpus
- host
- hwaddr_interfaces
- id
- init
- ip4_interfaces
- ip6_interfaces
- ip_interfaces
- ipv4
- ipv6
- kernel
- kernelrelease
- locale_info
- localhost
- lsb_distrib_id
- machine_id
- manufacturer
- master
- mdadm
- mem_total
- nodename
- num_cpus
- num_gpus
- os
- os_family
- osarch
- oscodename
- osfinger
- osfullname
- osmajorrelease
- osrelease
- osrelease_info
- path
- productname
- ps
- pythonexecutable
- pythonpath
- pythonversion
- saltpath
- saltversion
- saltversioninfo
- selinux
- serialnumber
- server_id
- shell
- systemd
- virtual
- zmqversion
1.3、显示grains名称对应的内容
# salt 'saltstack-41' grains.items #显示Grains的所有的内容
saltstack-:
----------
SSDs:
biosreleasedate:
//
biosversion:
6.00
cpu_flags:
- fpu
- vme
- de
- pse
- tsc
- msr
- pae
- mce
- cx8
- apic
- sep
- mtrr
- pge
- mca
- cmov
- pat
- pse36
- clflush
- dts
- mmx
- fxsr
- sse
- sse2
- ss
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- pebs
- bts
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- aperfmperf
- eagerfpu
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- ida
- arat
- epb
- pln
- pts
- dtherm
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- rdseed
- adx
- smap
- xsaveopt
cpu_model:
Intel(R) Core(TM) i5-5200U CPU @ .20GHz
cpuarch:
x86_64
domain:
fqdn:
saltstack-
fqdn_ip4:
- 172.16.1.41
fqdn_ip6:
- fe80::89d4:576f::41b
- fe80::8dbd:f1ae:d9d3:b966
- fe80::5bae::65d3:7d6b
- fe80::3ac2:51c0:e6ac:
- fe80:::a475:ac57:9b3e
- fe80::5c8:50dd::5eba
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
unknown
host:
saltstack-
hwaddr_interfaces:
----------
eth0:
:0c::ab::4d
eth1:
:0c::ab::
lo:
:::::
id:
saltstack-
init:
systemd
ip4_interfaces:
----------
eth0:
- 10.0.0.41
eth1:
- 172.16.1.41
lo:
- 127.0.0.1
ip6_interfaces:
----------
eth0:
- fe80::5c8:50dd::5eba
- fe80::8dbd:f1ae:d9d3:b966
- fe80:::a475:ac57:9b3e
eth1:
- fe80::89d4:576f::41b
- fe80::5bae::65d3:7d6b
- fe80::3ac2:51c0:e6ac:
lo:
- ::
ip_interfaces:
----------
eth0:
- 10.0.0.41
- fe80::5c8:50dd::5eba
- fe80::8dbd:f1ae:d9d3:b966
- fe80:::a475:ac57:9b3e
eth1:
- 172.16.1.41
- fe80::89d4:576f::41b
- fe80::5bae::65d3:7d6b
- fe80::3ac2:51c0:e6ac:
lo:
- 127.0.0.1
- ::
ipv4:
- 10.0.0.41
- 127.0.0.1
- 172.16.1.41
ipv6:
- ::
- fe80::3ac2:51c0:e6ac:
- fe80:::a475:ac57:9b3e
- fe80::5bae::65d3:7d6b
- fe80::5c8:50dd::5eba
- fe80::89d4:576f::41b
- fe80::8dbd:f1ae:d9d3:b966
kernel:
Linux
kernelrelease:
3.10.-.el7.x86_64
locale_info:
----------
defaultencoding:
UTF-
defaultlanguage:
en_US
detectedencoding:
UTF-
localhost:
saltstack-
lsb_distrib_id:
CentOS Linux
machine_id:
12bba36e67de4c8e94aa369560cfdbfd
manufacturer:
VMware, Inc.
master:
172.16.1.41
mdadm:
mem_total: nodename:
saltstack-
num_cpus: num_gpus: os:
CentOS
os_family:
RedHat
osarch:
x86_64
oscodename:
Core
osfinger:
CentOS Linux-
osfullname:
CentOS Linux
osmajorrelease: osrelease:
7.3.
osrelease_info:
-
-
-
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
productname:
VMware Virtual Platform
ps:
ps -efH
pythonexecutable:
/usr/bin/python
pythonpath:
- /usr/bin
- /usr/lib64/python27.zip
- /usr/lib64/python2.
- /usr/lib64/python2./plat-linux2
- /usr/lib64/python2./lib-tk
- /usr/lib64/python2./lib-old
- /usr/lib64/python2./lib-dynload
- /usr/lib64/python2./site-packages
- /usr/lib64/python2./site-packages/gtk-2.0
- /usr/lib/python2./site-packages
pythonversion:
-
-
-
- final
-
saltpath:
/usr/lib/python2./site-packages/salt
saltversion:
2015.5.
saltversioninfo:
-
-
-
-
selinux:
----------
enabled:
False
enforced:
Disabled
serialnumber:
VMware- 4d e3 e6 1a a1 7a-bc 4c d5 ac ab 4d
server_id: shell:
/bin/sh
systemd:
----------
features:
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
version: virtual:
VMware
zmqversion:
3.2.
1.3.1、显示指定grains名称对应的值
# salt 'saltstack-41' grains.item fqdn #查看Grains信息里面的单条
saltstack-:
----------
fqdn:
saltstack-
1.4、通过grains获取eth0网卡的地址
# salt 'salt*' grains.get ip_interfaces:eth0 #显示所有eth0的网卡信息
saltstack-:
- 10.0.0.41 #ipv4的ip地址
- fe80:::a475:ac57:9b3e #ipv6的ip地址
1.5、通过grains获取服务器系统名称
# salt 'saltstack-41' grains.get os
saltstack-:
CentOS
1.6、通过grains匹配系统,在指定类型的系统上执行一条命令
# salt -G os:CentOS cmd.run 'w'
saltstack-:
:: up :, user, load average: 0.05, 0.04, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/ 10.0.0.1 : .00s .90s .33s /usr/bin/python /usr/bin/salt -G os:CentOS cmd.run w
db02-:
:: up :, user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/ 10.0.0.1 : : .28s .28s -bash
注:-G:指使用Grains进行匹配,匹配到centos系统则执行w命令
同理,如果说以后想在有eth0网卡的服务器上批量执行一个命令就可以这样匹配,在vmware虚拟机上批量执行一个语句等等。
1.7、在minion端指定grains名称,然后在master端匹配到指定的grains名称就执行想要执行的指令
、minion端指定grains名称
# vim /etc/salt/minion
# sed -n '88,91p' /etc/salt/minion #去掉注释就好,不要添加其他的空格
grains:
roles:
- webserver
- memcache 、重启minion端
# systemctl restart salt-minion 、master端匹配grains名称,然后执行指定
# salt -G 'roles:memcache' cmd.run 'echo "hehe"'
saltstack-:
hehe
注:在所有minion里面配置了roles:memcache的机器上执行echo "hehe"
1.7.1、如果觉得在minion里配置不方便,可以在minion端单独写一个文件指定grains名称
、如果写/etc/salt/grains文件为定义grains名称文件,内容为:
# cat /etc/salt/grains
web: nginx #注意冒号后面有一个空格 、重启minion
# systemctl restart salt-minion 、在master端匹配minion端的grains名称并执行指定命令:
# salt -G 'web:nginx' cmd.run "echo 1111"
saltstack-: 注:即在/etc/salt/grains文件里匹配到web:nginx配置的服务器则批量执行echo
1.8、在top.sls(高级状态)里匹配grains并执行指定的指令
# cat /srv/salt/top.sls
base: #定义base
'web:nginx': #minion端匹配了web:nginx
- match: grain #匹配方式:Grains
- apache #以上两条都匹配了则执行apache的状态
1.8.1、配置后执行高级状态
# salt '*' state.highstate
2、SaltStack数据系统之pillar
Pillar给minion(客户端)指定它想要的数据,(给那个minion指定那个minion才能看得到数据。)Pillar存放在master端,
2.1、查看pillar
# salt '*' pillar.items
db02-:
---------- #没有
saltstack-:
----------
2.2、设置pillar
、master端配置pillar
# sed -n '529,531p' /etc/salt/master #取消一下行的注释
pillar_roots:
base:
- /srv/pillar 、创建Pillar目录
# mkdir /srv/pillar 、设置后重启master
# systemctl restart salt-master 、写pillar状态
# cat /srv/pillar/apache.sls
{% if grains['os'] == 'CentOS' %} #如果是CentOS系统
apache: httpd #就显示httpd
{% elif grains['os'] == 'Debian' %} #如果是Debian系统
apache: apche2 #就显示apche2
{% endif %}
2.3、在top.sls(高级状态)里设置那台minion端可以查看pillar状态
# cat /srv/pillar/top.sls #在top.sls文件里指定那个客户端可以查看pillar状态
base:
'*': #设置所有主机(*表示所有)可以查看apache的状态,也可以指定单个主机
- apache #可以查看的状态
2.3.1、设置后在minion端查看pillar
# salt '*' pillar.items #查看pillar的命令
db02-: #由于是centos系统
----------
apache: #所以查看到的状态是apache
httpd
saltstack-:
----------
apache:
httpd
2.4、用pillar定位主机
-I:匹配pillar
# salt '*' saltutil.refresh_pillar #定位主机前需要先刷新一下,通知设置pillar客户端
db02-:
True
saltstack-:
True
# salt -I 'apache:httpd' test.ping #然后定位存在apache:httpd的主机
db02-:
True #表示成功
saltstack-:
True