自定义Grains:
定义到配置文件!
[root@minion1 ~]# vim /etc/salt/minion
grains:
roles: nginx
env: prod
[root@minion1 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [FAILED]
Starting salt-minion daemon:
master
[root@master ~]# salt -G 'env:prod' test.ping
minion.23.com:
True
[root@master ~]# salt -G 'roles:nginx' test.ping
minion.23.com:
True
[root@master ~]#
定义到/etc/salt/grains,在/etc/salt/grains键入cloud: saltstack
minion
[root@minion1 ~]# cat /etc/salt/grains
cloud: saltstack
[root@minion1 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [ OK ]
Starting salt-minion daemon: [ OK ]
[root@minion1 ~]#
master端在可以找到有值的服务器执行相关的操作
[root@master ~]# salt -G 'cloud:saltstack' test.ping
minion.23.com:
True
[root@master ~]#
如:远程启动所有安装了nginx的机器,这里的环境只在minion.23.com安装
[root@minion1 ~]# vim /etc/salt/grains
cloud: saltstack
cloud: nginx
[root@minion1 ~]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [ OK ]
Starting salt-minion daemon: [ OK ]
master端
[root@master ~]# salt -G 'roles:nginx' service.start nginx
minion.23.com:
True
[root@master ~]#
minion端:
[root@minion1 ~]# service nginx status
nginx (pid 2459) is running...
[root@minion1 ~]#
同步:saltutil.sync_grains
例如:在minion上添加一条cloud: httpd(事实上这里没有安装)。不重启服务,使用saltutil.sync_grains
刷新即可,如下:
[root@minion1 ~]# vim /etc/salt/grains
cloud: nginx
cloud: httpd
master端第一次执行
[root@master ~]# salt -G 'cloud:httpd' test.ping
No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received
同步后执行正常
[root@master ~]# salt '*' saltutil.sync_grains
master.22.com:
minion.23.com:
minion.21.com:
[root@master ~]# salt -G 'cloud:httpd' test.ping
minion.23.com:
True
[root@master ~]#
-G:目标里面使用grains在top中的使用!在master的top.sls中添加:
'roles:nginx':
- match: grain
- init.pkg
在所有的定义过grains ,并且grains中有nginx的机器上,执行init.pkg的动作!添加如下:
[root@master ~]# vim /etc/salt/status/top.sls
base:
'm*.*.com':
- match: pcre
- init.pkg
'roles:nginx':
- match: grain
- init.pkg