您好,登錄后才能下訂單哦!
###Grains模塊:###
grains的應(yīng)用場(chǎng)景:
1、信息查詢
2、在遠(yuǎn)程執(zhí)行里面匹配minion
比如我想要在所有centos的機(jī)器上執(zhí)行一個(gè)命令
3、在top.sls里面匹配minion
4、在配置文件的模板里使用
1、信息查詢
###把所有g(shù)rains的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
##把所有g(shù)rains的內(nèi)容顯示出來
[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
####想查看某一項(xiàng)的,把items改為單數(shù)item,并在后面加上某一項(xiàng)的名稱:
[root@salt_master ~]# salt 'minion.saltstack.com' grains.item os
minion.saltstack.com:
----------
os:
CentOS
####get參數(shù):相對(duì)于items,get只顯示值的內(nèi)容
[root@salt_master ~]# salt 'minion.saltstack.com' grains.get os
minion.saltstack.com:
CentOS
2、在遠(yuǎn)程執(zhí)行里面匹配minion
比如我想要在所有centos的機(jī)器上執(zhí)行一個(gè)命令
[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進(jìn)行匹配,,參數(shù)必須緊跟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插件可以實(shí)現(xiàn)在什么條件的機(jī)器俠執(zhí)行一條相同的命令,都可以通過grains進(jìn)行匹配。
若是內(nèi)置的grains滿足不了我們的需求,,我們也可以自定義grains,,通過在minion端修改配置文件的方式來自定義grains
###minion端###
[root@salt_minion ~]# vim /etc/salt/minion ####切到文件結(jié)尾
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文件下
前提是和配置文件里的不能沖突
###區(qū)別:加入到配置文件/etc/salt/minion上面必須指明為grains,,第二種方法為在/etc/salt/下創(chuàng)建一個(gè)名為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端重啟,測(cè)試:####
[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 ###切到最后,添加如下內(nèi)容:
'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 ###先注釋掉,注釋用#號(hào),知道寫法即可
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。