SaltStack常用的模块
- 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.2.1 service.available
- 2.2.2 service.get_all
- 2.2.3 service.disabled
- 2.2.4 service.enabled
- 2.2.5 service.disable
- 2.2.6 service.enable
- 2.2.7 service.reload
- 2.2.8 service.stop
- 2.2.9 service.start
- 2.2.10 service.restart
- 2.2.11 service.status
- 2.3 SaltStack常用模块之pkg
- 2.3.1 pkg.download
- 2.3.2 pkg.file_list
- 2.3.3 pkg.group_info
- 2.3.4 pkg.group_list
- 2.3.5 pkg.install
- 2.3.6 pkg.list_downloaded
- 2.3.7 pkg.list_pkgs
- 2.3.8 pkg.owner
- 2.3.9 pkg.remove
- 2.3.10 pkg.upgrade
- 2.4 SaltStack常用模块之state
- 2.4.2 state.highstate
- 2.4.3 state.show_state_usage
- 2.4.4 state.show_top
- 2.4.5 state.top
- 2.4.6 state.show_sls
- 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.chown
- 2.6.9 file.ditectory_exists
- 2.6.10 file.diskusage
- 2.6.11 file.file_exists
- 2.6.13 file.get_gid
- 2.6.14 file.get_group
- 2.6.15 file.get_hash
- 2.6.16 file.get_mode
- 2.6.17 file.get_selinux_context
- 2.6.18 file.get_sum
- 2.6.19 file.get_uid与file.get_user
- 2.6.20 file.gid_to_group
- 2.6.21 file.group_to_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
1. SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
安装好Master和Minion包后,系统上会安装很多Module,可以通过以下命令查看支持的所有Module列表:
#查看所有module列表
[root@node01-linux ~]# salt 'node02*' sys.list_modules
...
#查看指定module的所有function
[root@node01-linux ~]# salt 'node02*' sys.list_functions smtp
node02-linux.example.com:
- smtp.send_msg
#查看指定module的用法
[root@node01-linux ~]# salt 'node02*' sys.doc smtp
smtp.send_msg:
Send a message to an SMTP recipient. Designed for use in states.
CLI Examples:
salt '*' smtp.send_msg 'admin@example.com' 'This is a salt module test' profile='my-smtp-account'
salt '*' smtp.send_msg 'admin@example.com' 'This is a salt module test' username='myuser' password='verybadpass' sender='admin@example.com' server='smtp.domain.com'
salt '*' smtp.send_msg 'admin@example.com' 'This is a salt module test' username='myuser' password='verybadpass' sender='admin@example.com' server='smtp.domain.com' attachments="['/var/log/messages']"
#SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@node01-linux ~]# salt 'node02*' test.echo,cmd.run,service.status hello,hostname,salt-minion
node02-linux.example.com:
----------
cmd.run:
node02-linux.example.com
service.status:
True
test.echo:
hello
2. SaltStack常用模块
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
#返回所有活动的tcp连接
[root@node01-linux ~]# salt '*' network.active_tcp
node02-linux.example.com:
----------
0:
----------
local_addr:
192.168.25.132
local_port:
58570
remote_addr:
192.168.25.131
remote_port:
4505
1:
----------
local_addr:
192.168.25.132
local_port:
22
remote_addr:
192.168.25.1
remote_port:
59276
2.1.2 network.calc_net
#通过IP和子网掩码计算出网段
[root@node01-linux ~]# salt '*' network.calc_net 192.168.25.132 255.255.255.0
node02-linux.example.com:
192.168.25.0/24
[root@node01-linux ~]# salt '*' network.calc_net 192.168.25.132 255.255.255.240
node02-linux.example.com:
192.168.25.128/28
2.1.3 network.connect
#测试minion至某一台服务器的网络是否连通
[root@node01-linux ~]# salt '*' network.connect baidu.com 80
node02-linux.example.com:
----------
comment:
Successfully connected to baidu.com (220.181.38.148) on tcp port 80
result:
True
2.1.4 network.default_route
#查看默认路由
[root@node01-linux ~]# salt '*' network.default_route
node02-linux.example.com:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.25.2
interface:
eth0
netmask:
0.0.0.0
2.1.5 network.get_fqdn
#查看主机的fqdn(完全限定域名)
[root@node01-linux ~]# salt '*' network.get_fqdn
node02-linux.example.com:
node02-linux.example.com
2.1.6 network.get_hostname
#获取主机名
[root@node01-linux ~]# salt '*' network.get_hostname
node02-linux.example.com:
node02-linux.example.com
2.1.7 network.get_route
#查询到一个目标网络的路由信息
[root@node01-linux ~]# salt '*' network.get_route 192.168.25.131
node02-linux.example.com:
----------
destination:
192.168.25.131
gateway:
None
interface:
eth0
source:
192.168.25.132
2.1.8 network.hw_addr
#返回指定网卡的MAC地址
[root@node01-linux ~]# salt '*' network.hw_addr eth0
node02-linux.example.com:
00:0c:29:4c:93:b6
2.1.9 network.ifacestartswith
#从特定CIDR检索接口名称
[root@node01-linux ~]# salt '*' network.ifacestartswith 192.168
node02-linux.example.com:
- eth0
2.1.10 network.in_subnet
#判断当前主机是否在某一个网段内
[root@node01-linux ~]# salt '*' network.in_subnet 192.168.25.0/24
node02-linux.example.com:
True
2.1.11 network.interface
#返回指定网卡的信息
[root@node01-linux ~]# salt '*' network.interface eth0
node02-linux.example.com:
|_
----------
address:
192.168.25.132
broadcast:
192.168.25.255
label:
eth0
netmask:
255.255.255.0
2.1.12 network.interface_ip
#返回指定网卡的IP地址
[root@node01-linux ~]# salt '*' network.interface_ip eth0
node02-linux.example.com:
192.168.25.132
2.1.13 network.interfaces
#返回当前系统中所有的网卡信息
[root@node01-linux ~]# salt '*' network.interfaces
node02-linux.example.com:
----------
eth0:
----------
hwaddr:
00:0c:29:4c:93:b6
inet:
|_
----------
address:
192.168.25.132
broadcast:
192.168.25.255
label:
eth0
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:fe4c:93b6
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 '*' network.ip_addrs
node02-linux.example.com:
- 192.168.25.132
2.1.15 network.netstat
#返回所有打开的端口和状态
[root@node01-linux ~]# salt '*' network.netstat
node02-linux.example.com:
|_
----------
inode:
19564
local-address:
*:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
*:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
20373
local-address:
127.0.0.1:25
program:
master
proto:
tcp
recv-q:
0
remote-address:
*:*
send-q:
100
state:
LISTEN
user:
0
|_
----------
inode:
21424
local-address:
192.168.25.132:58570
program:
salt-minion
proto:
tcp
recv-q:
0
remote-address:
192.168.25.131:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
0
local-address:
192.168.25.132:50082
program:
proto:
tcp
recv-q:
0
remote-address:
192.168.25.131:4506
send-q:
0
state:
TIME-WAIT
user:
0
|_
----------
inode:
21113
local-address:
192.168.25.132:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.25.1:59276
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
19573
local-address:
:::22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
:::*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
20374
local-address:
::1:25
program:
master
proto:
tcp
recv-q:
0
remote-address:
:::*
send-q:
100
state:
LISTEN
user:
0
2.1.16 network.ping
#使用ping命令测试到某主机的连通性
[root@node01-linux ~]# salt '*' network.ping baidu.com
node02-linux.example.com:
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=27.3 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=26.6 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=27.2 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=27.3 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3013ms
rtt min/avg/max/mdev = 26.676/27.137/27.360/0.338 ms
2.1.17 network.reverse_ip
#返回一个指定的IP地址的反向地址
[root@node01-linux ~]# salt '*' network.reverse_ip 192.168.25.131
node02-linux.example.com:
131.25.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
#判断指定的服务是否可用
[root@node01-linux ~]# salt '*' service.available postfix
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' service.available mysql
node02-linux.example.com:
False
2.2.2 service.get_all
#获取所有正在运行的服务
[root@node01-linux ~]# salt '*' service.get_all
node02-linux.example.com:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- auditd
- autovt@
...
2.2.3 service.disabled
#检查指定服务是否开机不自动启动
[root@node01-linux ~]# salt '*' service.disabled httpd
node02-linux.example.com:
False
2.2.4 service.enabled
#检查指定服务是否开机自动启动
[root@node01-linux ~]# salt '*' service.enabled httpd
node02-linux.example.com:
True
2.2.5 service.disable
#设置指定服务开机不自动启动
[root@node01-linux ~]# salt '*' service.disable httpd
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' service.enabled httpd
node02-linux.example.com:
False
2.2.6 service.enable
#设置指定服务开机自动启动
[root@node01-linux ~]# salt '*' service.enable httpd
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' service.disabled httpd
node02-linux.example.com:
False
2.2.7 service.reload
#重新加载指定服务
[root@node01-linux ~]# salt '*' service.reload httpd
node02-linux.example.com:
True
2.2.8 service.stop
#停止指定服务
[root@node01-linux ~]# salt '*' service.stop httpd
node02-linux.example.com:
True
2.2.9 service.start
#启动指定服务
[root@node01-linux ~]# salt '*' service.start httpd
node02-linux.example.com:
True
2.2.10 service.restart
#重启指定服务
[root@node01-linux ~]# salt '*' service.restart httpd
node02-linux.example.com:
True
2.2.11 service.status
#查看指定服务的状态
[root@node01-linux ~]# salt '*' service.status httpd
node02-linux.example.com:
True
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
#只下载软件包但不安装
#此功能将会下指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@node01-linux ~]# salt '*' pkg.download wget
node02-linux.example.com:
----------
wget:
/var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm //下载的软件包放置的路径
2.3.2 pkg.file_list
#列出指定包或系统中已安装的所有包的文件
[root@node01-linux ~]# salt '*' pkg.file_list httpd
node02-linux.example.com:
----------
errors:
files:
- /etc/httpd
- /etc/httpd/conf
- /etc/httpd/conf.d
- /etc/httpd/conf.d/README
- /etc/httpd/conf.d/autoindex.conf
- /etc/httpd/conf.d/userdir.conf
- /etc/httpd/conf.d/welcome.conf
- /etc/httpd/conf.modules.d
...
#当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@node01-linux ~]# salt '*' pkg.file_list
node02-linux.example.com:
----------
errors:
files:
VALUE_TRIMMED
2.3.3 pkg.group_info
#查看包组的信息
[root@node01-linux ~]# salt '*' pkg.group_info 'Development Tools'
node02-linux.example.com:
----------
conditional:
default:
- byacc
- cscope
- ctags
- diffstat
- doxygen
- elfutils
- gcc-gfortran
- git
- indent
- intltool
- patchutils
- rcs
- subversion
- swig
- systemtap
description:
A basic development environment.
group:
Development Tools
id:
development
mandatory:
- autoconf
- automake
- binutils
- bison
...
2.3.4 pkg.group_list
#列出系统中所有的包组
[root@node01-linux ~]# salt '*' pkg.group_list
node02-linux.example.com:
----------
available:
- Additional Development
- Anaconda Tools
- Backup Client
- Backup Server
- Base
- CentOS Linux Client product core
- CentOS Linux ComputeNode product core
- CentOS Linux Server product core
- CentOS Linux Workstation product core
- Common NetworkManager submodules
- Compatibility Libraries
- Conflicts (Client)
- Conflicts (ComputeNode)
- Conflicts (Server)
- Conflicts (Workstation)
- Console Internet Tools
- Core
2.3.5 pkg.install
#安装软件
[root@node01-linux ~]# salt '*' pkg.install wget
node02-linux.example.com:
----------
wget:
----------
new:
1.14-18.el7_6.1
old:
2.3.6 pkg.list_downloaded
#列出已下载到本地的软件包
[root@node01-linux ~]# salt '*' pkg.list_downloaded
node02-linux.example.com:
----------
wget:
----------
1.14-18.el7_6.1:
----------
creation_date_time:
2020-08-20T10:33:24
creation_date_time_t:
1597890804
path:
/var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm
size:
560272
2.3.7 pkg.list_pkgs
#以字典的方式列出当前已安装的软件包
[root@node01-linux ~]# salt '*' pkg.list_pkgs
node02-linux.example.com:
----------
GeoIP:
1.5.0-11.el7
NetworkManager:
1:1.10.2-13.el7
NetworkManager-libnm:
1:1.10.2-13.el7
NetworkManager-team:
1:1.10.2-13.el7
NetworkManager-tui:
1:1.10.2-13.el7
PyYAML:
3.11-1.el7
acl:
2.2.51-14.el7
...
2.3.8 pkg.owner
#列出指定文件是由哪个包提供的
[root@node01-linux ~]# salt '*' pkg.owner /usr/sbin/apachectl
node02-linux.example.com:
httpd
[root@node01-linux ~]# salt '*' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
node02-linux.example.com:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
2.3.9 pkg.remove
#卸载指定软件
[root@node01-linux ~]# salt '*' cmd.run 'rpm -qa|grep wget'
node02-linux.example.com:
wget-1.14-18.el7_6.1.x86_64
[root@node01-linux ~]# salt '*' pkg.remove wget
node02-linux.example.com:
----------
wget:
----------
new:
old:
1.14-18.el7_6.1
[root@node01-linux ~]# salt '*' cmd.run 'rpm -qa|grep wget'
node02-linux.example.com:
ERROR: Minions returned with non-zero exit code
2.3.10 pkg.upgrade
# 升级系统中所有的软件包或升级指定的软件包
# 若升级系统中所有的软件包则不加name参数
[root@node01-linux ~]# salt '*' pkg.upgrade name=openssl
node02-linux.example.com:
----------
openssl:
----------
new:
1:1.0.2k-19.el7
old:
1:1.0.2k-12.el7
openssl-libs:
----------
new:
1:1.0.2k-19.el7
old:
1:1.0.2k-12.el7
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
#显示当前系统中有哪些高级状态
[root@node01-linux ~]# salt '*' state.show_highstate
node02-linux.example.com:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.install
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate
#执行高级状态
[root@node01-linux ~]# salt '*' state.highstate web.apache.install
node02-linux.example.com:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 10:46:12.586534
Duration: 992.978 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 10:46:13.582244
Duration: 138.897 ms
Changes:
Summary for node02-linux.example.com
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.132 s
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@node01-linux ~]# salt '*' state.show_state_usage
node02-linux.example.com:
Passed invalid arguments to state.show_state_usage: argument of type 'NoneType' is not iterable
Retrieve the highstate data from the salt master to analyse used and unused states
Custom Pillar data can be passed with the ``pillar`` kwarg.
CLI Example:
.. code-block:: bash
salt '*' state.show_state_usage
ERROR: Minions returned with non-zero exit code
2.4.4 state.show_top
#返回minion将用于highstate的顶级数据
[root@node01-linux ~]# salt '*' state.show_top
node02-linux.example.com:
----------
base:
- web.apache.install
2.4.5 state.top
#执行指定的top file,而不是默认的
[root@node01-linux ~]# salt '*' state.top top.sls
node02-linux.example.com:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 10:52:25.992715
Duration: 704.943 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 10:52:26.698753
Duration: 63.509 ms
Changes:
Summary for node02-linux.example.com
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 768.452 ms
2.4.6 state.show_sls
#显示 master 上特定sls或sls文件列表中的状态数据
[root@node01-linux ~]# salt '*' state.show_sls web.apache.install
node02-linux.example.com:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.install
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.5 SaltStack常用模块之salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
#拷贝单个文件到目标主机的/usr/src目录下
[root@node01-linux ~]# salt '*' cmd.run 'ls /usr/src/'
node02-linux.example.com:
debug
kernels
[root@node01-linux ~]# salt-cp '*' /etc/passwd /usr/src/
node02-linux.example.com:
----------
/usr/src/passwd:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls /usr/src'
node02-linux.example.com:
debug
kernels
passwd
#拷贝多个文件到目标主机的/usr/src目录下
[root@node01-linux ~]# salt-cp '*' /etc/shadow /etc/group /usr/src
node02-linux.example.com:
----------
/usr/src/group:
True
/usr/src/shadow:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls /usr/src'
node02-linux.example.com:
debug
group
kernels
passwd
shadow
2.6 SaltStack常用模块之file
2.6.1 file.access
检查指定路径是否存在
[root@node01-linux ~]# salt '*' cmd.run 'ls /usr/src'
node02-linux.example.com:
debug
group
kernels
passwd
shadow
[root@node01-linux ~]# salt '*' file.access /usr/src/passwd f
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' file.access /usr/src/zcq f
node02-linux.example.com:
False
#检查指定文件的权限信息
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /usr/src/'
node02-linux.example.com:
total 12
drwxr-xr-x. 2 root root 6 Apr 11 2018 debug
-rw-r--r--. 1 root root 435 Aug 20 11:00 group
drwxr-xr-x. 2 root root 6 Apr 11 2018 kernels
-rw-r--r--. 1 root root 798 Aug 20 10:59 passwd
-rw-r--r--. 1 root root 563 Aug 20 11:00 shadow
[root@node01-linux ~]# salt '*' file.access /usr/src/passwd r
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' file.access /usr/src/passwd w
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' file.access /usr/src/passwd x
node02-linux.example.com:
False
2.6.2 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/a'
node02-linux.example.com:
ls: cannot access /root/a: No such file or directory
ERROR: Minions returned with non-zero exit code
[root@node01-linux ~]# salt '*' cmd.run 'touch a'
node02-linux.example.com:
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/a'
node02-linux.example.com:
-rw-r--r--. 1 root root 0 Aug 20 11:04 /root/a
[root@node01-linux ~]# salt '*' file.append /root/a "hello world" "haha" "xixi"
node02-linux.example.com:
Wrote 3 lines to "/root/a"
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/a'
node02-linux.example.com:
-rw-r--r--. 1 root root 22 Aug 20 11:04 /root/a
[root@node01-linux ~]# salt '*' cmd.run 'cat /root/a'
node02-linux.example.com:
hello world
haha
xixi
2.6.3 file.basename
#获取指定路径的基名
[root@node01-linux ~]# salt '*' file.basename '/var/www/html'
node02-linux.example.com:
html
2.6.4 file.dirname
#获取指定路径的目录名
[root@node01-linux ~]# salt '*' file.dirname '/var/www/html'
node02-linux.example.com:
/var/www
2.6.5 file.check_hash
#检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@node01-linux ~]# salt '*' cmd.run 'md5sum /etc/passwd'
node02-linux.example.com:
7a2e6944f423f7f36c63b2b88c88592a /etc/passwd
[root@node01-linux ~]# salt '*' file.check_hash /etc/passwd 7a2e6944f423f7f36c63b2b88c88592a
node02-linux.example.com:
True
2.6.6 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件, 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改, 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件, 不允许建立和删除文件 |
# 给指定文件添加属性
# 查看当前属性
[root@node01-linux ~]# salt '*' cmd.run 'lsattr /root'
node02-linux.example.com:
---------------- /root/anaconda-ks.cfg
---------------- /root/salt-repo-latest-2.el7.noarch.rpm
---------------- /root/a
#添加属性
[root@node01-linux ~]# salt '*' file.chattr /root/a operator=add attributes=ai
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' cmd.run 'lsattr /root'
node02-linux.example.com:
---------------- /root/anaconda-ks.cfg
---------------- /root/salt-repo-latest-2.el7.noarch.rpm
----ia---------- /root/a
#给指定文件去除属性
[root@node01-linux ~]# salt '*' file.chattr /root/a operator=remove attributes=i
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' cmd.run 'lsattr /root'
node02-linux.example.com:
---------------- /root/anaconda-ks.cfg
---------------- /root/salt-repo-latest-2.el7.noarch.rpm
-----a---------- /root/a
2.6.7 file.chown
#设置指定文件的属主、属组信息
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/'
node02-linux.example.com:
total 12
-rw-r--r--. 1 root root 0 Aug 20 11:05 a
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
[root@node01-linux ~]# salt '*' file.chown /root/a zcq zcq
node02-linux.example.com:
None
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
2.6.8 file.chown
在远程主机上复制文件或目录
#拷贝文件
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
[root@node01-linux ~]# salt '*' file.copy /root/a /root/b
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 b
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
#覆盖并拷贝目录,将会覆盖同名文件或目录
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 b
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
[root@node01-linux ~]# salt '*' file.copy /tmp/ /root/abc recurse=True
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
drwxrwxrwt. 9 root root 228 Aug 20 11:21 abc
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 b
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
#删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/abc'
node02-linux.example.com:
total 4
-rw-r--r--. 1 root root 836 Aug 20 11:21 ks-script-iuLFBm
drwxr-xr-x. 3 root root 17 Aug 20 11:21 systemd-private-2e4063c8f3654d1c9ac607c8b94cce24-httpd.service-LaKPOS
drwxr-xr-x. 2 root root 6 Aug 20 11:21 vmware-root
-rw-r--r--. 1 root root 0 Aug 20 11:21 yum.log
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /opt/abc'
node02-linux.example.com:
total 0
-rw-r--r--. 1 root root 0 Aug 20 11:24 1
-rw-r--r--. 1 root root 0 Aug 20 11:24 2
-rw-r--r--. 1 root root 0 Aug 20 11:24 3
[root@node01-linux ~]# salt '*' file.copy /opt/abc/ /root/abc/ recurse=True remove_existing=True
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/abc'
node02-linux.example.com:
total 0
-rw-r--r--. 1 root root 0 Aug 20 11:24 1
-rw-r--r--. 1 root root 0 Aug 20 11:24 2
-rw-r--r--. 1 root root 0 Aug 20 11:24 3
2.6.9 file.ditectory_exists
#判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /usr'
node02-linux.example.com:
total 108
dr-xr-xr-x. 2 root root 24576 Aug 20 10:43 bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 3 root root 23 Aug 19 10:00 include
dr-xr-xr-x. 27 root root 4096 Aug 19 10:01 lib
dr-xr-xr-x. 40 root root 20480 Aug 20 10:43 lib64
drwxr-xr-x. 20 root root 4096 Aug 19 10:01 libexec
drwxr-xr-x. 12 root root 131 Aug 19 10:00 local
dr-xr-xr-x. 2 root root 16384 Aug 20 10:24 sbin
drwxr-xr-x. 79 root root 4096 Aug 20 10:24 share
drwxr-xr-x. 4 root root 75 Aug 20 11:00 src
lrwxrwxrwx. 1 root root 10 Aug 19 10:00 tmp -> ../var/tmp
[root@node01-linux ~]# salt '*' file.directory_exists /usr/src/
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' file.directory_exists /usr/src
node02-linux.example.com:
True
2.6.10 file.diskusage
#递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@node01-linux ~]# salt '*' cmd.run 'du -sb /usr'
node02-linux.example.com:
1035241278 /usr
[root@node01-linux ~]# salt '*' file.diskusage /usr
node02-linux.example.com:
1032980765
2.6.11 file.file_exists
#判断指定文件是否存在
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
drwxr-xr-x. 2 root root 33 Aug 20 11:24 abc
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 b
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
[root@node01-linux ~]# salt '*' file.file_exists /root/a
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' file.file_exists /root/abc
node02-linux.example.com:
False //返回False是因为abc是目录而非文件
2.6.12 file.find
类似 find 命令并返回符合指定条件的路径列表
The options include match criteria:
name = path-glob # case sensitive
iname = path-glob # case insensitive
regex = path-regex # case sensitive
iregex = path-regex # case insensitive
type = file-types # match any listed type
user = users # match any listed user
group = groups # match any listed group
size = [+-]number[size-unit] # default unit = byte
mtime = interval # modified since date
grep = regex # search file contents
and/or actions:
delete [= file-types] # default type = 'f'
exec = command [arg ...] # where {} is replaced by pathname
print [= print-opts]
and/or depth criteria:
maxdepth = maximum depth to transverse in path
mindepth = minimum depth to transverse before checking files or directories
The default action is print=path
*path-glob:
* = match zero or more chars
? = match any char
[abc] = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y] = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c} = match a or b or c
path-regex
: a Python Regex (regular expression) pattern to match pathnames
file-types
: a string of one or more of the following:
a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket
users
: a space and/or comma separated list of user names and/or uids
groups
: a space and/or comma separated list of group names and/or gids
size-unit:
b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes
interval:
[<num>w] [<num>d] [<num>h] [<num>m] [<num>s]
where:
w: week
d: day
h: hour
m: minute
s: second
print-opts: a comma and/or space separated list of one or more of the following:
group: group name
md5: MD5 digest of file contents
mode: file permissions (as integer)
mtime: last modification time (as time_t)
name: file basename
path: file absolute path
size: file size in bytes
type: file type
user: user name
示例:
salt '*' file.find / type=f name=\*.bak size=+10m
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete
2.6.13 file.get_gid
#获取指定文件的gid
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/a'
node02-linux.example.com:
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 /root/a
[root@node01-linux ~]# salt '*' file.get_gid /root/a
node02-linux.example.com:
1000
2.6.14 file.get_group
#获取指定文件的组名
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/a'
node02-linux.example.com:
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 /root/a
[root@node01-linux ~]# salt '*' file.get_group /root/a
node02-linux.example.com:
zcq
2.6.15 file.get_hash
#获取指定文件的hash值,该值通过 sha256 算法得来
[root@node01-linux ~]# salt '*' cmd.run 'sha256sum /root/a'
node02-linux.example.com:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 /root/a
[root@node01-linux ~]# salt '*' file.get_hash /root/a
node02-linux.example.com:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
2.6.16 file.get_mode
#获取指定文件的权限,以数字方式显示
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/a'
node02-linux.example.com:
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 /root/a
[root@node01-linux ~]# salt '*' file.get_mode /root/a
node02-linux.example.com:
0644
2.6.17 file.get_selinux_context
#获取指定文件的 SELINUX 上下文信息
[root@node01-linux ~]# salt '*' cmd.run 'ls -Z /root/a'
node02-linux.example.com:
-rw-r--r--. zcq zcq system_u:object_r:admin_home_t:s0 /root/a
[root@node01-linux ~]# salt '*' file.get_selinux_context /root/a
node02-linux.example.com:
system_u:object_r:admin_home_t:s0
2.6.18 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@node01-linux ~]# salt '*' cmd.run 'sha256sum /root/a'
node02-linux.example.com:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 /root/a
[root@node01-linux ~]# salt '*' file.get_sum /root/a
node02-linux.example.com:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
[root@node01-linux ~]# salt '*' cmd.run 'md5sum /root/a'
node02-linux.example.com:
d41d8cd98f00b204e9800998ecf8427e /root/a
[root@node01-linux ~]# salt '*' file.get_sum /root/a md5
node02-linux.example.com:
d41d8cd98f00b204e9800998ecf8427e
2.6.19 file.get_uid与file.get_user
#获取指定文件的 uid 或 用户名
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/a'
node02-linux.example.com:
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 /root/a
[root@node01-linux ~]# salt '*' file.get_uid /root/a
node02-linux.example.com:
1000
[root@node01-linux ~]# salt '*' file.get_user /root/a
node02-linux.example.com:
zcq
2.6.20 file.gid_to_group
#将指定的 gid 转换为组名并显示
[root@node01-linux ~]# salt '*' file.gid_to_group 1000
node02-linux.example.com:
zcq
[root@node01-linux ~]# salt '*' file.gid_to_group 0
node02-linux.example.com:
root
2.6.21 file.group_to_gid
#将指定的组名转换为 gid 并显示
[root@node01-linux ~]# salt '*' file.group_to_gid root
node02-linux.example.com:
0
[root@node01-linux ~]# salt '*' file.group_to_gid zcq
node02-linux.example.com:
1000
2.6.22 file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i -B2
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
2.6.23 file.is_blkdev
#判断指定的文件是否是块设备文件
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /dev/sr0'
node02-linux.example.com:
brw-rw----. 1 root cdrom 11, 0 Aug 20 13:06 /dev/sr0
[root@node01-linux ~]# salt '*' file.is_blkdev /dev/sr0
node02-linux.example.com:
True
2.6.24 file.lsattr
#检查并显示出指定文件的属性信息
[root@node01-linux ~]# salt '*' cmd.run 'lsattr /root/a'
node02-linux.example.com:
---------------- /root/a
[root@node01-linux ~]# salt '*' cmd.run 'chattr +i /root/a'
node02-linux.example.com:
[root@node01-linux ~]# salt '*' cmd.run 'lsattr /root/a'
node02-linux.example.com:
----i----------- /root/a
[root@node01-linux ~]# salt '*' file.lsattr /root/a
node02-linux.example.com:
----------
/root/a:
- i
2.6.25 file.mkdir
#创建目录并设置属主、属组及权限
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
drwxr-xr-x. 2 root root 33 Aug 20 11:24 abc
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 b
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
[root@node01-linux ~]# salt '*' file.mkdir /root/def
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
drwxr-xr-x. 2 root root 33 Aug 20 11:24 abc
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 b
drwxr-xr-x. 2 root root 6 Aug 20 13:27 def
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
[root@node01-linux ~]# salt '*' file.mkdir /root/hehe zcq zcq 600
node02-linux.example.com:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root/'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
drwxr-xr-x. 2 root root 33 Aug 20 11:24 abc
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 b
drwxr-xr-x. 2 root root 6 Aug 20 13:27 def
drw-------. 2 zcq zcq 6 Aug 20 13:28 hehe
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
2.6.26 file.move
#移动或重命名
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
drwxr-xr-x. 2 root root 33 Aug 20 11:24 abc
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 b
drwxr-xr-x. 2 root root 6 Aug 20 13:27 def
drw-------. 2 zcq zcq 6 Aug 20 13:28 hehe
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
[root@node01-linux ~]# salt '*' file.move /root/b /root/c
node02-linux.example.com:
----------
comment:
'/root/b' moved to '/root/c'
result:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /root'
node02-linux.example.com:
total 12
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:05 a
drwxr-xr-x. 2 root root 33 Aug 20 11:24 abc
-rw-------. 1 root root 1246 Aug 19 10:06 anaconda-ks.cfg
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 c
drwxr-xr-x. 2 root root 6 Aug 20 13:27 def
drw-------. 2 zcq zcq 6 Aug 20 13:28 hehe
-rw-r--r--. 1 root root 5748 Apr 30 03:39 salt-repo-latest-2.el7.noarch.rpm
#移动
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /opt'
node02-linux.example.com:
total 0
drwxr-xr-x. 2 root root 33 Aug 20 11:24 abc
[root@node01-linux ~]# salt '*' file.move /root/c /opt/
node02-linux.example.com:
----------
comment:
'/root/c' moved to '/opt/'
result:
True
[root@node01-linux ~]# salt '*' cmd.run 'ls -l /opt'
node02-linux.example.com:
total 0
drwxr-xr-x. 2 root root 33 Aug 20 11:24 abc
-rw-r--r--. 1 zcq zcq 0 Aug 20 11:20 c
2.6.27 file.prepend
#把文本插入指定文件的开头
[root@node01-linux ~]# salt '*' cmd.run 'cat /root/b'
node02-linux.example.com:
hello
[root@node01-linux ~]# salt '*' file.prepend /root/b "jjyy" "123" "haha"
node02-linux.example.com:
Prepended 3 lines to "/root/b"
[root@node01-linux ~]# salt '*' cmd.run 'cat /root/b'
node02-linux.example.com:
jjyy
123
haha
hello
2.6.28 file.sed
#修改文本文件的内容
[root@node01-linux ~]# salt '*' cmd.run 'cat /root/b'
node02-linux.example.com:
jjyy
123
haha
hello
[root@node01-linux ~]# salt '*' file.sed /root/b '123' '666'
node02-linux.example.com:
----------
pid:
1829
retcode:
0
stderr:
stdout:
[root@node01-linux ~]# salt '*' cmd.run 'cat /root/b'
node02-linux.example.com:
jjyy
666
haha
hello
[root@node01-linux ~]# salt '*' cmd.run 'cat /root/b'
node02-linux.example.com:
jjyy 5 hh hh
666 hh hh xx
haha yy hh rr
hello hh gt hh
[root@node01-linux ~]# salt '*' file.sed /root/b 'hh' 'oo' flags=2
node02-linux.example.com:
----------
pid:
1843
retcode:
0
stderr:
stdout:
[root@node01-linux ~]# salt '*' cmd.run 'cat /root/b'
node02-linux.example.com:
jjyy 5 hh oo
666 hh oo xx
haha yy hh rr
hello hh gt oo
2.6.29 file.read
读取文件内容
[root@node01-linux ~]# salt '*' cmd.run 'cat /root/b'
node02-linux.example.com:
jjyy 5 hh oo
666 hh oo xx
haha yy hh rr
hello hh gt oo
[root@node01-linux ~]# salt '*' file.read /root/b
node02-linux.example.com:
jjyy 5 hh oo
666 hh oo xx
haha yy hh rr
hello hh gt oo
2.6.30 file.readdir
#列出指定目录下的所有文件或目录,包括隐藏文件
[root@node01-linux ~]# salt '*' file.readdir /root
node02-linux.example.com:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .bash_history
- salt-repo-latest-2.el7.noarch.rpm
- .pki
- a
- abc
- def
- hehe
- b.bak
- .viminfo
- b