saltstack组件
1. grains
2. pillar
3. state
一、grains组件
Grains是saltstack组件中非常重要的组件之一,它用来记录minion的系统信息(操作系统、域名、IP地址、内核信息、操作系统类型、内存信息以及其他更多的系统信息)。
Grain收集的信息是静态的,在minion第一次启动时采集数据(除非下次重起,否则数据不会变化)。
1.首先我们需要查询grains相关命令:
[root@salt-master ~]# salt 'izwz9f8xrvty50quc2gq50z' sys.list_functions grains
izwz9f8xrvty50quc2gq50z:
- 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
2.关于这些命令的用法我们可以查看帮助:
[root@salt-master ~]# salt 'izwz9f8xrvty50quc2gq50z' sys.doc grains.items
'grains.items:'
Return all of the minion's grains
CLI Example:
salt '*' grains.items
Sanitized CLI Example:
salt '*' grains.items sanitize=True
3.查看所有minion上的grains项:
[root@salt-master python]# salt '*' grains.ls
izwz9f8xrvty50quc2gq50z:
- 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
4.查看minion上某一具体的grains值:
[root@salt-master python]# salt '*' grains.item shell
izwz9f8xrvty50quc2gq50z:
----------
shell:
/bin/sh
iZbp150ikdomqe3b32qaubZ:
----------
shell:
/bin/sh
[root@salt-master python]# salt '*' grains.item ipv4
izwz9f8xrvty50quc2gq50z:
----------
ipv4:
- 127.0.0.1
- 172.18.205.187
iZbp150ikdomqe3b32qaubZ:
----------
ipv4:
- 127.0.0.1
- 172.16.102.26
5.设置grains数据
[root@salt-master python]# salt '*' sys.doc grains.setvals
'grains.setvals:'
Set new grains values in the grains config file
:param Destructive: If an operation results in a key being removed, delete the key, too. Defaults to False.
CLI Example:
salt '*' grains.setvals "{'key1': 'val1', 'key2': 'val2'}"
[root@salt-master python]# salt '*' sys.doc grains.setval
'grains.setval:'
Set a grains value in the grains config file
:param Destructive: If an operation results in a key being removed, delete the key, too. Defaults to False.
CLI Example:
salt '*' grains.setval key val
salt '*' grains.setval key "{'sub-key': 'val', 'sub-key2': 'val2'}"
a、命令行方式设置grains数据:
单个值设置:
[root@salt-master python]# salt '*' grains.setval name zhangjunchao
izwz9f8xrvty50quc2gq50z:
----------
name:
zhangjunchao
iZbp150ikdomqe3b32qaubZ:
----------
name:
zhangjunchao
多个值设置:
[root@salt-master python]# salt '*' grains.setvals "{'name1': 'zhangjunchao', 'name2': 'zhangyike'}"
izwz9f8xrvty50quc2gq50z:
----------
name1:
zhangjunchao
name2:
zhangyike
iZbp150ikdomqe3b32qaubZ:
----------
name1:
zhangjunchao
name2:
zhangyike
列表结构设置:
[root@salt-master python]# salt '*' grains.setval name "{'name1': 'zhangjunchao', 'name2': 'zhangyike'}"
izwz9f8xrvty50quc2gq50z:
----------
name:
----------
name1:
zhangjunchao
name2:
zhangyike
iZbp150ikdomqe3b32qaubZ:
----------
name:
----------
name1:
zhangjunchao
name2:
zhangyike
查看对应minion的/etc/salt/grains可以发现已经写入的grains数据:
[root@salt-master python]# salt '*' cmd.run 'cat /etc/salt/grains'
izwz9f8xrvty50quc2gq50z:
name: zhangjunchao
name1: zhangjunchao
name2: zhangyike
iZbp150ikdomqe3b32qaubZ:
name: zhangjunchao
name1: zhangjunchao
name2: zhangyike
b、grains_modules方式设置
写入一个简单的模块
[root@salt-master _grains]# vi mod.py
import time
def now():
grains={}
grains['now']=time.time()
return grains
同步模块到所有的minion
[root@salt-master _grains]# salt '*' saltutil.sync_all
izwz9f8xrvty50quc2gq50z:
----------
beacons:
grains:
- grains.mod
modules:
output:
renderers:
returners:
sdb:
states:
utils:
iZbp150ikdomqe3b32qaubZ:
----------
beacons:
grains:
- grains.mod
modules:
output:
renderers:
returners:
sdb:
states:
utils:
重载一次模块
[root@salt-master _grains]# salt '*' sys.reload_modules
izwz9f8xrvty50quc2gq50z:
True
iZbp150ikdomqe3b32qaubZ:
True
查看新设置的grains
[root@salt-master _grains]# salt '*' grains.item now
izwz9f8xrvty50quc2gq50z:
----------
now:
1526877401.09
iZbp150ikdomqe3b32qaubZ:
----------
now:
1526877401.11
c、minion端设置grains数据
通过修改配置文件,同样可以设置grains的数据,分别在所有的minion端创建grain.conf文件
[root@salt-slave-node1 minion.d]# cat /etc/salt/minion.d/grain.conf
grains:
name: zhangjunchao
age:
- 11
- 12
- 13
重启salt-minion加载配置文件,查看grains:
[root@salt-slave-node1 minion.d]# systemctl restart salt-minion.service
[root@salt-master _grains]# salt '*' grains.item name
izwz9f8xrvty50quc2gq50z:
----------
name:
zhangjunchao
iZbp150ikdomqe3b32qaubZ:
----------
name:
zhangjunchao
[root@salt-master _grains]# salt '*' grains.item age
izwz9f8xrvty50quc2gq50z:
----------
age:
- 11
- 12
- 13
iZbp150ikdomqe3b32qaubZ:
----------
age:
- 11
- 12
- 13
6、删除自定义的grains数据
通过grains.setval命令行设置的grains数据,可以通过grains.delval来删除
通过grains.setval命令行设置grains数据
[root@salt-master _grains]# salt '*' grains.setval myname zhangqianqun
izwz9f8xrvty50quc2gq50z:
----------
myname:
zhangqianqun
iZbp150ikdomqe3b32qaubZ:
----------
myname:
zhangqianqun
查询grains数据
[root@salt-master _grains]# salt '*' grains.item myname
izwz9f8xrvty50quc2gq50z:
----------
myname:
zhangqianqun
iZbp150ikdomqe3b32qaubZ:
----------
myname:
zhangqianqun
删除grains数据
[root@salt-master _grains]# salt '*' grains.delval myname
izwz9f8xrvty50quc2gq50z:
None
iZbp150ikdomqe3b32qaubZ:
None
查询是否删除
[root@salt-master _grains]# salt '*' grains.item myname
izwz9f8xrvty50quc2gq50z:
----------
myname:
None
iZbp150ikdomqe3b32qaubZ:
----------
myname:
None
注:通过一系列的操作演示,我们可以不难发现grains.delval的用法。其他方式删除grains数据的方式是与添加过程相反的操作。