saltstack之grains

3年前 (2017-05-24) gtj saltstack, 自动化 0评论 已收录 784℃

saltstack数据系统:
grains ##存储在minion端,,只有在minion启动时才会收集,,之后重启minion后才会重新收集
pillar

grains的应用场景:
1、信息查询
2、在远程执行里面匹配minion
比如我想要在所有centos的机器上执行一个命令
3、在top.sls里面匹配minion
4、在配置文件的模板里使用

1、信息查询
[root@linux-node1 ~]# salt 'linux-node1' grains.ls ##把所有grains的key列出来,#grains支持的所有模块
linux-node1:
- SSDs
- biosreleasedate
- biosversion
- cpu_flags
- cpu_model
- cpuarch
- domain
- fqdn
........skip........

[root@linux-node1 ~]# salt 'linux-node1' grains.items ##把所有grains的内容显示出来
linux-node1:
----------
SSDs:
biosreleasedate:
07/02/2015
biosversion:
6.00
cpu_flags:
- fpu
- vme
- de
........skip...................
serialnumber:
VMware-56 4d c0 95 3b 85 25 92-87 73 7e f3 2f 90 1b 3c
server_id:
1879729795
shell:
/bin/bash
virtual:
VMware
zmqversion:
3.2.5

####想查看某一项的,把items改为单数item,并在后面加上某一项的名称:
[root@linux-node1 ~]# salt 'linux-node1' grains.item fqdn ##可以显示某一项的内容
linux-node1:
----------
fqdn:
linux-node1
####get参数:相对于items,get只显示值的内容
[root@linux-node1 ~]# salt 'linux-node1' grains.get fqdn ##直接显示某一项的内容
linux-node1:
linux-node1

[root@linux-node1 ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:1B:3C
inet addr:10.20.23.144 Bcast:10.20.23.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe90:1b3c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:21592 errors:0 dropped:0 overruns:0 frame:0
TX packets:30570 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12803362 (12.2 MiB) TX bytes:2008047 (1.9 MiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:14374 errors:0 dropped:0 overruns:0 frame:0
TX packets:14374 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1343924 (1.2 MiB) TX bytes:1343924 (1.2 MiB)

[root@linux-node1 ~]# salt 'linux-node1' grains.get ip_interfaces:eth0
linux-node1:
- 10.20.23.144
- fe80::20c:29ff:fe90:1b3c

2、在远程执行里面匹配minion
比如我想要在所有centos的机器上执行一个命令
[root@linux-node1 ~]# salt 'linux-node1' grains.get os
linux-node1:
CentOS
[root@linux-node1 ~]# salt -G os:CentOS cmd.run 'w' ##-G表示使用grains进行匹配,,
linux-node2:
23:43:41 up 4:44, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - 18:59 1:10m 0.00s 0.00s -bash
root pts/1 10.20.23.1 22:36 50:16 0.02s 0.02s -bash
linux-node1:
23:43:39 up 4:43, 1 user, load average: 1.59, 1.44, 1.43
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/1 10.20.23.1 22:37 1.00s 0.40s 0.33s /usr/bin/python

所以通过grains插件可以实现在什么条件的机器侠执行一条相同的命令,都可以通过grains进行匹配

若是内置的grains满足不了我们的需求,,我们也可以自定义grains,,通过在minion端修改配置文件的方式来自定义grains
vim /etc/salt/minion
88 grains:
89 roles:
90 - webserver
91 - memcache
##保存退出
[root@linux-node1 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [确定]
Starting salt-minion daemon: [确定]
[root@linux-node1 ~]# salt -G roles:memcache cmd.run 'echo hehe'
linux-node1:
hehe

若是感觉grains写在minion配置文件里不方便,,也可以写在/etc/salt/grains文件下
前提是和配置文件里的不能冲突
###区别:加入到配置文件/etc/salt/minion上面必须指明为grains,,第二种方法为在/etc/salt/下创建一个名为grains的文件,不不指明grains,直接写就好###
[root@linux-node1 ~]# cat /etc/salt/grains
web: nginx
[root@linux-node1 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [确定]
Starting salt-minion daemon: [确定]
[root@linux-node1 ~]# salt '*' grains.item web
linux-node2:
----------
web:
linux-node1:
----------
web:
nginx

3、在top.sls里面匹配minion
[root@linux-node1 ~]# vim /srv/salt/top.sls
base:
'web: nginx':
- match: grain
- apache
##使用grains进行匹配,,所有grains web的值为nginx的执行apache这个状态。。
[root@linux-node1 ~]# salt '*' state.highstate
linux-node2:
----------
ID: states
Function: no.None
Result: False
Comment: No Top file or external nodes data matches found.
Started:
Duration:
Changes:

Summary
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
linux-node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: Package httpd is already installed.
Started: 01:48:44.407212
Duration: 14303.355 ms
Changes:
----------
ID: apache-install
Function: pkg.installed
Name: httpd-devel
Result: True
Comment: Package httpd-devel is already installed.
Started: 01:48:58.710699
Duration: 0.392 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is in the desired state
Started: 01:48:58.732498
Duration: 510.107 ms
Changes:

Summary
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
##linux-node2没有web: nginx这个grains,,所以失败

博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!