saltstack数据系统Grains

1.什么是Grains系统

Grains是SaltStack收集的有关底层管理系统的静态信息。 SaltStack收集的Grains信息包括操作系统版本、域名、IP地址、内核、操作系统类型、内存以及许多其他系统属性。
您可以将自定义的grains添加到Salt Minion的/etc/salt/grains文件中,或放置在Grains部分下的Salt Minion配置文件中。 例如,许多用户为每个Salt Minion添加一个称为角色的定制grain来描述系统的功能。

2.利用grains获取信息

2.1 查看Grains拥有那些函数

[root@localhost]# salt "agent1" sys.list_functions grains
agent1:
    - grains.append
    - grains.delkey
    - grains.delval
    - grains.equals
    - grains.fetch
    - grains.filter_by
    - grains.get
    - grains.get_or_set_hash
    - grains.has_value
    - grains.item
    - grains.items
    - grains.ls
    - grains.remove
    - grains.set
    - grains.setval
    - grains.setvals
    ...

每个命令的帮助信息又可以通过sys.doc查看,如下:
[root@localhost]# salt 'agent1' sys.doc grains

2.2 获取主机item信息

[root@localhost salt]# salt '*' grains.ls
agent1:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - fqdns
    - gid
    ......

查看item的详细信息

[root@localhost salt]# salt '*' grains.items
agent1:
    ----------
    SSDs:
    biosreleasedate:
        04/13/2018
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme
        - de
        - pse
        - tsc
        - msr
        ......  
        # 此处显示还有很多
    cpu_model:
        Intel(R) Core(TM) i5-8500 CPU @ 3.00GHz
    cpuarch:
        x86_64
    disks:
        - sda
        - sr0
        - dm-0
        - dm-1
    ......

查看某项或多项item值

[root@localhost salt]# salt '*' grains.item ipv4
agent1:
    ----------
    ipv4:
        - 127.0.0.1
        - 192.168.1.4
        
[root@localhost salt]# salt '*' grains.item localhost ipv4
agent1:
    ----------
    ipv4:
        - 127.0.0.1
        - 192.168.1.4
    localhost:
        wang

2.3 通过Grains模块定义Grains

方式一:命令行操作

[root@localhost salt]# salt 'agent1' grains.append hosttype 'redis-slave'
agent1:
    ----------
    hosttype:
        - redis-slave
[root@localhost salt]# salt '*' grains.item hosttype
agent1:
    ----------
    hosttype:
        - redis-slave
[root@localhost salt]# salt 'agent1' grains.setvals "{'city':'beijing'}"
agent1:
    ----------
    city:
        beijing
[root@localhost salt]# salt '*' grains.item city
agent1:
    ----------
    city:
        beijing

这样配置后,会在minion主机端生成配置文件grains ,如下:

[root@wang ~]# cat /etc/salt/grains
city: beijing
hosttype:
- redis-slave

方式二:
1.在配置文件中操作(minion端)

  • /etc/salt/minion.d/grains.conf
  • /etc/salt/minion
  • /etc/salt/grains
在三个文件中配置都会生效,这里在/etc/salt/minion中配置
# Custom static grains for this minion can be specified here and used in SLS
# files just like all other grains. This example sets 4 custom grains, with
# the 'roles' grain having two values that can be matched against.
#grains:
#  roles:
#    - webserver
#    - memcache
#  deployment: datacenter4
#  cabinet: 13
#  cab_u: 14-15
grains:
  roles: apache
  
在master节点查看item
[root@localhost salt]# salt 'agent1' grains.item roles
agent1:
    ----------
    roles:
        apache


注:如果/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配置中的优先级。

配置文件中操作,重启客户端才会生效。修改/etc/salt/grains不重启服务的方法,刷新命令如下

salt '*' saltutil.sync_grains

参考链接:http://www.361way.com/saltstack-grains/5104.html

2.grains_module的方式设置(master端)

首先在master上建立模块对应的目录:
# 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@localhost ~]# salt 'agent1' saltutil.sync_all
agent1:
    ----------
    beacons:
    clouds:
    engines:
    grains:
        - grains.my_grain_mod
    log_handlers:
    matchers:
    modules:
    output:
    proxymodules:
    renderers:
    returners:
    sdb:
    serializers:
    states:
    thorium:
    utils:

重新加载一次模块:

[root@localhost ~]# salt 'agent1' sys.reload_modules
agent1:
    True

查看新设置得Grains:

[root@localhost ~]# salt 'agent1' grains.item now
agent1:
    ----------
    now:
        1568104340.697266

转载于:https://www.cnblogs.com/wangyajian/p/11586990.html