企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
Grains是存储在minion上的数据,minion启动后就进行Grains计算,Grains是一种静态数据,包括很多诸如操作系统类型、操作系统版本或cpu、内存等数据 一)使用场景 1、信息查询 [admin@master ~]$ sudo salt 'node2*' grains.items(显示所有的grains项) 列出对应minion上某一个具体grains的值 [admin@master ~]$ sudo salt 'node2*' grains.ls查看所有的key [admin@master ~]$ sudo salt '*' grains.item hwaddr_interfaces:eth0 node2.51yuki.cn: ---------- hwaddr_interfaces:eth0: 52:54:00:7d:e2:a7 node3.51yuki.cn: ---------- hwaddr_interfaces:eth0: 52:54:00:51:95:09 [admin@master ~]$ sudo salt '*' grains.item mem_total node3.51yuki.cn: ---------- mem_total: 992 node2.51yuki.cn: ---------- mem_total: 992 [admin@master ~]$ sudo salt '*' grains.item os node2.51yuki.cn: ---------- os: CentOS node3.51yuki.cn: ---------- os: CentOS [admin@master ~]$ sudo salt '*' grains.item num_cpus node3.51yuki.cn: ---------- num_cpus: 1 node2.51yuki.cn: ---------- num_cpus: 1 案例: ~~~ [admin@master ~]$ sudo salt '*' grains.item os huancun03.51yuki.cn: ---------- os: CentOS node7.51yuki.cn: ---------- os: CentOS proxy01.51yuki.cn: ---------- os: CentOS [admin@master ~]$ sudo salt '*' grains.item cpu_model huancun03.51yuki.cn: ---------- cpu_model: Westmere E56xx/L56xx/X56xx (Nehalem-C) proxy01.51yuki.cn: ---------- cpu_model: Westmere E56xx/L56xx/X56xx (Nehalem-C) node7.51yuki.cn: ---------- cpu_model: Westmere E56xx/L56xx/X56xx (Nehalem-C) [admin@master ~]$ sudo salt '*' grains.item fqdn node7.51yuki.cn: ---------- fqdn: node7.51yuki.cn proxy01.51yuki.cn: ---------- fqdn: proxy01.51yuki.cn huancun03.51yuki.cn: ---------- fqdn: huancun03.51yuki.cn [admin@master ~]$ sudo salt '*' grains.item fqdn_ip4 huancun03.51yuki.cn: ---------- fqdn_ip4: - 10.2.13.196 node7.51yuki.cn: ---------- fqdn_ip4: - 192.168.20.132 - 10.2.13.219 proxy01.51yuki.cn: ---------- fqdn_ip4: - 10.2.13.243 - 192.168.20.131 [admin@master ~]$ sudo salt '*' grains.item hwaddr_interfaces:eth0 huancun03.51yuki.cn: ---------- hwaddr_interfaces:eth0: 52:54:00:54:94:6d proxy01.51yuki.cn: ---------- hwaddr_interfaces:eth0: 52:54:00:34:6b:00 node7.51yuki.cn: ---------- hwaddr_interfaces:eth0: 52:54:00:b7:82:10 [admin@master ~]$ sudo salt '*' grains.item hwaddr_interfaces:eth1 proxy01.51yuki.cn: ---------- hwaddr_interfaces:eth1: 52:54:00:d5:1d:3a huancun03.51yuki.cn: ---------- hwaddr_interfaces:eth1: 52:54:00:8d:6d:9e node7.51yuki.cn: ---------- hwaddr_interfaces:eth1: 52:54:00:8b:e5:e4 [admin@master ~]$ sudo salt '*' grains.item mem_total huancun03.51yuki.cn: ---------- mem_total: 488 node7.51yuki.cn: ---------- mem_total: 488 proxy01.51yuki.cn: ---------- mem_total: 488 [admin@master ~]$ sudo salt '*' grains.item num_cpus huancun03.51yuki.cn: ---------- num_cpus: 1 proxy01.51yuki.cn: ---------- num_cpus: 1 node7.51yuki.cn: ---------- num_cpus: 1 [admin@master ~]$ sudo salt '*' grains.item osarch proxy01.51yuki.cn: ---------- osarch: x86_64 huancun03.51yuki.cn: ---------- osarch: x86_64 node7.51yuki.cn: ---------- osarch: x86_64 [admin@master ~]$ sudo salt '*' grains.item osrelease proxy01.51yuki.cn: ---------- osrelease: 7.3.1611 node7.51yuki.cn: ---------- osrelease: 7.3.1611 huancun03.51yuki.cn: ---------- osrelease: 7.3.1611 [admin@master ~]$ sudo salt '*' grains.item selinux node7.51yuki.cn: ---------- selinux: ---------- enabled: True enforced: Permissive huancun03.51yuki.cn: ---------- selinux: ---------- enabled: True enforced: Enforcing proxy01.51yuki.cn: ---------- selinux: ---------- enabled: True enforced: Permissive ~~~ 2)匹配minion 例如在所有centos的机器上执行某个命令 [admin@master ~]$ sudo salt -G 'os:CentOS' cmd.run 'df -h' node3.51yuki.cn: Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 13G 1.5G 11G 12% / devtmpfs 486M 0 486M 0% /dev tmpfs 497M 12K 497M 1% /dev/shm tmpfs 497M 6.6M 490M 2% /run tmpfs 497M 0 497M 0% /sys/fs/cgroup /dev/vda1 1014M 121M 894M 12% /boot tmpfs 100M 0 100M 0% /run/user/1000 tmpfs 100M 0 100M 0% /run/user/0 node2.51yuki.cn: Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 13G 1.6G 11G 13% / devtmpfs 486M 0 486M 0% /dev tmpfs 497M 12K 497M 1% /dev/shm tmpfs 497M 6.6M 490M 2% /run tmpfs 497M 0 497M 0% /sys/fs/cgroup /dev/vda1 1014M 121M 894M 12% /boot tmpfs 100M 0 100M 0% /run/user/0 tmpfs 100M 0 100M 0% /run/user/1000 ~~~ [admin@master ~]$ sudo salt -G 'osrelease:7.3.1611' cmd.run 'df -h' huancun03.51yuki.cn: Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 8.0G 1.5G 6.6G 19% / devtmpfs 234M 0 234M 0% /dev tmpfs 245M 12K 245M 1% /dev/shm tmpfs 245M 4.3M 241M 2% /run tmpfs 245M 0 245M 0% /sys/fs/cgroup /dev/vda1 1014M 139M 876M 14% /boot tmpfs 49M 0 49M 0% /run/user/1000 node7.51yuki.cn: Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 8.0G 6.2G 1.9G 77% / devtmpfs 234M 0 234M 0% /dev tmpfs 245M 12K 245M 1% /dev/shm tmpfs 245M 29M 216M 12% /run tmpfs 245M 0 245M 0% /sys/fs/cgroup /dev/vda1 1014M 139M 876M 14% /boot tmpfs 49M 0 49M 0% /run/user/0 192.168.20.135:/data/application/web 8.0G 5.0G 3.0G 63% /opt/web tmpfs 49M 0 49M 0% /run/user/1000 proxy01.51yuki.cn: Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 8.0G 1.8G 6.2G 23% / devtmpfs 234M 0 234M 0% /dev tmpfs 245M 12K 245M 1% /dev/shm tmpfs 245M 29M 216M 12% /run tmpfs 245M 0 245M 0% /sys/fs/cgroup /dev/vda1 1014M 139M 876M 14% /boot tmpfs 49M 0 49M 0% /run/user/0 tmpfs 49M 0 49M 0% /run/user/1000 ~~~ 3)在top.sls文件中使用 [admin@master ~]$ sudo vim /srv/salt/top.sls base: "mysqlserver:Mysql": - match: grain - web.nginx ~ 4) 与pillar配合,在定义pillar中使用 [admin@master _grains]$ cd /srv/pillar/ [admin@master pillar]$ ll total 8 -rw-r--r--. 1 root root 112 Jan 21 12:13 apache.sls -rw-r--r--. 1 root root 26 Jan 21 12:18 top.sls [admin@master pillar]$ vim apache.sls {% if grains['os'] == 'CentOS' %} apache: httpd {% elif grains['os'] == 'Debian' %} apache: apache2 {% endif %} ~ 二) 自定义grains 第一种方法:通过minion配置文件 而在minion include的目录下/etc/salt/minion.d目录下单独创建grains.conf文件 1)给机器打上标签,例如需要安装webserver的机器上打一个标签 [admin@node2 minion.d]$ ll total 8 -rw-r--r--. 1 root root 30 Jan 21 13:00 grains.conf -rw-r--r--. 1 root root 100 Jan 20 22:52 _schedule.conf [admin@node2 minion.d]$ vim grains.conf grains: webserver: nginx [admin@node2 minion.d]$ sudo systemctl restart salt-minion [admin@master ~]$ sudo salt '*' grains.item webserver node2.51yuki.cn: ---------- webserver: nginx node3.51yuki.cn: ---------- webserver: nginx [admin@master ~]$ sudo salt -G 'mysqlserver:Mysql' service.stop nginx node2.51yuki.cn: True 第二种: 编写/etc/salt/grains配置文件 [admin@node2 minion.d]$ sudo vim /etc/salt/grains cache: redis 在master上执行刷新 [admin@master ~]$ sudo salt '*' saltutil.sync_grains node2.51yuki.cn: node3.51yuki.cn: [admin@master ~]$ sudo salt '*' grains.item cache node3.51yuki.cn: ---------- cache: node2.51yuki.cn: ---------- cache: redis 在redis的缓存机器上安装redis服务 [admin@master ~]$ sudo salt -G 'cache:redis' pkg.install redis node2.51yuki.cn: ---------- jemalloc: ---------- new: 3.6.0-1.el7 old: redis: ---------- new: 3.2.10-2.el7 old: [admin@master ~]$ sudo salt -G 'cache:redis' service.start redis node2.51yuki.cn: True [admin@master ~]$ sudo salt -G 'cache:redis' cmd.run 'ss -tunlp|grep redis' node2.51yuki.cn: tcp LISTEN 0 128 127.0.0.1:6379 *:* users:(("redis-server",pid=4974,fd=4)) 删除: [admin@master ~]$ sudo salt -G 'cache:redis' pkg.remove redis node2.51yuki.cn: ---------- redis: ---------- new: old: 3.2.10-2.el7 注意:一般情况下我们不会把grains写在配置文件中,而是采用第三种办法 3)编写/etc/salt/grains文件 [admin@huancun03 minion.d]$ sudo vim /etc/salt/grains cloud: openstack webserver: nginx ~ 重启 [admin@huancun03 minion.d]$ sudo systemctl restart salt-minion 或者在mastrer执行刷新 [admin@master ~]$ sudo salt 'huancun03*' saltutil.sync_grains huancun03.51yuki.cn: 然后在master上执行 [admin@master ~]$ sudo salt 'huancun03*' grains.item webserver huancun03.51yuki.cn: ---------- webserver: nginx 三)自定义python脚本获取grains信息 案例:获取主机系统时间 默认自定义脚本需要存放在master端的/srv/salt/_grains目录下 [admin@master ~]$ mkdir /srv/salt/_grains [admin@master ~]$ cd /srv/salt/_grains/ [admin@master _grains]$ vim get_time.py #!/usr/bin/env python # -*- coding=utf-8 -*- from datetime import datetime def get_time(): grains = {} grains['server_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') return grains 使用sync_grains命令同步脚本到minion主机上去,并通过grains.item命令获取相关信息即可,如下: [admin@master _grains]$ sudo salt '*' saltutil.sync_grains node2.51yuki.cn: - grains.get_time node3.51yuki.cn: - grains.get_time [admin@master _grains]$ sudo salt '*' grains.item server_time node3.51yuki.cn: ---------- server_time: 2018-01-21 13:25:35 node2.51yuki.cn: ---------- server_time: 2018-01-21 13:25:34 案例2:获取nginx的文件 #!/usr/bin/env python #coding:utf-8 #定义nginx配置文件、日志等信息 import os,commands def nginx(): grains = {} grains['nginx_bin'] = commands.getoutput("ps -ef | grep nginx |grep master| grep -v grep |awk '{print $(NF-2)}'").split('\n') grains['nginx_conf'] = commands.getoutput("ps -ef |grep nginx |grep master | grep -v grep |awk '{print $(NF)}'").split('\n') nginx_dir = [] nginx_log = [] for files in grains['nginx_bin']: nginx_dir.append(os.path.dirname(os.path.dirname(files))) nginx_log.append(os.path.dirname(os.path.dirname(files)) + '/logs/access.log') grains['nginx_dir'] = nginx_dir grains['nginx_log'] = nginx_log return grains if __name__ == '__main__': nginx() 同步并查看定义的grains信息 # salt 192.168.xxx.xxx saltutil.sync_grains 192.168.xxx.xxx: - grains.nginx # salt 192.168.xxx.xxx grains.item nginx_conf nginx_log nginx_bin 192.168.xxx.xxx: ---------- nginx_bin: - /data/PRG/nginx-magicube/sbin/nginx - /data/PRG/tengine-play10/sbin/nginx nginx_conf: - /data/PRG/nginx-magicube/conf/nginx.conf - /data/PRG/tengine-play10/conf/nginx.conf nginx_log: - /data/PRG/nginx-magicube/logs/access.log - /data/PRG/tengine-play10/logs/access.log