文章目录
- 1. SaltStack模块介绍
- 2.SaltStack常用模块
-
- 2.1 SaltStack常用模块之network
-
- 2.1.1 network.active_tcp
- 查看端口连接状态
- 2.1.2 network.calc_net
- 2.1.3 network.connect
- 2.1.4 network.default_route
- 2.1.5 network.get_fqdn
- 2.1.6 network.get_hostname
- 2.1.7 network.get_route
- 2.1.8 network.hw_addr
- 2.1.9 network.ifacestartswith
- 2.1.10 network.in_subnet
- 2.1.11 network.interface
- 2.1.12 network.interface_ip
- 2.1.13 network.interfaces
- 2.1.14 network.ip_addrs
- 2.1.15 network.netstat
- 2.1.16 network.ping
- 2.1.17 network.reverse_ip
- 2.2 SaltStack常用模块之service
-
- 2.3 SaltStack常用模块之pkg
- 2.4 SaltStack常用模块之state
- 2.5 SaltStack常用模块之salt-cp
- 2.6 SaltStack常用模块之file
-
- 2.6.1 file.access 检查指定路径是否存在
- 2.6.2 file.append 为存在的文件最近内容
- 2.6.3 file.basename 获取指定路径的基名
- 2.6.4 file.dirname 获取指定路径的目录名
- 2.6.5 file.check_hash
- 2.6.6 file.chattr 修改指定文件的属性
- 2.6.7 file.chown 设置指定文件的属主、属组信息
- 2.6.8 file.copy 在远程主机上复制文件或目录
- 2.6.9 file.ditectory_exists 判断目录是否存在
- 2.6.10 file.diskusage 递归计算磁盘使用情况
- 2.6.11 file.file_exists 判断指定文件是否存在
- 2.6.12 file.find
- 2.6.13 file.get_gid 获取指定文件的gid
- 2.6.14 file.get_group 获取指定文件的组名
- 2.6.15 file.get_hash 获取指定文件的hash值
- 2.6.16 file.get_mode 获取指定文件的权限
- 2.6.17 file.get_selinux_context 获取文件的SELINUX信息
- 2.6.18 file.get_sum 指定算法提取文件特征码
- 2.6.19 file.get_uid与file.get_user
- 2.6.20 file.gid_to_group 将 gid 转换为组名
- 2.6.21 file.group_to_gid 将组名转换为 gid
- 2.6.22 file.grep 检索指定文件中的指定内容
- 2.6.23 file.is_blkdev 判断指定的文件是否是块设备文件
- 2.6.24 file.lsattr 检查指定文件的属性信息
- 2.6.25 file.mkdir
- 2.6.26 file.move 移动或重命名
- 2.6.27 file.prepend 把文本插入指定文件的开头
- 2.6.28 file.sed 修改文本文件的内容
- 2.6.29 file.read 读取文件内容
- 2.6.30 file.readdir 列出所有文件,包括隐藏文件
- 2.6.31 file.remove 删除文件或目录
- 2.6.32 file.rename 重命名文件或目录
- 2.6.33 file.set_mode 给指定文件设置权限
- 2.6.34 file.symlink 创建软链接
- 2.6.35 file.touch 创建空文件或更新时间戳
- 2.6.36 file.uid_to_user
- 2.6.37 file.user_to_uid
- 2.6.38 file.write 写入内容至文件,并覆盖原有内容
1. SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
1.1 查看所有module列表
[root@node01-linux ~]# salt 'node01*' sys.list_modules
node01-linux.example.com:
- acl
- aliases
- alternatives
- ansible
- archive
- artifactory
- 省略N行
1.2 查看指定module的所有function
[root@node01-linux ~]# salt 'node01*' sys.list_functions cmd
node01-linux.example.com:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.powershell
- cmd.powershell_all
- cmd.retcode
- cmd.run
- cmd.run_all
- cmd.run_bg
- cmd.run_chroot
- cmd.run_stderr
- cmd.run_stdout
- cmd.script
- cmd.script_retcode
- cmd.shell
- cmd.shell_info
- cmd.shells
- cmd.tty
- cmd.which
- cmd.which_bin
1.3 查看指定module的用法
[root@node01-linux ~]# salt 'node01*' sys.doc cmd
cmd.tty:
Echo a string to a specific tty
CLI Example:
salt '*' cmd.tty tty0 'This is a test'
salt '*' cmd.tty pts3 'This is a test'
cmd.which:
Returns the path of an executable available on the minion, None otherwise
CLI Example:
salt '*' cmd.which cat
cmd.which_bin:
Returns the first command found in a list of commands
CLI Example:
salt '*' cmd.which_bin '[pip2, pip, pip-python]'
1.4 SaltStack执行多个Module
SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号–args-separator=@即可
[root@node01-linux ~]# salt 'node01*' test.echo,cmd.run,service.status hello,hostname,salt-minion
node01-linux.example.com:
----------
cmd.run:
node01-linux.example.com
service.status:
True
test.echo:
hello
2.SaltStack常用模块
以下命令均只查询一台被控机node01-linux,简写为node01*
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
返回所有活动的tcp连接
[root@node01-linux ~]# salt 'node01*' network.active_tcp
node01-linux.example.com:
----------
0:
----------
local_addr:
192.168.80.130
local_port:
4505
remote_addr:
192.168.80.130
remote_port:
51142
1:
----------
local_addr:
192.168.80.130
local_port:
4505
remote_addr:
192.168.80.1
remote_port:
20746
2:
----------
local_addr:
192.168.80.130
local_port:
22
remote_addr:
192.168.80.1
remote_port:
59085
3:
----------
local_addr:
192.168.80.130
local_port:
51142
remote_addr:
192.168.80.130
remote_port:
4505
4:
----------
local_addr:
192.168.80.130
local_port:
4506
remote_addr:
192.168.80.1
remote_port:
20739
查看端口连接状态
用法:lsof -i :[端口号]
[root@node01-linux ~]# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1053 root 3u IPv4 19859 0t0 TCP *:ssh (LISTEN)
sshd 1053 root 4u IPv6 19868 0t0 TCP *:ssh (LISTEN)
sshd 50962 root 3u IPv4 161636 0t0 TCP node01-linux.example.com:ssh->192.168.80.1:25279 (ESTABLISHED)
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@node01-linux ~]# salt 'node01*' network.calc_net 192.168.80.130 255.255.255.0
node01-linux.example.com:
192.168.80.0/24
[root@node01-linux ~]# salt 'node01*' network.calc_net 192.168.72.132 255.255.255.0
node01-linux.example.com:
192.168.72.0/24
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
[root@node01-linux ~]# salt 'node01*' network.connect www.baidu.com 80
node01-linux.example.com:
----------
comment:
Successfully connected to www.baidu.com (182.61.200.6) on tcp port 80
result:
True
2.1.4 network.default_route
查看默认路由
[root@node01-linux ~]# salt 'node01*' network.default_route
node01-linux.example.com:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.80.2
interface:
ens33
netmask:
0.0.0.0
|_
----------
addr_family:
inet6
destination:
::/0
flags:
!n
gateway:
::
interface:
lo
netmask:
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@node01-linux ~]# salt 'node01*' network.get_fqdn
node01-linux.example.com:
node01-linux.example.com
2.1.6 network.get_hostname
获取主机名
[root@node01-linux ~]# salt 'node01*' network.get_hostname
node01-linux.example.com:
node01-linux.example.com
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@node01-linux ~]# salt 'node01*' network.get_route 192.168.80.1
node01-linux.example.com:
----------
destination:
192.168.80.1
gateway:
None
interface:
ens33
source:
192.168.80.130
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@node01-linux ~]# salt 'node01*' network.hw_addr ens33
node01-linux.example.com:
00:0c:29:e8:c1:1f
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@node01-linux ~]# salt 'node01*' network.ifacestartswith 192.168
node01-linux.example.com:
- ens33
[root@node01-linux ~]# salt 'node01*' network.ifacestartswith 127.0.0.1
node01-linux.example.com:
- lo
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@node01-linux ~]# salt 'node01*' network.in_subnet 192.168.79.0/24
node01-linux.example.com:
False
[root@node01-linux ~]# salt 'node01*' network.in_subnet 192.168.80.0/24
node01-linux.example.com:
True
2.1.11 network.interface
返回指定网卡的信息
[root@node01-linux ~]# salt 'node01*' network.interface ens33
node01-linux.example.com:
|_
----------
address:
192.168.80.130
broadcast:
192.168.80.255
label:
ens33
netmask:
255.255.255.0
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@node01-linux ~]# salt 'node01*' network.interface_ip ens33
node01-linux.example.com:
192.168.80.130
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@node01-linux ~]# salt 'node01*' network.interfaces
node01-linux.example.com:
----------
ens33:
----------
hwaddr:
00:0c:29:e8:c1:1f
inet:
|_
----------
address:
192.168.80.130
broadcast:
192.168.80.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:fee8:c11f
prefixlen:
64
scope:
link
up:
True
lo:
----------
hwaddr:
00:00:00:00:00:00
inet:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
----------
address:
::1
prefixlen:
128
scope:
host
up:
True
2.1.14 network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址
[root@node01-linux ~]# salt 'node01*' network.ip_addrs
node01-linux.example.com:
- 192.168.80.130
2.1.15 network.netstat
返回所有打开的端口和状态
[root@node01-linux ~]# salt 'node01*' network.netstat
node01-linux.example.com:
|_
----------
inode:
19859
local-address:
0.0.0.0:22
program:
1053/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
56338
local-address:
0.0.0.0:4505
program:
23663/python
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
- 以下省略N行
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@node01-linux ~]# salt 'node01*' network.ping www.baidu.com
node01-linux.example.com:
PING www.a.shifen.com (182.61.200.6) 56(84) bytes of data.
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=1 ttl=128 time=30.2 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=2 ttl=128 time=29.4 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=3 ttl=128 time=29.6 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=4 ttl=128 time=29.5 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 29.463/29.713/30.213/0.344 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@node01-linux ~]# salt 'node01*' network.reverse_ip 192.168.80.1
node01-linux.example.com:
1.80.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用(判断是否安装该服务,安装即为True)
[root@node01-linux ~]# salt 'node01*' service.available sshd
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' service.available httpd
node01-linux.example.com:
False
2.2.2 service.get_all
获取所有正在运行的服务
[root@node01-linux ~]# salt 'node01*' service.get_all
node01-linux.example.com:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- brandbot
- 省略N行
2.2.3 service.disabled
检查指定服务是否开机不自动启动(不开机自启返回True)
[root@node01-linux ~]# salt 'node01*' service.disabled firewalld
node01-linux.example.com:
True
2.2.4 service.enabled
检查指定服务是否开机自动启动
[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
False
2.2.5 service.disable
设置指定服务开机不自动启动
[root@node01-linux ~]# salt 'node01*' service.disable firewalld
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
False
2.2.6 service.enable
设置指定服务开机自动启动
[root@node01-linux ~]# salt 'node01*' service.enable firewalld
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
True
2.2.7 service.reload
重新加载指定服务(服务必须启动才可执行)
[root@node01-linux ~]# systemctl start httpd
[root@node01-linux ~]# salt 'node01*' service.reload httpd
node01-linux.example.com:
True
2.2.8 service.stop
停止指定服务
[root@node01-linux ~]# salt 'node01*' service.stop httpd
node01-linux.example.com:
True
2.2.9 service.start
启动指定服务
[root@node01-linux ~]# salt 'node01*' service.start httpd
node01-linux.example.com:
True
2.2.10 service.restart
重启指定服务
[root@node01-linux ~]# salt 'node01*' service.restart httpd
node01-linux.example.com:
True
2.2.11 service.status
查看指定服务的状态
[root@node01-linux ~]# salt 'node01*' service.status httpd
node01-linux.example.com:
True
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@node01-linux ~]# salt 'node01*' pkg.download wget
node01-linux.example.com:
----------
wget:
/var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm
2.3.2 pkg.file_list
列出指定包或系统中已安装的所有包的文件
提供参数时,列出已安装的apache软件包提供的所有文件
[root@node01-linux ~]# salt 'node01*' pkg.file_list httpd
node01-linux.example.