1. 主机规划

Grains文档

https://docs.saltstack.com/en/latest/topics/grains/index.html    

注意事项

修改了master或者minion的配置文件,那么必须重启对应的服务。

2. Grains基本信息

Saltstack 数据系统
Grains (谷粒)
Pillar (柱子) Grains:静态数据 当Minion启动的时候收集的Minion本地的相关信息
如:操作系统版本,内核版本,CPU,内存,硬盘,设备型号,序列号
、信息查询 【资产管理】★★★★★
、用于目标选择 salt -G 'os:CentOS' test.ping ★★★★★
、配置管理中使用 刷新grains 【这样修改grains信息不用重启salt-minion 也能刷新】
不然需要重启salt-minion
salt '*' saltutil.sync_grains 开发一个Grains:
Python:写一个Python脚本,返回一个字典就可以了。
可以通过

salt 'salt01' saltutil.sync_grains   【推荐】
或salt 'salt01' saltutil.sync_all 【范围太广,不推荐使用】
或salt 'salt01' state.highstate 【执行高级状态时同步,不建议使用】
或salt 'salt01' saltutil.refresh_modules 【只刷新客户端,服务端向客户端分发的不刷新,比如修改了grains的py脚本时,不会推送到minion端 不建议使用】
或salt 'salt01' saltutil.refresh_grains 【只刷新客户端,服务端向客户端分发的不刷新,比如修改了grains的py脚本时,不会推送到minion端 不建议使用】 将配置同步到minion端
如果做了更改也可以使用
salt 'salt01' sys.reload_modules

3. Grains优先级信息

经验证优先级如下:
、 salt系统自带信息 【优先级最低】
、 自编写Python脚本 备注:在指定目录下存放py脚本
、 /etc/salt/grains 备注:该文件不存在,需要自己创建
、 /etc/salt/minion 【优先级最高】

推荐使用 自编写py脚本定义,这样自定义的grains只需要在master端维护即可,减少后期维护成本。

下文内容就是根据优先级写的。

4. Salt自带Grains 信息

4.1. grains条目项信息

 [root@salt100 salt]# salt 'salt01' grains.ls  # 查看salt01机器有哪些grains条目项信息
………………

4.2. grains全部信息

 [root@salt100 salt]# salt 'salt01' grains.items  # 查看salt01机器有哪些grains信息
salt01:
----------
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
- mmx
- fxsr
- sse
- sse2
- ss
- ht
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- 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
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- mpx
- rdseed
- adx
- smap
- clflushopt
- xsaveopt
- xsavec
- arat
cpu_model:
Intel(R) Core(TM) i5-8250U CPU @ .60GHz
cpuarch:
x86_64
disks:
- sda
- sr0
dns:
----------
domain:
ip4_nameservers:
- 10.0.0.2
- 223.5.5.5
ip6_nameservers:
nameservers:
- 10.0.0.2
- 223.5.5.5
options:
search:
sortlist:
domain:
fqdn:
salt01
fqdn_ip4:
- 172.16.1.11
fqdn_ip6:
- fe80::20c:29ff:fe95:1b84
- fe80::20c:29ff:fe95:1b7a
gid: gpus:
|_
----------
model:
SVGA II Adapter
vendor:
unknown
groupname:
root
host:
salt01
hwaddr_interfaces:
----------
eth0:
:0c:::1b:7a
eth1:
:0c:::1b:
lo:
:::::
id:
salt01
init:
systemd
ip4_gw:
10.0.0.2
ip4_interfaces:
----------
eth0:
- 172.16.1.11
eth1:
- 10.0.0.11
lo:
- 127.0.0.1
ip6_gw:
False
ip6_interfaces:
----------
eth0:
- fe80::20c:29ff:fe95:1b7a
eth1:
- fe80::20c:29ff:fe95:1b84
lo:
- ::
ip_gw:
True
ip_interfaces:
----------
eth0:
- 172.16.1.11
- fe80::20c:29ff:fe95:1b7a
eth1:
- 10.0.0.11
- fe80::20c:29ff:fe95:1b84
lo:
- 127.0.0.1
- ::
ipv4:
- 10.0.0.11
- 127.0.0.1
- 172.16.1.11
ipv6:
- ::
- fe80::20c:29ff:fe95:1b7a
- fe80::20c:29ff:fe95:1b84
kernel:
Linux
kernelrelease:
3.10.-.el7.x86_64
kernelversion:
# SMP Fri Apr :: UTC
locale_info:
----------
defaultencoding:
UTF-
defaultlanguage:
en_US
detectedencoding:
UTF-
localhost:
salt01
lsb_distrib_codename:
CentOS Linux (Core)
lsb_distrib_id:
CentOS Linux
machine_id:
5ae5f27014ee438780bf37ae68889471
manufacturer:
VMware, Inc.
master:
salt100
mdadm:
mem_total: nodename:
salt01
num_cpus: num_gpus: os:
CentOS
os_family:
RedHat
osarch:
x86_64
oscodename:
CentOS Linux (Core)
osfinger:
CentOS Linux-
osfullname:
CentOS Linux
osmajorrelease: osrelease:
7.5.
osrelease_info:
-
-
-
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
pid: productname:
VMware Virtual Platform
ps:
ps -efHww
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:
2018.3.
saltversioninfo:
-
-
-
-
selinux:
----------
enabled:
False
enforced:
Disabled
serialnumber:
VMware- 4d 5c a8 3c- be 1b 7a
server_id: shell:
/bin/sh
swap_total: systemd:
----------
features:
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
version: uid: username:
root
uuid:
86324d56-5c98-3ca8--82be30951b7a
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.1.

4.3. 查询grains指定信息

 [root@salt100 ~]# salt 'salt01' grains.item roles  # 没有该项信息,因此只显示key 不显示value
salt01:
----------
roles:
[root@salt100 ~]# salt 'salt01' grains.item os
salt01:
----------
os:
CentOS

5. 自写grains的py脚本

该脚本在salt master端,然后推送到minion端。

5.1. master端编写py脚本

 [root@salt100 salt]# pwd
/srv/salt
[root@salt100 salt]# mkdir _grains # salt指定的位置
[root@salt100 salt]# cd _grains/
[root@salt100 _grains]# vim my_grains.py
#!/usr/bin/env python
# -*- coding:utf- -*- def grains_define():
# initialize a grains dictionary
grains = {}
# 填充信息
grains['roles'] = ["webserver01","memcache01"],
grains['os'] = 'redhat01' # 返回这个字典
return grains

5.2. grains刷新并查看指定信息

在master端执行,并查看grains信息

 [root@salt100 ~]# salt 'salt01' saltutil.sync_grains  # 会推送py脚本到minion端,并刷新grains信息
salt01:
- grains.my_grains
[root@salt100 ~]# salt 'salt01' grains.item roles
salt01:
----------
roles:
|_
- webserver01
- memcache01
[root@salt100 ~]# salt 'salt01' grains.item os
salt01:
----------
os:
redhat01

5.3. 在minion端查看推送过来的文件位置

 [root@salt01 minion]# pwd
/var/cache/salt/minion
[root@salt01 minion]# tree
.
├── accumulator
├── extmods
│ └── grains # 在这个目录下
│ ├── my_grains.py
│ └── my_grains.pyc
├── files
│ └── base
│ ├── _grains
│ │ └── my_grains.py
│ ├── top.sls
│ └── web
│ └── apache.sls
├── highstate.cache.p
├── module_refresh
├── pkg_refresh
├── proc
└── sls.p directories, files

6. 在/etc/salt/grains 中定义grains

注意:

该文件默认不存在,需要自己创建。

6.1. minion端配置修改

在salt01上操作

 [root@salt01 salt]# cat /etc/salt/grains
roles:
- webserver02
- memcache02
os: redhat02

6.2. grains刷新并查看指定信息

在salt100上操作

 [root@salt100 ~]# salt 'salt01' saltutil.sync_grains  # 刷新grains信息
salt01:
[root@salt100 ~]# salt 'salt01' grains.item roles
salt01:
----------
roles:
- webserver02
- memcache02
[root@salt100 ~]# salt 'salt01' grains.item os
salt01:
----------
os:
redhat02

7. 在/etc/salt/minion 中定义grains

生产中不建议使用,因为写在了默认的配置文件中,不利于后期维护。

7.1. minion端配置修改

minion端salt01机器上配置修改

 [root@salt01 salt]# pwd
/etc/salt
[root@salt01 salt]# vim minion
………………
# Custom static grains for this minion can be specified here and used in SLS
# files just like all other grains. This example sets custom grains, with
# the 'roles' grain having two values that can be matched against.
grains:
roles:
- webserver03
- memcache03
os: redhat03
# deployment: datacenter4
# cabinet:
# cab_u: -

7.2. grains刷新并查看指定信息

在master端刷新grains信息,并查看

 [root@salt100 ~]# salt 'salt01' saltutil.sync_grains  # 刷新指定minion的grains信息
salt01:
[root@salt100 ~]# salt 'salt01' grains.item roles
salt01:
----------
roles:
- webserver03
- memcache03
[root@salt100 ~]# salt 'salt01' grains.item os
salt01:
----------
os:
redhat03

8. Grains使用方式

8.1. 查询grains的指定信息

 [root@salt100 salt]# salt 'salt0*' grains.item os  # 通过通配符匹配salt0*  minion端的操作系统信息
salt01:
----------
os:
CentOS
salt03:
----------
os:
CentOS
salt02:
----------
os:
CentOS
[root@salt100 salt]# salt 'salt0*' grains.item ip4_interfaces:eth0 # 多级查询
salt03:
----------
ip4_interfaces:eth0:
- 172.16.1.13
salt01:
----------
ip4_interfaces:eth0:
- 172.16.1.11
salt02:
----------
ip4_interfaces:eth0:
- 172.16.1.12
[root@salt100 salt]# salt 'salt0*' grains.item ip4_interfaces:eth0: # 取列表中的第一个值★★★★★
salt01:
----------
ip4_interfaces:eth0::
172.16.1.11
salt03:
----------
ip4_interfaces:eth0::
172.16.1.13
salt02:
----------
ip4_interfaces:eth0::
172.16.1.12

8.2. 通过grains查询信息

示例1

 [root@salt100 salt]# salt 'salt01' grains.item os
salt01:
----------
os:
CentOS
[root@salt100 salt]# salt -G 'os:CentOS' cmd.run 'echo "zhangliang $(date +%Y)"' # 指定操作系统为CentOS的
salt03:
zhangliang
salt02:
zhangliang
salt01:
zhangliang
salt100:
zhangliang

示例2

 [root@salt100 salt]# salt 'salt01' grains.item ip_interfaces
salt01:
----------
ip_interfaces:
----------
eth0:
- 172.16.1.11
- fe80::20c:29ff:fe95:1b7a
eth1:
- 10.0.0.11
- fe80::20c:29ff:fe95:1b84
lo:
- 127.0.0.1
- ::
[root@salt100 salt]# salt -G 'ip_interfaces:eth0:172.16.1.11' cmd.run 'whoami'
salt01:
root
[root@salt100 salt]# salt -G 'ip_interfaces:eth1:10.0.0.11' cmd.run 'whoami'
salt01:
root

9. 在top file中使用grains

9.1. top.sls编写

 [root@salt100 salt]# pwd
/srv/salt
[root@salt100 salt]# cat top.sls
base:
# 使用通配符 暂时注释掉
# 'salt0*':
# - web.apache
# - web.nginx # 可以有多个
# 指定具体minion
'salt03':
- web.apache
# 使用grains匹配,添加如下几行
'roles:webserver03':
- match: grain
- web.apache

9.2. state.highstate执行

首先通过grains查看salt01和salt02 的roles信息

 [root@salt100 salt]# salt 'salt01' grains.item roles
salt01:
----------
roles:
- webserver03
- memcache03
[root@salt100 salt]# salt 'salt02' grains.item roles
salt02:
----------
roles:

执行结果

 [root@salt100 salt]# salt 'salt01' state.highstate test=True # 执行正常
………………
[root@salt100 salt]# salt 'salt01' state.highstate # 执行正常
………………
[root@salt100 salt]# salt 'salt02' state.highstate test=True # 执行异常【top.file 中没有该匹配】
salt02:
----------
ID: states
Function: no.None
Result: False
Comment: No Top file or master_tops data matches found.
Changes: Summary for salt02
------------
Succeeded:
Failed:
------------
Total states run:
Total run time: 0.000 ms

最新文章

  1. python3 爬虫
  2. 如何使用JS脚本从HTML中分离图片标签与文本,替换文本中指定的内容并加粗(原创)
  3. 轻松认识JVM运行时数据区域(使用思维导图)
  4. SQL Server存储过程复习(一)
  5. 烂泥:查看服务器的BIOS是否开启CPU虚拟化
  6. python联接主流SQL的类库个人收藏
  7. 30天轻松学习javaweb_Eclipse在修改了web.xml后将自动更新到tomcat服务器中
  8. js Table冻结表头示例代码
  9. Ext.Ajax.request()方法和FormPanel.getForm().submit()方法,都返回success()方法的差异
  10. 被IDEA的打包功能打败了:dubbo服务端打包注意事项
  11. 一些记录查询的SQL语句
  12. JVM虚拟机和垃圾回收算法
  13. SSRF
  14. GridView的stretchMode属性
  15. IP路由配置之---------debugging调试
  16. windows phone开发-windows azure mobile service使用入门
  17. Centos7 64位 -- glibc-2.29 编译升级方法(已成功)
  18. 《Linux内核设计与实现》第4章读书整理
  19. 【BZOJ3240】【UOJ#124】【NOI2013】矩阵游戏
  20. 记开发个人图书收藏清单小程序开发(七)DB设计

热门文章

  1. Spring Boot druid监控页添加登录访问权限(用户名+密码)
  2. 【Spark篇】---Spark中Shuffle机制,SparkShuffle和SortShuffle
  3. Filebeat 模块与配置
  4. Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
  5. node 调试相关
  6. 带着新人学springboot的应用08(springboot+jpa的整合)
  7. EF架构~TransactionScope与SaveChanges的关系
  8. 从0打卡leetcode之day 3 -- 最大子序列和
  9. 使用MQ来保证分布式事务的最终一致性
  10. 看板中的WIP限制