saltstack的grains类似于ansible的setup模块,主要作用为手机客户端的主机基本信息(cpu,内核,os,virtual等),定义在客户端
[root@k8s_master ~]# cat /etc/salt/master |grep -v "^#|^$" cachedir: /var/cache/salt/master auto_accept: True file_recv: True file_roots: base: - /root nodegroups: master1: 'L@k8s_master' agents: 'L@k8s_node1,k8s_node2'
查看主机的详细信息
salt -N 'master1' grains.items
查看所有键(saltstack自带)
[root@k8s_master ~]# salt -N master1 grains.ls k8s_master: - SSDs - biosreleasedate - biosversion - 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 - kernel - kernelrelease - locale_info - localhost - 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 - productname - ps - pythonexecutable - pythonpath - pythonversion - saltpath - saltversion - saltversioninfo - selinux - serialnumber - server_id - shell - systemd - virtual - zmqversion
用法示例: salt <匹配主机> grains.item <grains.ls>获取的键
[root@k8s_master ~]# salt -N master1 grains.item ipv4 k8s_master: ---------- ipv4: - 10.1.14.0 - 127.0.0.1 - 172.17.0.1 - 192.168.132.148
自定义grains键及数据
首先,主控端(master端)编写获取数据的脚本,然后同步到客户端(minion),在主控端的base目录(在/etc/salt/master配置文件中指定的目录,默认为/srv/salt目录)下生成_grains目录,执行install -d /srv/salt/_grains开始编写代码(此例子为获取主机系统允许的最大打开文件数)
[root@k8s_master ~]# cat /root/_grains/test_grains.py #!/usr/bin/env python #-*-coding:utf-8-*- import os,sys,commands def get_custom_grains(): grains = {} _open_file=65535 try: getulimit = commands.getstatusoutput('source /etc/profile;ulimit -n') ##获取客户端文件描述符数量(可通过其他命令获取) except Exception,e: print e #print getulimit,type(getulimit) #此处打印的为一个元组 if getulimit[0] == 0: _open_file=int(getulimit[1]) grains['max_open_files'] = _open_file #为grains的新键(max_open_files)赋值 为_open_file return grains #返回grains返回值
同步并检查本地文件
[root@k8s_master ~]# salt 'k8s_master' saltutil.sync_all [root@k8s_master ~]# ls /var/cache/salt/minion/files/base/_grains/test_grains.py #生成文件 /var/cache/salt/minion/files/base/_grains/test_grains.py [root@k8s_master ~]# ls /var/cache/salt/minion/extmods/grains/test_grains.py /var/cache/salt/minion/extmods/grains/test_grains.py
重新加载模块(刷新模块)
[root@k8s_master ~]# salt 'k8s_master' sys.reload_modules #刷新重载模块 k8s_master: True [root@k8s_master ~]# ls /var/cache/salt/minion/extmods/grains/test_grains.py #增加了pyc文件 test_grains.py test_grains.pyc
主控端查看grains信息
[root@k8s_master ~]# salt 'k8s_master' grains.item max_open_files
k8s_master:
----------
max_open_files:
1024
[root@k8s_master ~]# salt 'k8s_node1' grains.item max_open_files #没有添加的则不能显示
k8s_node1:
----------
max_open_files:
引用:
采用jinja模板
{ % if grains['os'] == 'centos' % } host:{{ grains['host'] }} { % elif grains['os'] == 'RedHat' % } host: {{ grains['fqdn'] }} { % endif % }
注:在模板引擎里使用的时候遵循python列表和字典取值规则,如,获取某个主机某个网卡的ip地址
{{ grains['ip4_interfaces']['ens33'][0] }}