1. SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
//查看所有module列表
[root@zzl01 ~]# salt 'zzl02' sys.list_modules
zzl02:
- acl
- aliases
- alternatives
- ansible
- archive
- artifactory
- at
- augeas
- beacons
- bigip
- bridge
- btrfs
- buildout
- chroot
- cloud
- cmd
- composer
- config
- consul
······
//查看指定module的所有function
[root@zzl01 ~]# salt 'zzl02' sys.list_functions cmd
zzl02:
- 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
//查看指定module的用法
- cmd.which_bin
[root@zzl01 ~]# 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.
All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.
CLI Example:
salt '*' cmd.exec_code ruby 'puts "cheese"'
salt '*' cmd.exec_code ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
······
//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@zzl01 ~]# salt '*' test.echo,cmd.run,service.status hello,hostname,salt-minion
zzl02:
----------
cmd.run:
zzl02
service.status:
True
test.echo:
hello
2. SaltStack常用模块
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
返回所有活动的tcp连接
[root@zzl01 ~]# salt '*' network.active_tcp
zzl02:
----------
0:
----------
local_addr:
192.168.20.120
local_port:
22
remote_addr:
192.168.20.1
remote_port:
61516
1:
----------
local_addr:
192.168.20.120
local_port:
55332
remote_addr:
192.168.20.99
remote_port:
4505
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@zzl01 ~]# salt '*' network.calc_net 192.168.20.254 255.255.255.254
zzl02:
192.168.20.254/31
[root@zzl01 ~]# salt '*' network.calc_net 192.168.20.254 255.255.255.0
zzl02:
192.168.20.0/24
2.1.3 network.connect
[root@zzl01 ~]# salt '*' network.connect www.baidu.com 80
zzl02:
----------
comment:
Successfully connected to www.baidu.com (182.61.200.7) on tcp port 80
result:
True
2.1.4 network.default_route
查看默认路由
[root@zzl01 ~]# salt '*' network.default_route
zzl02:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.20.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:
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@zzl01 ~]# salt '*' network.get_fqdn
zzl02:
zzl02
2.1.6 network.get_hostname
获取主机名
[root@zzl01 ~]# salt '*' network.get_hostname
zzl02:
zzl02
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@zzl01 ~]# salt '*' network.get_route 192.168.20.150
zzl02:
----------
destination:
192.168.20.150
gateway:
None
interface:
ens33
source:
192.168.20.120
[root@zzl01 ~]# salt '*' network.get_route 192.168.30.150
zzl02:
----------
destination:
192.168.30.150
gateway:
192.168.20.2
interface:
ens33
source:
192.168.20.120
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@zzl01 ~]# salt '*' network.hw_addr ens33
zzl02:
00:0c:29:90:27:d3
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@zzl01 ~]# salt '*' network.ifacestartswith 192.168
zzl02:
- virbr0
- ens33
[root@zzl01 ~]# salt '*' network.ifacestartswith 192.168.20
zzl02:
- ens33
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@zzl01 ~]# salt '*' network.in_subnet 192.168.20.0/24
zzl02:
True
2.1.11 network.interface
返回指定网卡的信息
[root@zzl01 ~]# salt '*' network.interface ens33
zzl02:
|_
----------
address:
192.168.20.120
broadcast:
192.168.20.255
label:
ens33
netmask:
255.255.255.0
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@zzl01 ~]# salt '*' network.interface_ip ens33
zzl02:
192.168.20.120
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
zzl02:
----------
ens33:
----------
hwaddr:
00:0c:29:90:27:d3
inet:
|_
----------
address:
192.168.20.120
broadcast:
192.168.20.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::cc4:6cc0:d6df:6335
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
virbr0:
----------
hwaddr:
52:54:00:d2:0d:9f
inet:
|_
----------
address:
192.168.122.1
broadcast:
192.168.122.255
label:
virbr0
netmask:
255.255.255.0
up:
True
virbr0-nic:
----------
hwaddr:
52:54:00:d2:0d:9f
up:
False
2.1.14 network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址
[root@zzl01 ~]# salt '*' network.ip_addrs
zzl02:
- 192.168.20.120
- 192.168.122.1
2.1.15 network.netstat
返回所有打开的端口和状态
[root@zzl01 ~]# salt '*' network.netstat
zzl02:
|_
----------
inode:
17142
local-address:
0.0.0.0:111
program:
1/systemd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
211620
local-address:
0.0.0.0:80
program:
51392/nginx:
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
24940
local-address:
192.168.122.1:53
program:
1427/dnsmasq
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
23286
local-address:
0.0.0.0:22
program:
1059/sshd
proto:
tcp
recv-q:
0
·······
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@zzl01 ~]# salt '*' network.ping baidu.com
zzl02:
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=33.2 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=39.0 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=37.4 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=41.8 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 33.204/37.893/41.852/3.138 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@zzl01 ~]# salt '*' network.reverse_ip 192.168.20.99
zzl02:
99.20.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用
[root@zzl01 ~]# salt '*' service.available sshd
zzl02:
True
[root@zzl01 ~]# salt '*' service.available vsftpd
zzl02:
False
2.2.2 service.get_all
获取所有正在运行的服务
[root@zzl01 ~]# salt '*' service.get_all
zzl02:
- ModemManager
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- abrt-ccpp
- abrt-oops
- abrt-pstoreoops
- abrt-vmcore
- abrt-xorg
- abrtd
- accounts-daemon
- alsa-restore
- alsa-state
- alsa-store
- anaconda
- anaconda-direct
·······
2.2.3 service.disabled
检查指定服务是否开机不自动启动
[root@zzl01 ~]# salt '*' service.disabled httpd
zzl02:
True
[root@zzl01 ~]# salt '*' service.disabled nginx
zzl02:
False
2.2.4 service.enabled
检查指定服务是否开机自动启动
[root@zzl01 ~]# salt '*' service.enabled nginx
zzl02:
True
2.2.5 service.enable
设置指定服务开机不自动启动
[root@zzl01 ~]# salt '*' service.enable nginx
zzl02:
True
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@zzl01 ~]# salt '*' pkg.download nginx
zzl02:
----------
nginx:
/var/cache/yum/packages/nginx-1.16.1-1.el7.x86_64.rpm
2.3.2 pkg.file_list
列出指定包或系统中已安装的所有包的文件
//列出已安装的nginx软件包提供的所有文件
[root@zzl01 ~]# salt '*' pkg.file_list nginx
zzl02:
----------
errors:
files:
- /etc/logrotate.d/nginx
- /etc/nginx/fastcgi.conf
- /etc/nginx/fastcgi.conf.default
- /etc/nginx/fastcgi_params
- /etc/nginx/fastcgi_params.default
- /etc/nginx/koi-utf
- /etc/nginx/koi-win
- /etc/nginx/mime.types
- /etc/nginx/mime.types.default
- /etc/nginx/nginx.conf
- /etc/nginx/nginx.conf.default
- /etc/nginx/scgi_params
- /etc/nginx/scgi_params.default
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@zzl01 ~]# salt '*' pkg.file_list
zzl02:
----------
errors:
files:
VALUE_TRIMMED
2.3.3 pkg.group_info
查看包组的信息
[root@zzl01 ~]# salt '*' pkg.group_info 'Development Tools'
zzl02:
----------
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
- ccache
- chrpath
- clips
- clips-devel
- clips-doc
- clips-emacs
- clips-xclips
- clipsmm-devel
- clipsmm-doc
- cmake
- cmucl
- colordiff
- compat-gcc-44
- compat-gcc-44-c++
- cvs
- cvsps
- darcs
- dejagnu
- email2trac
- expect
- ftnchek
- gcc-gnat
- gcc-objc
- gcc-objc++
- ghc
- git
- haskell-platform
- imake
- javapackages-tools
- ksc
- libstdc++-docs
- lua
- mercurial
- mock
- mod_dav_svn
- nasm
- nqc
- nqc-doc
- ocaml
- perltidy
- python-docs
- qgit
- rpmdevtools
- rpmlint
- sbcl
- scorep
- systemtap-sdt-devel
- systemtap-server
- trac
- trac-git-plugin
- trac-mercurial-plugin
- trac-webadmin
- translate-toolkit
type:
package group
2.3.4 pkg.group_list
列出系统中所有的包组
[root@zzl01 ~]# salt '*' pkg.group_list
zzl02:
----------
available:
- Additional Development
- Anaconda Tools
- Backup Client
- Backup Server
- Base
- Buildsystem building group
- CentOS Linux Client product core
- CentOS Linux ComputeNode product core
- CentOS Linux Server product core
- CentOS Linux Workstation product core
- Cinnamon
- Common NetworkManager submodules
- Compatibility Libraries
- Conflicts (Client)
- Conflicts (ComputeNode)
- Conflicts (Server)
- Conflicts (Workstation)
- 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
- Educational Software
- Electronic Lab
- Emacs
- FTP Server
- Fedora Packager
- File and Storage Server
- Fonts
- GNOME
- GNOME Applications
- General Purpose Desktop
- Graphical Administration Tools
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Hardware Monitoring Utilities
- Haskell
- High Availability
- Hyper-v platform specific packages
- Identity Management Server
- Infiniband Support
- Input Methods
- Internet Applications
- Internet Browser
- Java Platform
- KDE
- KDE Applications
- KDE Multimedia Support
- KVM platform specific packages
- LXQt Desktop
- Large Systems Performance
- Legacy UNIX Compatibility
- Legacy X Window System Compatibility
- Load Balancer
- MATE
- Mainframe Access
- MariaDB Database Client
- MariaDB Database Server
- Milkymist
- Multimedia
- Network File System Client
- Network Infrastructure Server
- Networking Tools
- Office Suite and Productivity
- 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
- Resilient Storage
- Ruby Support
- Scientific Support
- Security Tools
- Smart Card Support
- System Administration Tools
- System Management
- Technical Writing
- TurboGears application framework
- VMware platform specific packages
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Web Server
- Web Servlet Engine
- X Window System
- Xfce
available environments:
- Minimal Install
- Compute Node
- Infrastructure Server
- File and Print Server
- Cinnamon Desktop
- MATE Desktop
- Basic Web Server
- Virtualization Host
- Server with GUI
- GNOME Desktop
- KDE Plasma Workspaces
- Development and Creative Workstation
available languages:
----------
installed:
installed environments:
2.3.6 pkg.list_downloaded
列出已下载到本地的软件包
[root@zzl01 ~]# salt '*' pkg.list_downloaded
zzl02:
----------
nginx:
----------
1:1.16.1-1.el7:
----------
creation_date_time:
2020-08-18T18:30:50
creation_date_time_t:
1597746650
path:
/var/cache/yum/packages/nginx-1.16.1-1.el7.x86_64.rpm
size:
575413
2.3.7 pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@zzl01 ~]# salt '*' pkg.list_pkgs
zzl02:
----------
GConf2:
3.2.6-8.el7
GeoIP:
1.5.0-11.el7
ModemManager:
1.6.0-2.el7
ModemManager-glib:
1.6.0-2.el7
NetworkManager:
1:1.8.0-9.el7
NetworkManager-adsl:
1:1.8.0-9.el7
NetworkManager-config-server:
1:1.8.0-9.el7
NetworkManager-glib:
1:1.8.0-9.el7
NetworkManager-libnm:
1:1.8.0-9.el7
NetworkManager-libreswan:
1.2.4-2.el7
NetworkManager-libreswan-gnome:
1.2.4-2.el7
NetworkManager-ppp:
1:1.8.0-9.el7
NetworkManager-team:
1:1.8.0-9.el7
NetworkManager-tui:
1:1.8.0-9.el7
NetworkManager-wifi:
1:1.8.0-9.el7
ORBit2:
2.14.19-13.el7
PackageKit:
2.3.8 pkg.owner
列出指定文件是由哪个包提供的
[root@zzl01 ~]# salt '*' pkg.owner /usr/sbin/nginx
zzl02:
nginx
2.3.9 pkg.remove
卸载指定软件
[root@zzl01 ~]# salt '*' pkg.remove wget
zzl02:
----------
wget:
----------
new:
old:
1.14-15.el7
//若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt '*' pkg.upgrade name=openssl
zzl02:
----------
openssl:
----------
new:
1:1.0.2k-16.el7
old:
1:1.0.2k-8.el7
openssl-libs:
----------
new:
1:1.0.2k-16.el7
old:
1:1.0.2k-8.el7
//若想升级系统中所有的软件包则把 name 参数去掉即可
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@zzl01 ~]# salt '*' state.show_highstate
zzl02:
----------
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.install
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10000
nginx-service:
----------
__env__:
dev
__sls__:
web.nginx.service
service:
|_
----------
name:
nginx
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate
执行高级状态
[root@zzl01 ~]# salt '*' state.highstate web.ngnix.install
zzl02:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 18:44:21.843658
Duration: 1020.354 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: The service nginx is already running
Started: 18:44:22.922747
Duration: 29.332 ms
Changes:
Summary for zzl02
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.050 s
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@zzl01 ~]# salt '*' state.show_state_usage
zzl02:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.nginx.install
dev:
----------
count_all:
1
count_unused:
0
count_used:
1
unused:
used:
- web.nginx.service
2.4.4 state.show_top
返回minion将用于highstate的顶级数据
[root@zzl01 ~]# salt '*' state.show_top
zzl02:
----------
base:
- web.nginx.install
dev:
- web.nginx.service
2.4.5 state.top
执行指定的top file,而不是默认的
[root@master ~]# salt '*' state.top runtime.sls
zzl02:
----------
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 zzl02
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 964.617 ms
2.4.6 state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@zzl01 ~]# salt '*' state.show_sls web.nginx.install
zzl02:
----------
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.install
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10000
2.5 SaltStack常用模块之salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@zzl01 ~]# salt '*' cmd.run 'ls /usr/src/'
zzl02:
debug
kernels
[root@zzl01 ~]# salt-cp '*' /etc/passwd /usr/src/
zzl02:
----------
/usr/src/passwd:
True
[root@zzl01 ~]# salt '*' cmd.run 'ls /usr/src/'
zzl02:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@zzl01 ~]# salt-cp '*' /etc/shadow /etc/group /usr/src/
zzl02:
----------
/usr/src/group:
True
/usr/src/shadow:
True
[root@zzl01 ~]# salt '*' cmd.run 'ls /usr/src/'
zzl02:
debug
group
kernels
passwd
shadow
2.6 SaltStack常用模块之file
2.6.1 file.access
检查指定路径是否存在
[root@zzl01 ~]# salt '*' cmd.run 'ls /usr/src'
zzl02:
debug
group
kernels
passwd
shadow
[root@zzl01 ~]# salt '*' file.access /usr/src/passwd f
zzl02:
True
[root@zzl01 ~]# salt '*' file.access /usr/src/abc f
zzl02:
False
检查指定文件的权限信息
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /usr/src'
zzl02:
total 12
drwxr-xr-x. 2 root root 6 Mar 10 2016 debug
-rw-r--r-- 1 root root 464 Aug 18 18:52 group
drwxr-xr-x. 2 root root 6 Mar 10 2016 kernels
-rw-r--r-- 1 root root 886 Aug 18 18:51 passwd
-rw-r--r-- 1 root root 710 Aug 18 18:52 shadow
[root@zzl01 ~]# salt '*' file.access /usr/src/passwd r
zzl02:
True
[root@zzl01 ~]# salt '*' file.access /usr/src/passwd w
zzl02:
True
[root@zzl01 ~]# salt '*' file.access /usr/src/passwd x
zzl02:
False
2.6.2 file.touch
创建文件
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root'
zzl02:
total 16
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
[root@zzl01 ~]# salt '*' file.touch /root/abc
zzl02:
True
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root'
zzl02:
total 16
-rw-r--r-- 1 root root 0 Aug 18 18:58 abc
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
2.6.3 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@zzl01 ~]# salt '*' file.append /root/abc "hello world" "haha" "xixi"
zzl02:
Wrote 3 lines to "/root/abc"
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc'zzl02:
hello world
haha
xixi
2.6.4 file.basename
获取指定路径的基名
[root@zzl01 ~]# salt '*' file.basename '/root/abc/zdf'
zzl02:
zdf
2.6.5 file.dirname
获取指定路径的目录名
[root@zzl01 ~]# salt '*' file.dirname '/root/abc/zdf'
zzl02:
/root/abc
2.6.6 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@zzl01 ~]# salt '*' cmd.run 'md5sum /etc/passwd'
zzl02:
eadf1a339476159e2bc5353759dab0ab /etc/passwd
[root@zzl01 ~]# salt '*' file.check_hash /etc/passwd eadf1a339476159e2bc5353759dab0ab
zzl02:
True
2.6.7 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件,而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改,不能删除、更改、移动 | 任何的进程只能修改目录之下的文件,不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@zzl01 ~]# salt '*' cmd.run 'lsattr /root'
zzl02:
---------------- /root/anaconda-ks.cfg
---------------- /root/initial-setup-ks.cfg
---------------- /root/salt-repo-latest-2.el7.noarch.rpm
---------------- /root/abc
//添加属性
[root@zzl01 ~]# salt '*' file.chattr /root/abc operator=add attributes=ai
zzl02:
True
[root@zzl01 ~]# salt '*' cmd.run 'lsattr /root'
zzl02:
---------------- /root/anaconda-ks.cfg
---------------- /root/initial-setup-ks.cfg
---------------- /root/salt-repo-latest-2.el7.noarch.rpm
----ia---------- /root/abc
给指定文件去除属性
[root@zzl01 ~]# salt '*' file.chattr /root/abc operator=remove attributes=ai
zzl02:
True
[root@zzl01 ~]# salt '*' cmd.run 'lsattr /root'
zzl02:
---------------- /root/anaconda-ks.cfg
---------------- /root/initial-setup-ks.cfg
---------------- /root/salt-repo-latest-2.el7.noarch.rpm
---------------- /root/abc
2.6.8 file.chown
设置指定文件的属主、属组信息
[root@zzl01 ~]# salt '*' file.chown /root/abc tom tom
zzl02:
None
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root/'
zzl02:
total 20
-rw-r--r-- 1 tom tom 22 Aug 18 19:00 abc
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
2.6.9 file.mkdir
创建目录
[root@zzl01 ~]# salt '*' file.mkdir /root/z
zzl02:
True
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root'
zzl02:
total 24
-rw-r--r-- 1 tom tom 22 Aug 18 19:00 abc
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 22 Aug 18 19:20 cc
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
drwxr-xr-x 2 root root 6 Aug 18 19:22 z
2.6.10 file.copy
在远程主机上复制文件或目录
拷贝文件
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root'
zzl02:
total 20
-rw-r--r-- 1 tom tom 22 Aug 18 19:00 abc
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
[root@zzl01 ~]# salt '*' file.copy /root/abc /root/cc
zzl02:
True
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root'
zzl02:
total 24
-rw-r--r-- 1 tom tom 22 Aug 18 19:00 abc
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 22 Aug 18 19:20 cc
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@zzl01 ~]# salt '*' file.copy /root/anaconda-ks.cfg /root/z recurse=True
zzl02:
True
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@zzl01 ~]# salt '*' file.copy /opt/ /root/z recurse=True remove_existing=True
zzl02:
True
2.6.11 file.file_exists
判断指定文件是否存在
[root@zzl01 ~]# salt '*' file.file_exists /root/zbc
zzl02:
False
[root@zzl01 ~]# salt '*' file.file_exists /root/abc
zzl02:
True
2.6.12 file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@zzl01 ~]# salt '*' cmd.run 'sha256sum /root/abc'
zzl02:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/abc
[root@zzl01 ~]# salt '*' file.get_hash /root/abc
zzl02:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b7
2.6.16 file.get_mode
获取指定文件的权限,以数字方式显示
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root/abc'
zzl02:
-rw-r--r-- 1 tom tom 22 Aug 18 19:00 /root/abc
[root@zzl01 ~]# salt '*' file.get_mode /root/abc
zzl02:
0644
2.6.17 file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起
//查找/etc/passwd下的nobody
[root@zzl01 ~]# salt '*' file.grep /etc/passwd nobody
zzl02:
----------
pid:
6679
retcode:
0
stderr:
stdout:
nobody:x:99:99:Nobody:/:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
//查找tom和它的前2行
[root@zzl01 ~]# salt '*' file.grep /etc/passwd tom -- -i -B2
zzl02:
----------
pid:
7200
retcode:
0
stderr:
stdout:
yue:x:1000:1000:yue:/home/yue:/bin/bash
nginx:x:991:986:Nginx web server:/var/lib/nginx:/sbin/nologin
tom:x:1001:1001::/home/tom:/bin/bash
[root@zzl01 ~]# salt '*' file.grep /etc/passwd tom -- -i -l
zzl02:
----------
pid:
7603
retcode:
0
stderr:
stdout:
/etc/passwd
2.6.18 file.move
移动或重命名
//重命名
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root'
zzl02:
total 24
-rw-r--r-- 1 tom tom 22 Aug 18 19:00 abc
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 22 Aug 18 19:20 cc
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
drwxr-xr-x 3 root root 27 Aug 18 19:29 z
[root@zzl01 ~]# salt '*' file.move /root/cc /root/c
zzl02:
----------
comment:
'/root/cc' moved to '/root/c'
result:
True
//移动
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root'
zzl02:
total 24
-rw-r--r-- 1 tom tom 22 Aug 18 19:00 abc
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 22 Aug 18 19:20 c
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
drwxr-xr-x 3 root root 27 Aug 18 19:29 z
[root@zzl01 ~]# salt '*' file.move /root/c /opt/
zzl02:
----------
comment:
'/root/c' moved to '/opt/'
result:
True
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /opt'
zzl02:
total 4
-rw-r--r-- 1 tom tom 22 Aug 18 19:20 c
drwxr-xr-x. 2 root root 6 Mar 9 2015 rh
-rw-r--r-- 1 root root 0 Aug 18 19:29 zbc
2.6.19 file.prepend
把文本插入指定文件的开头
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc'
zzl02:
hello world
haha
xixi
[root@zzl01 ~]# salt '*' file.prepend /root/abc 'xixi' 'hehe'
zzl02:
Prepended 2 lines to "/root/abc"
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc'
zzl02:
xixi
hehe
hello world
haha
xixi
2.6.20 file.sed
修改文本文件的内容
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc'
zzl02:
xixi
hehe
hello world
haha
xixi
[root@zzl01 ~]# salt '*' file.sed /root/abc 'xixi' 'haha'
zzl02:
----------
pid:
20225
retcode:
0
stderr:
stdout:
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc'
zzl02:
haha
hehe
hello world
haha
haha
2.6.21 file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
[root@zzl01 ~]# salt '*' file.remove /root/abc
zzl02:
True
[root@zzl01 ~]# salt '*' file.remove /root/z
zzl02:
True
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root/'
zzl02:
total 20
-rw-r--r-- 1 tom tom 32 Aug 18 21:15 abc.bak
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
2.6.22 file.rename
重命名文件或目录
[root@zzl01 ~]# salt '*' file.rename /root/abc /root/a
zzl02:
True
[root@zzl01 ~]# salt '*' cmd.run 'ls -l /root/'
zzl02:
total 20
-rw-r--r-- 1 root root 0 Aug 19 17:39 a
-rw-r--r-- 1 tom tom 32 Aug 18 21:15 abc.bak
-rw-------. 1 root root 1873 May 3 09:41 anaconda-ks.cfg
-rw-r--r--. 1 root root 1966 May 3 09:42 initial-setup-ks.cfg
-rw-r--r--. 1 root root 5748 Aug 15 13:22 salt-repo-latest-2.el7.noarch.rpm
drwxr-xr-x 2 root root 6 Aug 19 17:40 zz
2.6.23 file.set_mode
给指定文件设置权限
[root@zzl01 ~]# salt '*' file.set_mode /root/a 755
zzl02:
0755
2.6.24 file.get_mode
查看文件权限
ERROR: Minions returned with non-zero exit code
[root@zzl01 ~]# salt '*' file.get_mode /root/a
zzl02:
0644
2.6.25 file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@zzl01 ~]# salt '*' file.uid_to_user 1001
zzl02:
tom
2.6.26 file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@zzl01 ~]# salt '*' file.user_to_uid tom
zzl02:
1001
2.6.27 file.write
往一个指定的文件里覆盖写入指定内容
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc.bak'
zzl02:
haha
hehe
hello world
haha
haha
[root@zzl01 ~]# salt '*' file.write /root/abc.bak hello world
zzl02:
Wrote 2 lines to "/root/abc.bak"
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc.bak'
zzl02:
hello
world
[root@zzl01 ~]# salt '*' file.write /root/abc.bak 'hello world'
zzl02:
Wrote 1 lines to "/root/abc.bak"
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc.bak'
zzl02:
hello world
2.6.28 file.read
读取文件内容
[root@zzl01 ~]# salt '*' cmd.run 'cat /root/abc.bak'
zzl02:
hello world
[root@zzl01 ~]# salt '*' file.read /root/abc.bak
zzl02:
hello world
2.6.29 file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件
[root@zzl01 ~]# salt '*' file.readdir /root
zzl02:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .cache
- .dbus
- initial-setup-ks.cfg
- .xauthqOI2Yp
- .config
- salt-repo-latest-2.el7.noarch.rpm
- .pki
- .bash_history
- .viminfo
- abc.bak
- zz
- a
2.6.30 file.find
类似 find 命令并返回符合指定条件的路径列表
[root@zzl01 ~]# salt '*' file.find / name=a
zzl02:
- /root/a
- /usr/share/terminfo/a
- /var/lib/yum/yumdb/a
[root@zzl01 ~]# salt '*' file.find /root/ name=a delete
zzl02:
- /root/a
[root@zzl01 ~]# salt '*' cmd.run 'ls /root'
zzl02:
abc.bak
anaconda-ks.cfg
initial-setup-ks.cfg
salt-repo-latest-2.el7.noarch.rpm
zz