第一个应用场景:信息的搜集
[[email protected] salt]# salt ‘linux-node1*‘ grains.ls 列出所有grains的名称
[[email protected] salt]# salt ‘linux-node1*‘ grains.items 列出所有grains的内容
[[email protected] salt]# salt ‘linux-node1*‘ grains.item fqdn
linux-node1.example.com:
----------
fqdn:
linux-node1.example.com
[[email protected] salt]# salt ‘linux-node1*‘ grains.get fqdn
linux-node1.example.com:
linux-node1.example.com
[[email protected] salt]# salt ‘linux-node1*‘ grains.get ip_interfaces:eth0 查所有eth0的ip地址
linux-node1.example.com:
- 10.0.0.7
- fe80::20c:29ff:fe5c:e013
###############################################################################################
第二个应用场景:匹配minion
[[email protected] salt]# salt ‘linux-node1*‘ grains.get os
linux-node1.example.com:
CentOS
[[email protected] salt]# salt -G os:CentOS cmd.run ‘w‘ -G:指的是使用grains进行匹配
linux-node2.example.com:
22:24:49 up 1:06, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 21:19 22:25 0.02s 0.02s -bash
linux-node1.example.com:
22:24:49 up 1:06, 1 user, load average: 0.00, 0.04, 0.07
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 21:19 1.00s 1.09s 0.86s /usr/bin/python
第一种指定grains的方法,直接在配置文件中加
[[email protected] salt]# vim /etc/salt/minion
82 grains:
83 roles:
84 - webserver
85 - memcache
重新启动minion
[[email protected] salt]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [确定]
Starting salt-minion daemon: [确定]
[[email protected] salt]# salt -G ‘roles:webserver‘ cmd.run ‘df -Th‘
linux-node1.example.com: =====只有linux-node1返回了结果,因为在linux-node1的配置文件中指定了角色
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 ext4 19G 1.8G 16G 11% /
tmpfs tmpfs 937M 16K 937M 1% /dev/shm
/dev/sda1 ext4 190M 36M 145M 20% /boot
另外一种指定grains的方法:
[[email protected] salt]# cat /etc/salt/grains
web: nginx
[[email protected] salt]# /etc/init.d/salt-minion restart
Stopping salt-minion daemon: [确定]
Starting salt-minion daemon: [确定]
[[email protected] salt]# salt -G web:nginx cmd.run ‘w‘
linux-node1.example.com: ===========只在设置了角色的机器上返回了值,匹配是成功的
22:39:32 up 1:21, 1 user, load average: 0.15, 0.09, 0.06
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 21:19 1.00s 1.15s 0.83s /usr/bin/python
###############################################################################################
在top file里面指定grains
[[email protected] salt]# cat /srv/salt/top.sls
base:
‘web:nginx‘:
- match: grain
- apache
[[email protected] salt]# salt ‘*‘ state.highstate 执行高级状态,则显示已匹配到的一台执行了
老男孩网址:http://www.etiantian.org/
qq:406564728
欢迎提出宝贵建议