Grains是什麼?
Grains是SaltStack收集的有關底層管理系統的靜態信息。 SaltStack收集的Grains信息包括操作系統版本、域名、IP地址、內核、操作系統類型、內存以及許多其他系統屬性。
您可以將自定義的grains添加到Salt Minion的/etc/salt/grains文件中,或放置在Grains部分下的Salt Minion配置文件中。 例如,許多用戶爲每個Salt Minion添加一個稱爲角色的定製grain來描述系統的功能。
Grains獲取信息
查看Grains函數:
root@King: /home# salt 'salt-minion-1' sys.list_functions grains salt-minion-1: - grains.append # 將grains的key和value添加到grains配置文件中以列表形式保存,類型 list - grains.delval # 刪除grains的value - grains.filter_by - grains.get # 獲取setvals插入的標籤指定的值 - grains.get_or_set_hash # 在自定義配置文件中加入哈希加密值 - grains.has_value - grains.item # 獲取一個或者多個信息 - grains.items # 獲取詳細信息 - grains.ls # 獲取主機信息 - grains.remove # 從grains配置文件中的列表中刪除一個值(刪除grains的key列表中的一個value值,如果grains的key和value不是列表不能刪除) - grains.setval # 批量添加grains信息 - grains.setvals # 添加多個grains的key和value到grains配置文件中,類型是key-value
grains.append 是將value添加到key的列表中,如果key不存在將創建
grains.setvals 添加多個grains的key和value到grains配置文件中,類型是key-value
grains.delkey 會把key和value一起刪掉,
grains.delval只是把value刪掉,key還在,key的值變爲null
每個命令的幫助信息又可以通過sys.doc查看,如下:
root@King: /home# salt 'salt-minion-1' sys.doc grains 'grains.append:' New in version 0.17.0 Append a value to a list in the grains config file. If the grain doesn't exist, the grain key is added and the value is appended to the new grain as a list item. key The grain key to be appended to val The value to append to the grain key :param convert: If convert is True, convert non-list contents into a list. If convert is False and the grain contains non-list contents, an error is given. Defaults to False. :param delimiter: The key can be a nested dict key. Use this parameter to specify the delimiter you use. You can now append values to a list in nested dictionnary grains. If the list doesn't exist at this level, it will be created. New in version 2014.7.6 CLI Example: salt '*' grains.append key val .........此處省略
獲取主機item信息
root@King: /home# salt 'salt-minion-1' grains.ls salt-minion-1: - SSDs - a - 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 ......此處省略
查看item詳細信息
root@King: /home# salt 'salt-minion-1' grains.items salt-minion-1: ---------- SSDs: a: - 1 cpu_flags: - fpu - vme - de - pse - tsc - msr ......此處省略
查看一項或者多項item值
root@King: /home# salt 'salt-minion-1' grains.item ipv4 # 查看ipv4的值 salt-minion-1: ---------- ipv4: - 127.0.0.1 - 172.16.61.116
root@King: /home# salt 'salt-minion-1' grains.item localhost ipv4 # 查看多個值 salt-minion-1: ---------- ipv4: - 127.0.0.1 - 172.16.61.116 localhost: King
自定義Grains
命令行操作:
# 定義一個hosttype 值爲redis-slave root@King: /home# salt 'salt-minion-1' grains.append hosttype 'redis-slave' salt-minion-1: ---------- hosttype: - redis-slave # 查看自定義標籤 root@King: /home# salt 'salt-minion-1' grains.item hosttype salt-minion-1: ---------- hosttype: - redis-slave # 自定義配置 root@King: /home# salt 'salt-minion-1' grains.setvals "{'city':'beijing'}" salt-minion-1: ---------- city: beijing # 查看新增的配置 root@King: /home# salt 'salt-minion-1' grains.item city salt-minion-1: ---------- city: beijing
自定義後,會在minion主機端生成配置文件grains:
root@King: ~# cat /etc/salt/grains # 可以看到剛纔自定義的標籤和值 city: beijing hosttype: - redis-slave
配置文件修改(minion端)
修改minion端後,要重啓minion端服務才能生效
修改minion端的配置文件:修改這三個文件中的配置都會生效
修改/etc/salt/grains不重啓服務的方法,刷新命令如下:
salt '*' saltutil.sync_grains
配置文件:
/etc/salt/minion.d/grains.conf /etc/salt/minion /etc/salt/grains
總結:
如果/etc/salt/minion.d/grains.conf中已經存在的自定義items, 再通過執行grains.append或grains.setval去執行時,發現會以 grains.conf中的爲準,雖然在/etc/salt/grains中也有內容生成。
而且執行grains.append操作後,/etc/salt/minion.d/grains.conf 中已存在的值會覆蓋/etc/salt/grains中的重複值。
/etc/salt/minion.d/grains.conf (或/etc/salt/minion) > /etc/salt/grains配置中的優先級。
grains_module的方式設置(master端)
在master上建立模塊對應目錄:
root@King: ~# mkdir -pv /srv/salt/_grains
寫入一個簡單的模塊
#vi /srv/salt/_grains/my_grain_mod.py import time def now(): grains = {} grains['now'] = time.time() return grains
同步模塊到minion
root@King: ~# salt 'salt-minion-1' saltutil.sync_all salt-minion-1: ---------- beacons: grains: - grains.my_grain_mod # 新增的自定義 modules: output: renderers: returners: sdb: states: utils:
重新加載模塊和查看新設置的Grains:
root@King: ~# salt 'salt-minion-1' sys.reload_modules salt-minion-1: True root@King: ~# salt 'salt-minion-1' grains.item now salt-minion-1: ---------- now: 1611036019.0