您好,登錄后才能下訂單哦!
參考:劉天斯《Python自動化運維技術與最佳實踐》
grains是Saltstack最重要的組件之一,grains的作用是收集被控主機的基本信息,這些信息通常都是一些靜態類的數據,包括CPU、內核、操作系統、虛擬化等,在服務器端可以根據這些信息進行靈活定制,管理員可以利用這些信息對不同業務進行個性化定制。
獲取所有主機的grains項信息,發現沒有max_openfile,等會我們手動編寫一個。
[root@salt-master base]# salt '*' grains.ls salt-master: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - disks - dns - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gid - gpus - groupname - host - hwaddr_interfaces - id - init - ip4_interfaces - ip6_interfaces - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - locale_info - localhost - lsb_distrib_codename - lsb_distrib_id - machine_id - manufacturer - master - mdadm - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - osrelease_info - path - pid - productname - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - serialnumber - server_id - shell - systemd - uid - username - uuid - virtual - zmqversion salt-minion01: - SSDs - cpu_flags - cpu_model - cpuarch - disks - dns - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gid - gpus - groupname - host - hwaddr_interfaces - id - init - ip4_interfaces - ip6_interfaces - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - locale_info - localhost - lsb_distrib_codename - lsb_distrib_id - lsb_distrib_release - master - max_open_file - mdadm - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - osrelease_info - path - pid - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - server_id - shell - uid - username - virtual - zmqversion hddcluster2: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - disks - dns - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gid - gpus - groupname - host - hwaddr_interfaces - id - init - ip4_interfaces - ip6_interfaces - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - locale_info - localhost - lsb_distrib_codename - lsb_distrib_id - lsb_distrib_release - machine_id - manufacturer - master - mdadm - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - osrelease_info - path - pid - productname - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - serialnumber - server_id - shell - systemd - uid - username - uuid - virtual - zmqversion hddcluster4: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - disks - dns - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gid - gpus - groupname - host - hwaddr_interfaces - id - init - ip4_interfaces - ip6_interfaces - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - locale_info - localhost - lsb_distrib_codename - lsb_distrib_id - lsb_distrib_release - machine_id - manufacturer - master - mdadm - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - osrelease_info - path - pid - productname - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - serialnumber - server_id - shell - systemd - uid - username - uuid - virtual - zmqversion hddcluster3: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - disks - dns - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gid - gpus - groupname - host - hwaddr_interfaces - id - init - ip4_interfaces - ip6_interfaces - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - locale_info - localhost - lsb_distrib_codename - lsb_distrib_id - lsb_distrib_release - machine_id - manufacturer - master - mdadm - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - osrelease_info - path - pid - productname - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - serialnumber - server_id - shell - systemd - uid - username - uuid - virtual - zmqversion hddcluster1: - SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - disks - dns - domain - fqdn - fqdn_ip4 - fqdn_ip6 - gid - gpus - groupname - host - hwaddr_interfaces - id - init - ip4_interfaces - ip6_interfaces - ip_interfaces - ipv4 - ipv6 - kernel - kernelrelease - locale_info - localhost - lsb_distrib_codename - lsb_distrib_id - lsb_distrib_release - machine_id - manufacturer - master - mdadm - mem_total - nodename - num_cpus - num_gpus - os - os_family - osarch - oscodename - osfinger - osfullname - osmajorrelease - osrelease - osrelease_info - path - pid - productname - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - serialnumber - server_id - shell - systemd - uid - username - uuid - virtual - zmqversio
定義grains數據的方法有兩種,一種為在被控主機定制配置文件:另一種是通過主控端擴展模塊API實現。
我這這里直說主控端,畢竟我們要搞自動化運維,需要批量操作。
主控端擴展模塊定制grains數據 vim /etc/salt/master file_roots: base: //基礎平臺 - /srv/salt/base prod: //生產平臺 - /srv/salt/prod install -d /srv/salt/base/_grains
vim /srv/salt/base/_grains/grains_openfile.py //編寫一個python腳本獲取max_openfile的值 #!/usr/bin/env python # -*- coding:utf-8 -*- import os,sys,commands #定義一個獲取最大打開文件數的函數,函數名稱沒有要求,符合python函數命名規則即可 def Grains_openfile(): ''' return os max open file of grains value ''' grains = {} #初始化一個字典,變量名一定要用grains,以便Saltstack識別 _open_file=65535 #初始化一個默認值 try: getulimit = commands.getstatusoutput('source /etc/profile;ulimit -n') except Exception,e: pass if getulimit[0]==0: _open_file=int(getulimit[1]) grains['max_open_file'] = _open_file #將獲取的ulimit -n的結果進行賦值,其中'max_open_file'就是grains項,——open_file就是grains的值 return grains
最后同步模塊到指定被控主機并刷新生效,因為grains比較適合采集靜態類的數據,比如硬件、內核信息等,當有動態類的功能需求時,需要進行刷新。
同步操作:
服務端master [root@salt-master base]# salt 'salt-minion01' saltutil.sync_all salt-minion01: ---------- beacons: engines: grains: - grains.grains_openfile log_handlers: modules: output: proxymodules: renderers: returners: sdb: states: utils:
客戶端minion 結果:文件同步到被控端的cache目錄中 [root@salt-minion01 ~]# find / -type f -name grains_openfile.py /var/cache/salt/minion/files/base/_grains/grains_openfile.py /var/cache/salt/minion/extmods/grains/grains_openfile.py #注:/var/cache/salt/minion/extmods/grains/為擴展模塊文件最終存放位置,刷新模塊后將在同路徑下生產字節碼pyc;/var/cache/salt/minion/files/base/_grains/為臨時存放位置。
刷新操作: [root@salt-master base]# salt 'salt-minion01' sys.reload_modules salt-minion01: True [root@salt-minion01 ~]# find / -type f -name grains_openfile.py* /var/cache/salt/minion/files/base/_grains/grains_openfile.py /var/cache/salt/minion/extmods/grains/grains_openfile.pyc //多了一個pyc /var/cache/salt/minion/extmods/grains/grains_openfile.py
查看同步后是否能正常獲取數據 [root@salt-master base]# salt 'salt-minion01' grains.item max_open_file salt-minion01: ---------- max_open_file: 1024 其他未同步的,并不會有max_open_file數據 [root@salt-master base]# salt '*' grains.item max_open_file salt-master: ---------- max_open_file: salt-minion01: ---------- max_open_file: 1024 hddcluster2: ---------- max_open_file: hddcluster4: ---------- max_open_file: hddcluster3: ---------- max_open_file: hddcluster1: ---------- max_open_file: [root@salt-master base]# 其他未同步的,并不會有max數據,也沒有這個模塊 [root@salt-master base]# salt '*' grains.item max salt-master: ---------- max: hddcluster2: ---------- max: salt-minion01: ---------- max: hddcluster3: ---------- max: hddcluster4: ---------- max: hddcluster1: ---------- max:
最后再試一下吧,同步所有機器,再刷新所有機器
[root@salt-master base]# salt '*' saltutil.sync_all salt-master: ---------- beacons: engines: grains: - grains.grains_openfile log_handlers: modules: output: proxymodules: renderers: returners: sdb: states: utils: salt-minion01: //這個同步過了,沒有更新 ---------- beacons: engines: grains: log_handlers: modules: output: proxymodules: renderers: returners: sdb: states: utils: hddcluster2: ---------- beacons: engines: grains: - grains.grains_openfile log_handlers: modules: output: proxymodules: renderers: returners: sdb: states: utils: hddcluster3: ---------- beacons: engines: grains: - grains.grains_openfile log_handlers: modules: output: proxymodules: renderers: returners: sdb: states: utils: hddcluster4: ---------- beacons: engines: grains: - grains.grains_openfile log_handlers: modules: output: proxymodules: renderers: returners: sdb: states: utils: hddcluster1: ---------- beacons: engines: grains: - grains.grains_openfile log_handlers: modules: output: proxymodules: renderers: returners: sdb: states: utils:
[root@salt-master base]# salt '*' sys.reload_modules salt-master: True salt-minion01: True hddcluster2: True hddcluster4: True hddcluster3: True hddcluster1: True
[root@salt-master base]# salt '*' grains.item max_open_file salt-master: ---------- max_open_file: 8192 hddcluster2: ---------- max_open_file: salt-minion01: ---------- max_open_file: 1024 hddcluster3: ---------- max_open_file: 8192 hddcluster4: ---------- max_open_file: 8192 hddcluster1: ---------- max_open_file: 8192
至此,已經測試完畢,需要寫其他模塊,可以在_grains目錄下繼續添加。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。