-rw-r--r-- 1 root root 451 8月 4 13:26saltstack-node2.example.com 3.1.2不同写法概述 3.1.2.1 与minion> r globbing(通配符)
r regex(正则)
r list (列表) 范例:redis-node1-redis03-idc04-soa.example.com
r redis-node1:运行的服务是redis,这个是第一个节点
r redis03:说明这个redis是redis集群编号03里面的节点
r idc04:这台服务器运行在编号04的IDC机房中
r soa:这台服务器是给soa服务使用的
r example.com是域名 3.1.2.2 与minion> r 子网/IP地址
r grains
r pillar
r compound matchers(复合匹配)
r node groups(节点组)
r batching execution(批处理执行) 3.1.3匹配目标的不同写法 3.1.3.1 通配符
r 任意字符
r ? 单个字符
r ! 取反
salt 'saltstack-node*' test.ping
salt '*.example.com' test.ping
salt 'saltstack-node?.example.com' test.ping
salt 'saltstack-node[1-2].example.com' test.ping
salt 'saltstack-node[!2].example.com' test.ping 3.1.3.2 列表(不推荐)
salt -L 'saltstack-node1.example.com,saltstack-node2.example.com' test.ping3.1.3.3 正则※
salt -E 'saltstack-(node1|node2).example.com'test.ping ~正则表达式在top file里的写法
[root@saltstack-node1 ~]# cat/srv/salt/top.sls
base:
'saltstack-(node1|node2).example.com':
- match:pcre #一定要加上这一行,声明使用正则匹配。
- apache 3.1.3.4 子网/IP地址
salt -S 10.0.0.0/24 test.ping
salt -S 10.0.0.22 test.ping 3.1.3.5 grains
salt -G 'os:CentOS' test.ping
salt -G cloud:openstack cmd.run 'df -h'
- apache 3.1.3.6 pillar
salt -I 'Zabbix_Server:10.0.0.22' test.ping3.1.3.7 compound matchers(复合匹配)
salt -C 'saltstack-node1* orI@Zabbix_Server:10.0.0.22' test.ping
salt -C '* and not I@Zabbix_Server:10.0.0.22'test.ping
3.2 执行模块(Modules)
模块是远程执行中的重要组成部分。
在远程执行中的模块叫做执行模块(executionmodules),配置管理的模块称之为状态模块(executionmodules)。
我们学习执行模块时,必须要学会看官方文档,它的优点是全,缺点也是全,太丰富了。
官网(执行模块): https://docs.saltstack.com/en/latest/
模块的数量,至少有300加,而且数量还在不停的递增中……
接下来,我们一起来简单学习几个常用的模块。 注意:cmd.run这个模块,生产环境中,不建议使用,因为比较危险。 3.2.1network模块
模块由名称和方法组成。
注意,有些地方需要指定参数。 3.2.1.1 active_tcp
~返回minion端所有的TCP链接
salt 'saltstack-node1.example.com'network.active_tcp3.2.1.2 arp
~Return the arp table from theminion
salt 'saltstack-node1.example.com' network.arp3.2.1.3 default_route
~Return default route(s) fromrouting table
salt 'saltstack-node1.example.com'network.default_route3.2.1.4 get_hostname
~Get hostname
salt '*' network.get_hostname3.2.1.5 hw_addr
~Return the hardware address(a.k.a. MAC address) for a given interface
salt '*' network.hw_addr eth03.2.1.6 interface
~Return the inet address for agiven interface
salt '*' network.interface eth03.2.1.7 traceroute
~Performs a traceroute to a 3rdparty host
salt '*' network.traceroute baidu.com3.2.2service模块 3.2.2.1 available
~Returns True if the specifiedservice is available, otherwise returns False.
salt '*' service.available sshd3.2.2.2 get_all
~Return a list of all availableservices
salt 'saltstack-node1*' service.get_all3.2.2.3 start
~Start the specified service
salt 'saltstack-node1*' service.start postfix3.2.2.4 stop
~Stop the specified service
salt 'saltstack-node1*' service.stop postfix3.2.2.5 status
~Return the status for aservice, returns the PID or an empty string if the service is running or not,pass a signature to use to find the service via ps
salt 'saltstack-node1*' service.status postfix3.2.3 state模块
作用:Control the state system on the minion. 3.2.3.1 show_top
~Return the top data that theminion will use for a highstate
salt 'saltstack-node1*' state.show_top3.2.3.2 show_highstate
~Retrieve the highstate datafrom the salt master and display it
salt 'saltstack-node1*' state.show_highstate3.2.3.3 highstate
~Retrieve the state data fromthe salt master for this minion and execute it
salt '*' state.highstate3.2.3.4 sls
~Execute the states in one ormore SLS files
salt '*' state.sls apache env=base3.2.4控制cmd模块的方法
我们可以编辑master的主配置文件:/etc/salt/master
~ACL
client_acl: #访问控制列表
larry: #用户larry
# 查看所有的表 3.3.4.3 授权
grant all on salt.* tosalt@'10.0.0.0/255.255.255.0' identified by 'salt';
exit; 3.3.5验证
我们可以在master端打开两个窗口,一个看shell执行返回结果,一个看mysql中返回的结果。
返回程序把执行结果写入到mysql中,但是,返回程序无论写到哪里,master端都会显示执行结果。 3.3.5.1 shell
我们输入下面两条命令,将远程执行的结果,返回给mysql。
salt '*' test.ping --return mysql
salt '*' cmd.run 'uptime' --return mysql
执行结果(命令行):
3.1.1.1 mysql
我们可以登录数据库,查看远程执行的结果是否已被保存在数据库的salt_returns表里。
mysql
use salt;