Saltstack之Grains组件
参考:刘天斯《Python自动化运维技术与最佳实践》
创新互联建站是一家专业提供阳春企业网站建设,专注与成都网站设计、做网站、H5网站设计、小程序制作等业务。10年已为阳春众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
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目录下继续添加。
标题名称:Saltstack之Grains组件
分享链接:http://scjbc.cn/article/gcghdg.html