重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
###Grains模块:###
grains的应用场景:
1、信息查询
2、在远程执行里面匹配minion
比如我想要在所有centos的机器上执行一个命令
3、在top.sls里面匹配minion
4、在配置文件的模板里使用
1、信息查询
###把所有grains的key列出来,grains支持的所有模块
[root@salt_master ~]# salt 'minion.saltstack.com' grains.ls
minion.saltstack.com:
- SSDs
- 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_codename
- lsb_distrib_id
- lsb_distrib_release
- master
- mdadm
- mem_total
- nodename
- num_cpus
- num_gpus
- os
- os_family
- osarch
- oscodename
- osfinger
- osfullname
- osmajorrelease
- osrelease
- osrelease_info
- path
- ps
- pythonexecutable
- pythonpath
- pythonversion
- saltpath
- saltversion
- saltversioninfo
- selinux
- server_id
- shell
- virtual
- zmqversion
##把所有grains的内容显示出来
[root@salt_master ~]# salt 'minion.saltstack.com' grains.items
minion.saltstack.com:
----------
SSDs:
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
- up
- arch_perfmon
- pebs
- bts
- xtopology
- tsc_reliable
- nonstop_tsc
- aperfmperf
- unfair_spinlock
- 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
- ida
- arat
- epb
- xsaveopt
- pln
- pts
- dts
- fsgsbase
- bmi1
- avx2
- smep
- bmi2
- invpcid
cpu_model:
Intel(R) Core(TM) i5-4460 CPU @ 3.20GHz
cpuarch:
x86_64
domain:
saltstack.com
fqdn:
minion.saltstack.com
fqdn_ip4:
- 10.20.23.124
fqdn_ip6:
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
unknown
host:
minion
hwaddr_interfaces:
----------
eth2:
00:0c:29:6e:29:0d
lo:
00:00:00:00:00:00
id:
minion.saltstack.com
init:
upstart
ip4_interfaces:
----------
eth2:
- 10.20.23.124
lo:
- 127.0.0.1
ip6_interfaces:
----------
eth2:
- fe80::20c:29ff:fe6e:290d
lo:
- ::1
ip_interfaces:
----------
eth2:
- 10.20.23.124
- fe80::20c:29ff:fe6e:290d
lo:
- 127.0.0.1
- ::1
ipv4:
- 10.20.23.124
- 127.0.0.1
ipv6:
- ::1
- fe80::20c:29ff:fe6e:290d
kernel:
Linux
kernelrelease:
2.6.32-431.el6.x86_64
locale_info:
----------
defaultencoding:
UTF8
defaultlanguage:
zh_CN
detectedencoding:
UTF-8
localhost:
salt_minion
lsb_distrib_codename:
Final
lsb_distrib_id:
CentOS
lsb_distrib_release:
6.5
master:
salt_master
mdadm:
mem_total:
474
nodename:
salt_minion
num_cpus:
1
num_gpus:
1
os:
CentOS
os_family:
RedHat
osarch:
x86_64
oscodename:
Final
osfinger:
CentOS-6
osfullname:
CentOS
osmajorrelease:
6
osrelease:
6.5
osrelease_info:
- 6
- 5
path:
/sbin:/usr/sbin:/bin:/usr/bin
ps:
ps -efH
pythonexecutable:
/usr/bin/python2.6
pythonpath:
- /usr/bin
- /usr/lib64/python26.zip
- /usr/lib64/python2.6
- /usr/lib64/python2.6/plat-linux2
- /usr/lib64/python2.6/lib-tk
- /usr/lib64/python2.6/lib-old
- /usr/lib64/python2.6/lib-dynload
- /usr/lib64/python2.6/site-packages
- /usr/lib/python2.6/site-packages
pythonversion:
- 2
- 6
- 6
- final
- 0
saltpath:
/usr/lib/python2.6/site-packages/salt
saltversion:
2015.5.10
saltversioninfo:
- 2015
- 5
- 10
- 0
selinux:
----------
enabled:
True
enforced:
Permissive
server_id:
748666745
shell:
/bin/bash
virtual:
VMware
zmqversion:
3.2.5
####想查看某一项的,把items改为单数item,并在后面加上某一项的名称:
[root@salt_master ~]# salt 'minion.saltstack.com' grains.item os
minion.saltstack.com:
----------
os:
CentOS
####get参数:相对于items,get只显示值的内容
[root@salt_master ~]# salt 'minion.saltstack.com' grains.get os
minion.saltstack.com:
CentOS
2、在远程执行里面匹配minion
比如我想要在所有centos的机器上执行一个命令
[root@salt_master ~]# salt --help |grep grain
-G, --grain Instead of using shell globs to evaluate the target
use a grain value to identify targets, the syntax for
the target is the grain key followed by a
--grain-pcre Instead of using shell globs to evaluate the target
use a grain value to identify targets, the syntax for
the target is the grain key followed by a pcre regular
[root@salt_master ~]# salt -G 'os:CentOS' test.ping ##-G表示使用grains进行匹配,,参数必须紧跟salt
minion.saltstack.com:
True
minion1.saltstack.com:
True
[root@salt_master ~]# salt -G 'fqdn:minion.saltstack.com' test.ping
minion.saltstack.com:
True
[root@salt_master ~]# salt 'minion.saltstack.com' grains.get fqdn ###反查minion.saltstack.com的fqdn,用来定位
minion.saltstack.com:
minion.saltstack.com
所以通过grains插件可以实现在什么条件的机器侠执行一条相同的命令,都可以通过grains进行匹配。
若是内置的grains满足不了我们的需求,,我们也可以自定义grains,,通过在minion端修改配置文件的方式来自定义grains
###minion端###
[root@salt_minion ~]# vim /etc/salt/minion ####切到文件结尾
grains:
roles: nginx
env: prod
[root@salt_minion ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [确定]
Starting salt-minion daemon: [确定]
[root@salt_master ~]# salt -G 'env:prod' test.ping
minion.saltstack.com:
True ###成功匹配
[root@salt_master ~]# salt -G 'roles:nginx' test.ping
minion.saltstack.com:
True
若是感觉grains写在minion配置文件里不方便,,也可以写在/etc/salt/grains文件下
前提是和配置文件里的不能冲突
###区别:加入到配置文件/etc/salt/minion上面必须指明为grains,,第二种方法为在/etc/salt/下创建一个名为grains的文件,不指明grains,直接写就好###
[root@salt_minion ~]# vim /etc/salt/grains
cloud: openstack
[root@salt_minion ~]# /etc/init.d/salt-minion restart #####添加完后必须重启
Stopping salt-minion daemon: [确定]
Starting salt-minion daemon: [确定]
[root@salt_minion ~]# cat /etc/salt/grains
cloud: openstack
[root@salt_minion ~]# tail /etc/salt/minion
#
mysql.host: '10.20.23.209'
mysql.user: 'salt'
mysql.pass: 'salt'
mysql.db: 'salt'
mysql.port: 3306
grains:
role: nginx
env: prod
###匹配一下:
[root@salt_master ~]# salt -G 'cloud:openstack' test.ping
minion.saltstack.com:
True
###使用salt '*' saltutil.sync_grains刷新grains,就不需要minion端重启,测试:####
[root@salt_minion ~]# vim /etc/salt/grains
cloud: openstack
test: salt ###添加了这一行,保存退出,不重启minion端
###切到master端###
[root@salt_master ~]# salt '*' saltutil.sync_grains
minion.saltstack.com:
minion1.saltstack.com:
[root@salt_master ~]# salt -G 'test:salt' test.ping
minion.saltstack.com:
True
3、在top.sls里面匹配minion
[root@salt_master ~]# vim /srv/salt/top.sls ###切到最后,添加如下内容:
'role:nginx':
- match: grain
- init.pkg
[root@salt_master ~]# cat /srv/salt/top.sls
base:
'(minion|minion1).saltstack.com':
- match: pcre
- init.pkg
- init.limit
# 'role:nginx':
# - match: grain
# - init.pkg ###先注释掉,注释用#号,知道写法即可
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。