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.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.lsattr
- 2.6.7 file.chattr
- 2.6.8 file.chown
- 2.6.9 file.copy
- 2.6.10 file.ditectory_exists
- 2.6.11 file.diskusage
- 2.6.12 file.file_exists
- 2.6.13 file.find
- 2.6.14 file.get_uid
- 2.6.15 file.get_user
- 2.6.16 file.get_gid
- 2.6.17 file.get_group
- 2.6.18 file.get_hash
- 2.6.19 file.set_mode
- 2.6.20 file.get_mode
- 2.6.21 file.get_selinux_context
- 2.6.22 file.get_sum
- 2.6.23 file.uid_to_user
- 2.6.24 file.user_to_uid
- 2.6.25 file.gid_to_group
- 2.6.26 file.group_to_gid
- 2.6.27 file.grep
- 2.6.28 file.is_blkdev
- 2.6.29 file.mkdir
- 2.6.30 file.move
- 2.6.31 file.prepend
- 2.6.32 file.write
- 2.6.33 file.sed
- 2.6.34 file.read
- 2.6.35 file.readdir
- 2.6.36 file.remove
- 2.6.37 file.rename
- 2.6.38 file.symlink
- 2.6.39 file.touch
1. SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
实验信息:
[root@master ~]# salt-key -L
Accepted Keys:
192.168.30.245
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[root@master ~]# salt '*' test.ping
192.168.30.245:
True
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
//查看所有module列表
[root@master ~]# salt '*' sys.list_modules
192.168.30.245:
- acl
- aliases
- alternatives
- ansible
- apache
- archive
- artifactory
//查看指定module的所有function
[root@master ~]# salt '*' sys.list_functions cmd
192.168.30.245:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.powershell
- cmd.powershell_all
- cmd.retcode
- cmd.run
- cmd.run_all
//查看指定module的用法
[root@master ~]# salt '*' sys.doc cmd
cmd.exec_code:
Pass in two strings, the first naming the executable language, aka -
python2, python3, ruby, perl, lua, etc. the second string containing
the code you wish to execute. The stdout will be returned.
//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt '*' test.echo,cmd.run,service.status hello,hostname,salt-minion192.168.30.245:
----------
cmd.run:
minion
service.status:
True
test.echo:
hello
2. SaltStack常用模块
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
返回所有活动的tcp连接
[root@master ~]# salt '*' network.active_tcp
192.168.30.245:
----------
0:
----------
local_addr:
192.168.30.245
local_port:
22
remote_addr:
192.168.30.1
remote_port:
50632
1:
----------
local_addr:
192.168.30.245
local_port:
22
remote_addr:
192.168.30.1
remote_port:
50630
2:
----------
local_addr:
192.168.30.245
local_port:
43014
remote_addr:
192.168.30.244
remote_port:
4505
[root@minion ~]# netstat -anlt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.30.245:22 192.168.30.1:50632 ESTABLISHED
tcp 0 0 192.168.30.245:22 192.168.30.1:50630 ESTABLISHED
tcp 0 0 192.168.30.245:43014 192.168.30.244:4505 ESTABLISHED
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt '*' network.calc_net 192.168.30.2 255.255.255.0
192.168.30.245:
192.168.30.0/24
[root@master ~]# salt '*' network.calc_net 192.168.30.2 255.255.240.0
192.168.30.245:
192.168.16.0/20
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt '*' network.connect baidu.com 80
192.168.30.245:
----------
comment:
Successfully connected to baidu.com (39.156.69.79) on tcp port 80
result:
True
2.1.4 network.default_route
查看默认路由
[root@master ~]# salt '*' network.default_route
192.168.30.245:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.30.2
interface:
ens33
netmask:
0.0.0.0
|_
----------
addr_family:
inet6
destination:
::/0
flags:
!n
gateway:
::
interface:
lo
netmask:
|_
----------
addr_family:
inet6
destination:
::/0
flags:
!n
gateway:
::
interface:
lo
netmask:
[root@minion ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.30.2 0.0.0.0 UG 100 0 0 ens33
192.168.30.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt '*' network.get_fqdn
192.168.30.245:
minion
2.1.6 network.get_hostname
获取主机名
[root@master ~]# salt '*' network.get_hostname
192.168.30.245:
minion
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt '*' network.get_route 192.168.31.233
192.168.30.245:
----------
destination:
192.168.31.233
gateway:
192.168.30.2
interface:
ens33
source:
192.168.30.245
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt '*' network.hw_addr ens33
192.168.30.245:
00:0c:29:c8:03:9e
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@minion ~]# ip addr add 192.163.30.132 dev ens33
[root@master ~]# salt '*' network.ifacestartswith 192.163
192.168.30.245:
- ens33
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt '*' network.in_subnet 192.168.30.0/24
192.168.30.245:
True
2.1.11 network.interface
返回指定网卡的信息
[root@master ~]# salt '*' network.interface ens33
192.168.30.245:
|_
----------
address:
192.168.30.245
broadcast:
192.168.30.255
label:
ens33
netmask:
255.255.255.0
|_
----------
address:
192.163.30.132
broadcast:
None
label:
ens33
netmask:
255.255.255.255
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt '*' network.interface_ip ens33
192.168.30.245:
192.168.30.245
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt '*' network.interfaces
192.168.30.245:
----------
ens33:
----------
hwaddr:
00:0c:29:c8:03:9e
inet:
|_
----------
address:
192.168.30.245
broadcast:
192.168.30.255
label:
ens33
netmask:
255.255.255.0
|_
----------
address:
192.163.30.132
broadcast:
None
label:
ens33
netmask:
255.255.255.255
inet6:
|_
----------
address:
fe80::def9:ed60:13e2:5273
prefixlen:
64
scope:
link
up:
True
ens37:
----------
hwaddr:
00:0c:29:c8:03:a8
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@master ~]# salt '*' network.ip_addrs
192.168.30.245:
- 192.163.30.132
- 192.168.30.245
2.1.15 network.netstat
返回所有打开的端口和状态
root@master ~]# salt '*' network.netstat
192.168.30.245:
|_
----------
inode:
19861
local-address:
0.0.0.0:22
program:
1002/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
20354
local-address:
127.0.0.1:25
program:
1201/master
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt '*' network.ping baidu.com
192.168.30.245:
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=30.7 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=79.4 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=270 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=31.5 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 30.778/103.165/270.871/98.815 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt '*' network.reverse_ip 192.168.30.245
192.168.30.245:
245.30.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用
[root@master ~]# salt '*' service.available sshd
192.168.30.245:
True
2.2.2 service.get_all
获取所有正在运行的服务
[root@master ~]# salt '*' service.get_all
192.168.30.245:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- auditd
- autovt@
- basic.target
2.2.3 service.disabled
检查指定服务是否开机不自动启动
[root@master ~]# salt '*' service.disabled httpd
192.168.30.245:
False ##httpd是开机自启
2.2.4 service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt '*' service.enabled httpd
192.168.30.245:
True
2.2.5 service.disable
设置指定服务开机不自动启动
[root@master ~]# salt '*' service.disable httpd
192.168.30.245:
True
[root@master ~]# salt '*' service.enabled httpd
192.168.30.245:
False
2.2.6 service.enable
设置指定服务开机自动启动
[root@master ~]# salt '*' service.enable httpd
192.168.30.245:
True
[root@master ~]# salt '*' service.enabled httpd
192.168.30.245:
True
2.2.7 service.reload
重新加载指定服务
[root@minion ~]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[root@master ~]# salt '*' service.reload httpd
192.168.30.245:
True
[root@minion ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
2.2.8 service.stop
停止指定服务
[root@master ~]# salt '*' service.stop httpd
192.168.30.245:
True
2.2.9 service.start
启动指定服务
[root@master ~]# salt '*' service.start httpd
192.168.30.245:
True
2.2.10 service.restart
重启指定服务
[root@master ~]# salt '*' service.restart httpd
192.168.30.245:
True
2.2.11 service.status
查看指定服务的状态
[root@master ~]# salt '*' service.status httpd
192.168.30.245:
True
关闭服务
[root@master ~]# salt '*' service.status httpd
192.168.30.245:
False
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@master ~]# salt '*' pkg.download wget
192.168.30.245:
----------
wget:
/var/cache/yum/packages/wget-1.14-15.el7.x86_64.rpm //下载的软件在这
2.3.2 pkg.file_list
列出指定包或系统中已安装的所有包的文件
//列出已安装的apache软件包提供的所有文件
[root@master ~]# salt '*' pkg.file_list httpd // rpm -ql httpd
192.168.30.245:
----------
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
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt '*' pkg.file_list
2.3.3 pkg.group_info
查看包组的信息
[root@master ~]# salt '*' pkg.group_info 'Development Tools'
192.168.30.245:
----------
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
- flex
- gcc
- gcc-c++
- gettext
- libtool
- make
- patch
- pkgconfig
- redhat-rpm-config
- rpm-build
- rpm-sign
optional:
- ElectricFence
- ant
- babel
- bzr
- chrpath
- cmake
- compat-gcc-44
- compat-gcc-44-c++
- cvs
- dejagnu
- expect
- gcc-gnat
- gcc-objc
- gcc-objc++
- imake
- javapackages-tools
- ksc
- libstdc++-docs
- mercurial
- mod_dav_svn
- nasm
- perltidy
- python-docs
- rpmdevtools
- rpmlint
- systemtap-sdt-devel
- systemtap-server
type:
package group
2.3.4pkg.group_list
列出系统中所有的包组
[root@master ~]# salt '*' pkg.group_list
192.168.30.245:
----------
available:
- Additional Development
- Anaconda Tools
- Backup Client
- Backup Server
- Base
- Common NetworkManager submodules
- Compatibility Libraries
- Conflicts (Server)
- Console Internet Tools
- Core
- DNS Name Server
- Debugging Tools
- Desktop Debugging and Performance Tools
- Development Tools
- Dial-up Networking Support
- Directory Client
- Directory Server
- E-mail Server
- Emacs
- FTP Server
- File and Storage Server
- Fonts
- GNOME
- GNOME Applications
- Graphical Administration Tools
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Hardware Monitoring Utilities
- Hyper-v platform specific packages
- Identity Management Server
- Infiniband Support
- Input Methods
- Internet Browser
- Java Platform
- KDE
- KVM platform specific packages
- Large Systems Performance
- Legacy UNIX Compatibility
- Legacy X Window System Compatibility
- Load Balancer
- Mainframe Access
- MariaDB Database Client
- MariaDB Database Server
- Multimedia
- Network File System Client
- Network Infrastructure Server
- Networking Tools
- PHP Support
- Performance Tools
- Perl Support
- Perl for Web
- Platform Development
- PostgreSQL Database Client
- PostgreSQL Database Server
- Print Server
- Printing Client
- Python
- Remote Desktop Clients
- Remote Management for Linux
- Ruby Support
- Scientific Support
- Security Tools
- Smart Card Support
- System Administration Tools
- System Management
- Technical Writing
- VMware platform specific packages
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Web Server
- Web Servlet Engine
- X Window System
available environments:
- Minimal Install
- Infrastructure Server
- File and Print Server
- Basic Web Server
- Virtualization Host
- Server with GUI
available languages:
----------
installed:
installed environments:
2.3.5 pkg.install
安装软件
[root@master ~]# salt '*' pkg.install wget
192.168.30.245:
----------
wget:
----------
new:
1.14-15.el7
old:
2.3.6 pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt '*' pkg.list_downloaded
[root@master ~]# salt '*' pkg.list_downloaded
192.168.30.245:
----------
wget:
----------
1.14-15.el7:
----------
creation_date_time:
2020-08-17T21:54:07
creation_date_time_t:
1597672447
path:
/var/cache/yum/packages/wget-1.14-15.el7.x86_64.rpm
size:
559832
2.3.7 pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt '*' pkg.list_pkgs
192.168.30.245:
----------
GeoIP:
1.5.0-11.el7
NetworkManager:
1:1.8.0-9.el7
NetworkManager-config-server:
1:1.8.0-9.el7
NetworkManager-libnm:
1:1.8.0-9.el7
NetworkManager-team:
1:1.8.0-9.el7
NetworkManager-tui:
1:1.8.0-9.el7
2.3.8 pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt '*' pkg.owner /usr/sbin/apachectl //rpm -qf /usr/sbin/apachectl
192.168.30.245:
httpd
[root@master ~]# salt '*' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
192.168.30.245:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
2.3.9 pkg.remove
卸载指定软件
[root@master ~]# salt '*' cmd.run 'rpm -qa|grep wget'
192.168.30.245:
wget-1.14-15.el7.x86_64
//若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt '*' pkg.upgrade name=openssl
192.168.30.245:
----------
openssl:
----------
new:
1:1.0.2k-14.el7
old:
1:1.0.2k-8.el7
openssl-libs:
----------
new:
1:1.0.2k-14.el7
old:
1:1.0.2k-8.el7
//若想升级系统中所有的软件包则把 name 参数去掉即可
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt '*' state.show_highstate
192.168.30.245:
----------
mariadb-install:
----------
__env__:
base
__sls__:
web.apache.apache-install
pkg:
|_
----------
name:
mariadb
- installed
|_
----------
order:
10000
mariadb-service:
----------
__env__:
prod
__sls__:
web.apache.apache-service
service:
|_
----------
name:
mariadb
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate
执行高级状态
[root@master ~]# salt '*' state.highstate mysql.mariadb.mariadb-install
192.168.30.245:
----------
ID: mariadb-install
Function: pkg.installed
Name: mariadb
Result: None
Comment: The following packages would be installed/updated: mariadb
Started: 18:32:30.305475
Duration: 1159.828 ms
Changes:
----------
ID: mariadb-service
Function: service.running
Name: mariadb
Result: None
Comment: Service mariadb not present; if created in this state run, it would have been started
Started: 18:32:31.511794
Duration: 11.765 ms
Changes:
Summary for 192.168.30.245
------------
Succeeded: 2 (unchanged=2)
Failed: 0
------------
Total states run: 2
Total run time: 1.172 s
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt '*' state.show_state_usage
192.168.30.245:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.apache.apache-install
prod:
----------
count_all:
1
count_unused:
0
count_used:
1
unused:
used:
- web.apache.apache-service
2.4.4 state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt '*' state.show_top
192.168.30.245:
----------
base:
- web.apache.apache-install
prod:
- web.apache.apache-service
2.4.5 state.top
执行指定的top file,而不是默认的
[root@master ~]# salt '*' state.top runtime.sls
192.168.30.245:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:17:20.129390
Duration: 917.659 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 20:17:21.048144
Duration: 46.958 ms
Changes:
Summary for 192.168.30.245
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 964.617 ms
2.4.6 state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt '*' state.show_sls web.apache.apache-install
192.168.30.245:
----------
mariadb-install:
----------
__env__:
base
__sls__:
web.apache.apache-install
pkg:
|_
----------
name:
mariadb
- installed
|_
----------
order:
10000
2.5 SaltStack常用模块之salt-cp
salt-cp
能够很方便的把 master 上的文件批量传到 minion上
//复制
[root@master ~]# salt-cp '*' /root/yyz /srv/
192.168.30.245:
----------
/srv/yyz:
[root@master ~]# salt '*' cmd.run 'ls /srv/'
192.168.30.245:
yyz
//复制多个文件
[root@master ~]# salt-cp '*' /root/111 /root/222 /srv/
192.168.30.245:
----------
/srv/111:
True
/srv/222:
True
[root@master ~]# salt '*' cmd.run 'ls /srv/'
192.168.30.245:
111
222
yyz
//改名
[root@master ~]# salt-cp '*' /root/111 /srv/777
192.168.30.245:
----------
/srv/777:
True
2.6 SaltStack常用模块之file
2.6.1 file.access
检查指定路径是否存在
[root@master ~]# salt '*' file.access /root/111 f
192.168.30.245:
False
[root@master ~]# salt '*' file.access /srv/111 f
192.168.30.245:
True
检查指定文件的权限信息
[root@master ~]# salt '*' file.access /root/zyy r
192.168.30.245:
True
[root@master ~]# salt '*' file.access /root/zyy w
192.168.30.245:
True
[root@master ~]# salt '*' file.access /root/zyy x
192.168.30.245:
False
2.6.2 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt '*' file.append /root/zyy "hello world"
192.168.30.245:
Wrote 1 lines to "/root/zyy"
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
hello world
2.6.3 file.basename
获取指定路径的基名
[root@master ~]# salt '*' file.basename /root/111/222
192.168.30.245:
222
2.6.4 file.dirname
获取指定路径的目录名
[root@master ~]# salt '*' file.dirname /root/111/222
192.168.30.245:
/root/111
2.6.5 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt '*' cmd.run 'md5sum /root/zyy'
192.168.30.245:
6f5902ac237024bdd0c176cb93063dc4 /root/zyy
[root@master ~]# salt '*' file.check_hash /root/zyy 6f5902ac237024bdd0c176cb93063dc4
192.168.30.245:
True
2.6.6 file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt '*' file.lsattr /root/zyy
192.168.30.245:
----------
/root/zyy:
2.6.7 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件, 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改, 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件, 不允许建立和删除文件 |
给指定文件添加属性
//给文件添加属性a
[root@master ~]# salt '*' file.chattr /root/zyy operator=add attributes=a
192.168.30.245:
True
[root@master ~]# salt '*' file.lsattr /root/zyy
192.168.30.245:
----------
/root/zyy:
- a
//不能覆盖
[root@master ~]# salt '*' cmd.run 'echo zyy > /root/zyy'
192.168.30.245:
/bin/sh: /root/zyy: Operation not permitted //没有权限
//给目录添加属性a
[root@master ~]# salt '*' file.chattr /root/yyz operator=add attributes=a
192.168.30.245:
True
[root@master ~]# salt '*' cmd.run 'lsattr -d /root/yyz'
192.168.30.245:
-----a---------- /root/yyz
[root@master ~]# salt '*' file.touch /root/yyz/111
192.168.30.245:
True
//不能删除
[root@master ~]# salt '*' file.remove /root/yyz/111
192.168.30.245:
ERROR: Could not remove '/root/yyz/111': [Errno 1] Operation not permitted: '/root/yyz/111'
//给文件添加属性i
[root@master ~]# salt '*' file.chattr /root/first operator=add attributes=i
192.168.30.245:
True
[root@master ~]# salt '*' file.lsattr /root/first
192.168.30.245:
----------
/root/first:
- i
//不能删除
[root@master ~]# salt '*' file.remove /root/first
192.168.30.245:
ERROR: Could not remove '/root/first': [Errno 1] Operation not permitted: '/root/first'
//给目录添加属性i
[root@master ~]# salt '*' file.chattr /root/222 operator=add attributes=i
192.168.30.245:
True
[root@master ~]# salt '*' cmd.run 'lsattr -d /root/222'
192.168.30.245:
----i----------- /root/222
[root@master ~]# salt '*' file.touch /root/222/333
192.168.30.245:
ERROR: Permission denied
给指定文件去除属性
[root@master ~]# salt '*' file.chattr /root/zyy operator=remove attributes=a
192.168.30.245:
True
2.6.8 file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt '*' cmd.run 'ls -l /root/zyy'
192.168.30.245:
-rw-r--r-- 1 root root 12 Aug 18 18:51 /root/zyy
[root@master ~]# salt '*' file.chown /root/zyy zyy zyy
192.168.30.245:
None
[root@master ~]# salt '*' cmd.run 'ls -l /root/zyy'
192.168.30.245:
-rw-r--r-- 1 zyy zyy 12 Aug 18 18:51 /root/zyy
2.6.9 file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt '*' file.copy /root/zyy /tmp/777
192.168.30.245:
True
[root@master ~]# salt '*' cmd.run 'ls /tmp/'
192.168.30.245:
777
覆盖并拷贝目录,将会覆盖同名文件或目录
192.168.30.245:
drwxr-xr-x 2 root root 6 Aug 18 21:55 /tmp/zyy
[root@master ~]# salt '*' file.copy /root/zyy /tmp/zyy recurse=True
192.168.30.245:
True
[root@master ~]# salt '*' cmd.run 'ls -ld /tmp/zyy'
192.168.30.245:
drw-r--r-- 2 zyy zyy 6 Aug 18 21:55 /tmp/zyy
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
192.168.69.202:
total 336
drwxr-xr-x. 2 root root 6 Feb 24 15:02 abc
-rw-r--r--. 1 root root 1259 Feb 24 15:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 219068 Feb 24 15:02 libevent-2.0.21-4.el7.x86_64.rpm
-rw-r--r--. 1 root root 86564 Feb 24 15:02 libevent-devel-2.0.21-4.el7.x86_64.rpm
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-httpd.service-qGPJuL
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vgauthd.service-8p0jur
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vmtoolsd.service-ouRK2O
drwxr-xr-x. 2 root root 6 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vmtoolsd.service-ouRK2Ovmware-root
-rw-r--r--. 1 root root 493 Feb 24 15:02 yum_save_tx.2019-02-22.14-22.RXRzka.yumtx
-rw-r--r--. 1 root root 22692 Feb 24 15:02 zabbix_agentd.log
[root@master ~]# salt '*' cmd.run 'ls -l /opt/abc'
192.168.69.202:
total 0
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vgauthd.service-kI2C1K
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vmtoolsd.service-ZzRiqC
//拷贝目录
[root@master ~]# salt '*' file.copy /opt/abc/ /root/abc/ recurse=True remove_existing=True
192.168.69.202:
True
[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
192.168.69.202:
total 0
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vgauthd.service-kI2C1K
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vmtoolsd.service-ZzRiqC
2.6.10 file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt '*' file.directory_exists /root/dir
192.168.30.245:
True
[root@master ~]# salt '*' file.directory_exists /opt/abc
192.168.30.245:
False
2.6.11 file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt '*' cmd.run 'du -sb /root/'
192.168.30.245:
8281 /root/
[root@master ~]# salt '*' file.diskusage /root/
192.168.30.245:
8065
2.6.12 file.file_exists
判断指定文件是否存在
[root@master ~]# salt '*' file.file_exists /root/zyy
192.168.30.245:
True
[root@master ~]# salt '*' file.file_exists /root/yyz
192.168.30.245:
False
2.6.13 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.14 file.get_uid
获取指定文件的uid
[root@master ~]# salt '*' file.get_uid /root/zyy
192.168.30.245:
1000
2.6.15 file.get_user
获取指定文件的用户名
[root@master ~]# salt '*' file.get_user /root/zyy
192.168.30.245:
zyy
2.6.16 file.get_gid
获取指定文件的gid
[root@master ~]# salt '*' file.get_gid /root/zyy
192.168.30.245:
1000
2.6.17 file.get_group
获取指定文件的组名
[root@master ~]# salt '*' file.get_group /root/zyy
192.168.30.245:
zyy
2.6.18 file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt '*' cmd.run 'sha256sum /root/zyy'
192.168.30.245:
a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447 /root/zyy
[root@master ~]# salt '*' file.get_hash /root/zyy
192.168.30.245:
a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
2.6.19 file.set_mode
给指定文件设置权限
[root@master ~]# salt '*' file.get_mode /root/yyz
192.168.30.245:
0644
[root@master ~]# salt '*' file.set_mode /root/yyz 755
192.168.30.245:
0755
[root@master ~]# salt '*' file.get_mode /root/yyz
192.168.30.245:
0755
2.6.20 file.get_mode
获取指定文件的权限,以数字方式显示
192.168.30.245:
-rw-r--r-- 1 zyy zyy 12 Aug 18 18:51 /root/zyy
[root@master ~]# salt '*' file.get_mode /root/zyy
192.168.30.245:
0644
2.6.21 file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt '*' cmd.run 'ls -Z /etc/profile'
192.168.30.245:
-rw-r--r--. root root system_u:object_r:etc_t:s0 /etc/profile
[root@master ~]# salt '*' file.get_selinux_context /etc/profile
192.168.30.245:
system_u:object_r:etc_t:s0
2.6.22 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt '*' cmd.run 'md5sum /root/zyy'
192.168.30.245:
6f5902ac237024bdd0c176cb93063dc4 /root/zyy
[root@master ~]# salt '*' file.get_sum /root/zyy md5
192.168.30.245:
6f5902ac237024bdd0c176cb93063dc4
2.6.23 file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@master ~]# salt '*' file.uid_to_user 1000
192.168.30.245:
zyy
2.6.24 file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@master ~]# salt '*' file.user_to_uid zyy
192.168.30.245:
1000
2.6.25 file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt '*' file.gid_to_group 1000
192.168.30.245:
zyy
2.6.26 file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt '*' file.group_to_gid 1000
192.168.30.245:
1000
2.6.27 file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
192.168.30.245:
----------
pid:
49948
retcode:
0
stderr:
stdout:
IPADDR=192.168.30.245
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
192.168.30.245:
----------
pid:
50330
retcode:
0
stderr:
stdout:
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.30.245
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -l
192.168.30.245:
----------
pid:
50659
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-ens33
2.6.28 file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt '*' file.is_blkdev /dev/sr0
192.168.30.245:
True
2.6.29 file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt '*' file.mkdir /root/111
192.168.30.245:
True
[root@master ~]# salt '*' file.mkdir /root/222 zyy zyy 700
192.168.30.245:
True
2.6.30 file.move
移动或重命名
[root@master ~]# salt '*' file.move /root/zyy /tmp/
192.168.30.245:
----------
comment:
'/root/zyy' moved to '/tmp/'
result:
True
[root@master ~]# salt '*' file.move /tmp/zyy /root/yyz
192.168.30.245:
----------
comment:
'/tmp/zyy' moved to '/root/yyz'
result:
True
2.6.31 file.prepend
把文本插入指定文件的开头
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
zyy
[root@master ~]# salt '*' file.prepend /root/zyy "111"
192.168.30.245:
Prepended 1 lines to "/root/zyy"
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
111
zyy
2.6.32 file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
zyy zyy
[root@master ~]# salt '*' file.write /root/zyy "1111111"
192.168.30.245:
Wrote 1 lines to "/root/zyy"
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
1111111
2.6.33 file.sed
修改文本文件的内容
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
zyy
zyy
zyy
[root@master ~]# salt '*' file.sed /root/zyy "zyy" "yyz"
192.168.30.245:
----------
pid:
61779
retcode:
0
stderr:
stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
yyz
yyz
yyz
//flags=2 第2个
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
yyz yyz
yyz yyz
yyz yyz
[root@master ~]# salt '*' file.sed /root/zyy "yyz" "zyy" flags=2
192.168.30.245:
----------
pid:
63935
retcode:
0
stderr:
stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/zyy'
192.168.30.245:
yyz zyy
yyz zyy
yyz zyy
2.6.34 file.read
读取文件内容
[root@master ~]# salt '*' file.read /root/zyy
192.168.30.245:
333 222
333 222
333 222
2.6.35 file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt '*' file.readdir /root/
192.168.30.245:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .bash_history
- .pki
- dir
- 111
- zyy.bak
- zyy
- .viminfo
2.6.36 file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt '*' file.remove /root/dir
192.168.30.245:
True
2.6.37 file.rename
重命名文件或目录
[root@master ~]# salt '*' cmd.run 'ls /root/'
192.168.30.245:
zyy
[root@master ~]# salt '*' file.rename /root/zyy /root/yyz
192.168.30.245:
True
[root@master ~]# salt '*' cmd.run 'ls /root/'
192.168.30.245:
yyz
2.6.38 file.symlink
给指定的文件创建软链接
[root@master ~]# salt '*' file.symlink /root/yyz /root/zyy
192.168.30.245:
True
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.30.245:
total 0
-rwxr-xr-x 1 root root 0 Aug 19 22:36 yyz
lrwxrwxrwx 1 root root 9 Aug 19 22:42 zyy -> /root/yyz
2.6.39 file.touch
创建空文件或更新时间戳
[root@master ~]# salt '*' file.touch /root/111
192.168.30.245:
True
[root@master ~]# salt '*' cmd.run 'ls /root/'
192.168.30.245:
111
yyz
zyy