saltstack資料系統Grains
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 grainssalt-minion-1: - grains.append - grains.delval - grains.filter_by - grains.get - grains.get_or_set_hash - grains.has_value - grains.item - grains.items - grains.ls - grains.remove - grains.setval - grains.setvals
每個命令的幫助資訊又可以通過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 convertis 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