一、grains
grains 是在 minion(客戶端)啓動時收集到的一些信息,好比操做系統類型、網卡ip等。強調是minion啓動時收集到的數據,因此若是改了什麼硬件啥的,要重啓minion才能收集,要否則仍是老數據。node
1.1)列出grains的keypython
[root@master salt]# salt '192.168.222.145' grains.ls
1.2)顯示全部的key及對應key的值nginx
[root@master salt]# salt '192.168.222.145' grains.items
1.3)顯示單個key對應的值web
[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的機器並執行wapache
[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的配置文件中修改vim
[root@master ~]# vim /etc/salt/minion grains: roles: - webserver - memcache
匹配角色是memcache的機器並輸出hehecentos
[root@master ~]# salt -G roles:memcache cmd.run 'echo hehe' 192.168.222.145: hehe
1.6)也能夠本身建立一個文件來匹配grainsbash
[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匹配spa
[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
二、Pillar,給minion指定的數據,給誰指定誰就能看到。以下開啓pillar,修改配置以後重啓服務,可能或會纔會有響應。 操作系統
[root@master ~]# vim /etc/salt/master pillar_opts: True
[root@master salt]# salt '*' pillar.items
2.1)定義pillar數據,這個入口文件的要求是跟grains同樣的
[root@master ~]# vim /etc/salt/master pillar_roots: base: - /srv/pillar
pillar_opts: False
改成false
[root@master ~]# mkdir /srv/pillar
[root@master pillar]# pwd /srv/pillar [root@master pillar]# cat apache.sls {% if grains['os'] == 'CentOS' %} apache: httpd {% elif grains['os'] == 'Debian' %} apache: apache2 {% endif %} [root@master pillar]# cat top.sls base: '*': - apache
刷新一下 [root@master pillar]# salt '*' saltutil.refresh_pillar 192.168.222.145: True 192.168.222.146: True [root@master pillar]# salt -I 'apache:httpd' test.ping 192.168.222.145: True 192.168.222.146: True
名稱 | 存儲位置 | 數據類型 | 數據採集更新方式 | 應用 |
Grains | minion | 靜態數據 | minion啓動時收集,也可使用 saltutil.sync_grains進行刷新。 |
存儲minion基本數據。好比用於匹配minion, 自身數據能夠用來作資產管理等。 |
Pillar | master | 動態數據 | 在master端定義,指定給對應 的minion。可使用saltutil.refresh_pillar刷新。 |
存儲master指定的數據,只有指定的minion能夠看到,用於敏感數據保存。 |