1、grains
grains 是在 minion(客户端)启动时收集到的一些信息,比如操作系统类型、网卡ip等。强调是minion启动时收集到的数据,所以如果改了什么硬件啥的,要重启minion才能收集,要不然还是老数据。
1.1)列出grains的key
[root@master salt]# salt ‘192.168.222.145‘ grains.ls
1.2)显示所有的key及对应key的值
[root@master salt]# salt ‘192.168.222.145‘ grains.items
1.3)显示单个key对应的值
[root@master salt]# salt ‘192.168.222.145‘ grains.item fqdn 192.168.222.145: ---------- fqdn: master
[root@master salt]# salt ‘192.168.222.145‘ grains.get fqdn 192.168.222.145: master
1.4)根据grains匹配并操作,如下匹配所有操作系统是centos的机器并执行w
[root@master salt]# salt -G os:CentOS cmd.run ‘w‘ 192.168.222.146: 01:42:30 up 5:59, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/1 192.168.222.1 19:43 56:20 0.34s 0.34s -bash 192.168.222.145: 01:42:32 up 6:01, 1 user, load average: 0.10, 0.05, 0.01 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/1 192.168.222.1 19:41 1.00s 1.07s 0.45s /usr/bin/python
1.5)也可以自定义grains,然后来匹配,在minion的配置文件中修改
[root@master ~]# vim /etc/salt/minion grains: roles: - webserver - memcache
匹配角色是memcache的机器并输出hehe
[root@master ~]# salt -G roles:memcache cmd.run ‘echo hehe‘ 192.168.222.145: hehe
1.6)也可以自己创建一个文件来匹配grains
[root@master ~]# vim /etc/salt/grains web: nginx 这个web不要跟minion里面的重复
[root@master ~]# salt -G web:nginx cmd.run ‘uptime‘ 192.168.222.145: 01:52:23 up 6:10, 1 user, load average: 0.08, 0.05, 0.00
1.7)top.sls里面也可以通过grains匹配
[root@master ~]# vim /srv/salt/top.sls base: ‘web:nginx‘: - match: grain - apache [root@master ~]# salt ‘*‘ state.highstate 192.168.222.146: ---------- ID: states Function: no.None Result: False Comment: No Top file or external nodes data matches found. Started: Duration: Changes: Summary ------------ Succeeded: 0 Failed: 1 ------------ Total states run: 1 192.168.222.145: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: Package httpd is already installed. Started: 03:02:16.972245 Duration: 1319.294 ms Changes: ---------- ID: apache-install Function: pkg.installed Name: httpd-devel Result: True Comment: Package httpd-devel is already installed. Started: 03:02:18.291724 Duration: 0.646 ms Changes: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service httpd is already enabled, and is in the desired state Started: 03:02:18.293048 Duration: 50.703 ms Changes: Summary ------------ Succeeded: 3 Failed: 0 ------------ Total states run: 3 ERROR: Minions returned with non-zero exit code