saltstack的常用模块
文章目录
- 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.11 network.interface
- 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常用模块之user
- 2.6 SaltStack常用模块之salt-cp
- 2.7 SaltStack常用模块之file
- 2.7.1 file.access
- 2.7.2 file.append
- 2.7.3 file.basename
- 2.7.4 file.dirname
- 2.7.5 file.check_hash
- 2.7.6 file.chattr
- 2.7.7 file.chown
- 2.7.8 file.copy
- 2.7.9 file.ditectory_exists
- 2.7.10 file.diskusage
- 2.7.11 file.file_exists
- 2.7.12 file.find
- 2.7.13 file.get_gid
- 2.7.14 file.get_group
- 2.7.15 file.get_hash
- 2.7.16 file.get_mode
- 2.7.17 file.get_selinux_context
- 2.7.18 file.get_sum
- 2.7.19 file.get_uid与file.get_user
- 2.7.20 file.gid_to_group
- 2.7.21 file.group_to_gid
- 2.7.22 file.grep
- 2.7.23 file.is_blkdev
- 2.7.24 file.lsattr
- 2.7.25 file.mkdir
- file.move
- file.prepend
- file.sed
- file.read
- file.readdir
- file.remove
- file.rename
- file.set_mode
- file.symlink
- file.touch
- file.uid_to_user
- file.user_to_uid
- file.write
1. SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
//查看所有的模块列表
[root@master ~]# salt 'node1' sys.list_modules
node1:
- acl
- aliases
- alternatives
- apache
- archive
- artifactory
- baredoc
- beacons
- bigip
- btrfs
- buildout
- chroot
- cloud
- cmd
- composer
- config
- consul
...........此处省略
//查看指定模块的所有function函数
[root@master ~]# salt 'node1' sys.list_functions cmd
node1:
- 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
[root@master ~]#
//查看指定模块的使用文档
[root@master ~]# salt 'node1' 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@master ~]# salt 'node1' test.echo,cmd.run,service.status hello,hostname,salt-minion
node1:
----------
cmd.run:
node1
service.status:
True
test.echo:
hello
[root@master ~]#
2. SaltStack常用模块
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
返回所有活动的tcp连接
[root@master ~]# salt 'node1' network.active_tcp
node1:
----------
0:
----------
local_addr:
192.168.10.202
local_port:
39442
remote_addr:
192.168.10.201
remote_port:
4505
1:
----------
local_addr:
192.168.10.202
local_port:
22
remote_addr:
192.168.10.1
remote_port:
49708
[root@master ~]#
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt 'node1' network.calc_net 192.168.10.201 255.255.255.0
node1:
192.168.10.0/24
[root@master ~]# salt 'node1' network.calc_net 192.168.10.201 255.255.255.240
node1:
192.168.10.192/28
[root@master ~]#
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
必须要指定端口
[root@master ~]# salt 'node1' network.connect 192.168.10.201 22
node1:
----------
comment:
Successfully connected to 192.168.10.201 (192.168.10.201) on tcp port 22
result:
True
[root@master ~]# salt 'node1' network.connect baidu.com 443
node1:
----------
comment:
Successfully connected to baidu.com (220.181.38.251) on tcp port 443
result:
True
[root@master ~]#
2.1.4 network.default_route
查看默认路由
[root@master ~]# salt 'node1' network.default_route
node1:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.10.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@master ~]#
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt 'node1' network.get_fqdn
node1:
node1
[root@master ~]#
2.1.6 network.get_hostname
获取主机名
[root@master ~]# salt 'node1' network.get_hostname
node1:
node1
[root@master ~]#
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt 'node1' network.get_route 192.168.10.201
node1:
----------
destination:
192.168.10.201
gateway:
None
interface:
ens33
source:
192.168.10.202
[root@master ~]#
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt 'node1' network.hw_addr ens33
node1:
00:0c:29:21:00:bb
[root@master ~]#
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt 'node1' network.ifacestartswith 192
node1:
- ens33
[root@master ~]# salt 'node1' network.ifacestartswith 127
node1:
- lo
[root@master ~]#
2.1.11 network.interface
返回指定网卡的信息
[root@master ~]# salt 'node1' network.interface ens33
node1:
|_
----------
address:
192.168.10.202
broadcast:
192.168.10.255
label:
ens33
netmask:
255.255.255.0
[root@master ~]#
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt 'node1' network.interfaces
node1:
----------
ens33:
----------
hwaddr:
00:0c:29:21:00:bb
inet:
|_
----------
address:
192.168.10.202
broadcast:
192.168.10.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:fe21:bb
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
[root@master ~]#
2.1.14 network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1
的地址
[root@master ~]# salt 'node1' network.ip_addrs
node1:
- 192.168.10.202
[root@master ~]#
2.1.15 network.netstat
返回所有打开的端口和状态
[root@master ~]# salt 'node1' network.netstat
node1:
|_
----------
inode:
27453
local-address:
0.0.0.0:22
program:
935/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
30265
local-address:
192.168.10.202:39442
program:
1781/python3.6
proto:
tcp
recv-q:
0
remote-address:
192.168.10.201:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
58589
local-address:
192.168.10.202:22
program:
2579/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.10.1:49708
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
0
local-address:
192.168.10.202:60926
program:
-
proto:
tcp
recv-q:
0
remote-address:
192.168.10.201:4506
send-q:
0
state:
TIME_WAIT
user:
0
|_
----------
inode:
29342
local-address:
:::3306
program:
1193/mysqld
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
992
|_
----------
inode:
27596
local-address:
:::80
program:
930/httpd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
27464
local-address:
:::22
program:
935/sshd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
25806
local-address:
127.0.0.1:323
program:
907/chronyd
proto:
udp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
user:
0
|_
----------
inode:
25807
local-address:
::1:323
program:
907/chronyd
proto:
udp6
recv-q:
0
remote-address:
:::*
send-q:
0
user:
0
[root@master ~]#
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt 'node1' network.ping 192.168.10.201
node1:
PING 192.168.10.201 (192.168.10.201) 56(84) bytes of data.
64 bytes from 192.168.10.201: icmp_seq=1 ttl=64 time=0.270 ms
64 bytes from 192.168.10.201: icmp_seq=2 ttl=64 time=0.482 ms
64 bytes from 192.168.10.201: icmp_seq=3 ttl=64 time=0.253 ms
64 bytes from 192.168.10.201: icmp_seq=4 ttl=64 time=0.524 ms
--- 192.168.10.201 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3084ms
rtt min/avg/max/mdev = 0.253/0.382/0.524/0.122 ms
[root@master ~]#
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt 'node1' network.reverse_ip 192.168.10.100
node1:
100.10.168.192.in-addr.arpa
[root@master ~]#
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用
[root@master ~]# salt 'node1' service.available httpd
node1:
True
[root@master ~]# salt 'node1' service.available nginx
node1:
False
[root@master ~]#
2.2.2 service.get_all
获取所有正在运行的服务
[root@master ~]# salt 'node1' service.get_all
node1:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- boot-complete.target
- chrony-dnssrv@
- chrony-dnssrv@.timer
- chrony-wait
- chronyd
- console-getty
- container-getty@
- cpupower
- crond
- cryptsetup-pre.target
- cryptsetup.target
- ctrl-alt-del.target
- dbus
- dbus-org.freedesktop.hostname1
...............省略
2.2.3 service.disabled
检查指定服务是否开机不自动启动
[root@master ~]# salt 'node1' service.disabled httpd
node1:
False
[root@master ~]#
2.2.4 service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt 'node1' service.enabled httpd
node1:
True
[root@master ~]#
2.2.5 service.disable
设置指定服务开机不自动启动
[root@master ~]# salt 'node1' service.disable httpd
node1:
True
[root@master ~]# salt 'node1' service.enabled httpd
node1:
False
[root@master ~]#
2.2.6 service.enable
设置指定服务开机自动启动
[root@master ~]# salt 'node1' service.enable httpd
node1:
True
[root@master ~]# salt 'node1' service.enabled httpd
node1:
True
[root@master ~]#
2.2.7 service.reload
重新加载指定服务
[root@master ~]# salt 'node1' service.reload httpd
node1:
True
[root@master ~]#
2.2.8 service.stop
停止指定服务
[root@master ~]# salt 'node1' service.stop httpd
node1:
True
[root@master ~]#
2.2.9 service.start
启动指定服务
[root@master ~]# salt 'node1' service.start httpd
node1:
True
[root@master ~]#
2.2.10 service.restart
重启指定服务
[root@master ~]# salt 'node1' service.restart httpd
node1:
True
[root@master ~]#
2.2.11 service.status
查看指定服务的状态
[root@master ~]# salt 'node1' service.status httpd
node1:
True
[root@master ~]# salt 'node1' service.stop httpd
node1:
True
[root@master ~]# salt 'node1' service.status httpd
node1:
False
[root@master ~]#
2.3 SaltStack常用模块之pkg
2.3.1 pkg.install
安装软件
[root@master ~]# salt 'node1' pkg.install wget
node1:
----------
wget:
----------
new:
1.19.5-10.el8
old:
[root@master ~]#
2.3.2 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils
,可以使用 cmd.run 进行远程安装
[root@master ~]# salt 'node1' pkg.download wget
node1:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
[root@master ~]# salt 'node1' cmd.run 'ls /var/cache/yum/packages/'
node1:
wget-1.19.5-10.el8.x86_64.rpm
[root@master ~]#
2.3.3 pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt 'node1' pkg.list_downloaded
node1:
----------
[root@master ~]#
2.3.4 pkg.file_list
列出指定包或系统中已安装的所有包的文件
//列出已安装的apache软件包提供的所有文件
[root@master ~]# salt 'node1' pkg.file_list httpd
node1:
----------
errors:
files:
- /etc/httpd/conf
- /etc/httpd/conf.d/autoindex.conf
- /etc/httpd/conf.d/userdir.conf
- /etc/httpd/conf.d/welcome.conf
- /etc/httpd/conf.modules.d
- /etc/httpd/conf.modules.d/00-base.conf
- /etc/httpd/conf.modules.d/00-dav.conf
- /etc/httpd/conf.modules.d/00-lua.conf
- /etc/httpd/conf.modules.d/00-mpm.conf
- /etc/httpd/conf.modules.d/00-optional.conf
- /etc/httpd/conf.modules.d/00-proxy.conf
- /etc/httpd/conf.modules.d/00-systemd.conf
- /etc/httpd/conf.modules.d/01-cgi.conf
- /etc/httpd/conf.modules.d/README
- /etc/httpd/conf/httpd.conf
.................省略
2.3.5 pkg.group_info
查看包组的信息
[root@master ~]# salt 'node1' pkg.group_info 'Development Tools'
node1:
----------
conditional:
default:
- asciidoc
- byacc
- ctags
- diffstat
- elfutils-libelf-devel
- git
- intltool
- jna
- ltrace
- patchutils
- perl-Fedora-VSP
- perl-Sys-Syslog
- perl-generators
- pesign
- source-highlight
- systemtap
- valgrind
- valgrind-devel
description:
A basic development environment.
group:
Development Tools
id:
None
mandatory:
- autoconf
- automake
- binutils
- bison
- flex
- gcc
- gcc-c++
- gdb
- glibc-devel
- libtool
- make
- pkgconf
- pkgconf-m4
- pkgconf-pkg-config
- redhat-rpm-config
- rpm-build
- rpm-sign
- strace
optional:
- cmake
- expect
- rpmdevtools
- rpmlint
type:
package group
[root@master ~]#
2.3.6 pkg.group_list
列出系统中所有的包组
[root@master ~]# salt 'node1' pkg.group_list
node1:
----------
available:
- Conflicts BaseOS
- Dial-up Networking Support
- Hardware Monitoring Utilities
- Hardware Support
- Large Systems Performance
- Legacy UNIX Compatibility
- Python Web
- Server product core
- Windows File Server
- Additional Development
- Anaconda tools
- Backup Client
- Base
- base-x
- Conflicts AppStream
- Container Management
- Debugging Tools
- Desktop Debugging and Performance Tools
- .NET Core Development
- File and Storage Server
- Fonts
- FTP Server
- GNOME Applications
- GNOME
- Graphical Administration Tools
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Headless Management
- Infiniband Support
- Input Methods
- Internet Applications
- Internet Browser
- Java Platform
- Legacy X Window System Compatibility
- Mail Server
- Mainframe Access
- Multimedia
- Network File System Client
- Network Servers
- Networking Tools
- Common NetworkManager submodules
- Office Suite and Productivity
- Atomic Host ostree support
- Performance Tools
- Platform Development
- KVM platform specific packages
- Hyper-v platform specific packages
- Printing Client
- Remote Desktop Clients
- Remote Management for Linux
- RPM Development Tools
- Scientific Support
- Security Tools
- Smart Card Support
- Standard
- System Tools
- TeX formatting system
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Basic Web Server
- Workstation product core
available environments:
- Server with GUI
- Server
- Workstation
- Custom Operating System
- Virtualization Host
available languages:
----------
installed:
- Core
- Development Tools
- VMware platform specific packages
installed environments:
- Minimal Install
[root@master ~]#
2.3.7 pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt 'node1' pkg.list_pkgs
node1:
----------
NetworkManager:
1:1.30.0-0.3.el8
NetworkManager-libnm:
1:1.30.0-0.3.el8
NetworkManager-team:
1:1.30.0-0.3.el8
NetworkManager-tui:
1:1.30.0-0.3.el8
abattis-cantarell-fonts:
0.0.25-4.el8
acl:
2.2.53-1.el8
adwaita-cursor-theme:
3.28.0-2.el8
adwaita-icon-theme:
3.28.0-2.el8
apr:
1.6.3-11.el8
apr-util:
1.6.1-6.el8
apr-util-bdb:
1.6.1-6.el8
apr-util-openssl:
1.6.1-6.el8
at-spi2-atk:
2.26.2-1.el8
...........省略
2.3.8 pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt 'node1' pkg.owner /usr/sbin/apachectl
node1:
httpd
[root@master ~]# salt 'node1' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
node1:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
[root@master ~]#
2.3.9 pkg.remove
卸载指定软件
[root@master ~]# salt 'node1' cmd.run 'rpm -aq|grep wget'
node1:
wget-1.19.5-10.el8.x86_64
[root@master ~]# salt 'node1' pkg.remove wget
node1:
----------
wget:
----------
new:
old:
1.19.5-10.el8
[root@master ~]#
//若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
//若想升级系统中所有的软件包则把 name 参数去掉即可
[root@master ~]# salt 'node1' pkg.upgrade name=openssl
node1:
----------
openssl:
----------
new:
1:1.1.1k-4.el8
old:
1:1.1.1g-11.el8
openssl-libs:
----------
new:
1:1.1.1k-4.el8
old:
1:1.1.1g-11.el8
//升级所有
[root@master ~]# salt 'node1' pkg.upgrade
node1:
----------
NetworkManager:
----------
new:
1:1.30.0-10.el8_4
old:
1:1.30.0-0.3.el8
NetworkManager-libnm:
----------
new:
1:1.30.0-10.el8_4
old:
1:1.30.0-0.3.el8
NetworkManager-team:
----------
new:
1:1.30.0-10.el8_4
old:
1:1.30.0-0.3.el8
NetworkManager-tui:
----------
new:
1:1.30.0-10.el8_4
old:
1:1.30.0-0.3.el8
abattis-cantarell-fonts:
----------
new:
0.0.25-6.el8
old:
0.0.25-4.el8
.............省略
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt 'node1' state.show_highstate
node1:
----------
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.nginx
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10000
nginx-service:
----------
__env__:
base
__sls__:
web.nginx.nginx
service:
|_
----------
name:
nginx
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate
执行高级状态
[root@master ~]# salt 'node1' state.highstate web.nginx.nginx
node1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: None
Comment: The following packages would be installed/updated: nginx
Started: 04:11:38.008551
Duration: 1031.626 ms
Changes:
----------
installed:
----------
nginx:
----------
new:
installed
old:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: None
Comment: Service nginx not present; if created in this state run, it would have been started
Started: 04:11:39.065791
Duration: 33.262 ms
Changes:
Summary for node1
------------
Succeeded: 2 (unchanged=2, changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 1.065 s
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt 'node1' state.show_state_usage
node1:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.nginx.nginx
dev:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
prod:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
test:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
2.4.4 state.show_top
返回node1将用于highstate的*数据
[root@master ~]# salt 'node1' state.show_top
node1:
----------
base:
- web.nginx.nginx
2.4.5 state.top
执行指定的top file,而不是默认的
[root@master ~]# salt 'node1' state.top top.sls
node1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: The following packages were installed/updated: nginx
Started: 04:21:12.353629
Duration: 17337.336 ms
Changes:
----------
gd:
----------
new:
2.2.5-7.el8
old:
libXpm:
----------
new:
3.5.12-8.el8
old:
libwebp:
----------
new:
1.0.0-5.el8
old:
nginx:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-all-modules:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-filesystem:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-http-image-filter:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-http-perl:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-http-xslt-filter:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-mail:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-stream:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx has been enabled, and is running
Started: 04:21:29.710267
Duration: 363.483 ms
Changes:
----------
nginx:
True
Summary for node1
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 17.701 s
2.4.6 state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt 'node1' state.show_sls web.nginx.nginx
node1:
----------
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.nginx
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10000
nginx-service:
----------
__env__:
base
__sls__:
web.nginx.nginx
service:
|_
----------
name:
nginx
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.5 SaltStack常用模块之user
2.5.1 user.add
user.add:在node1端上创建一个用户。
用法:salt ‘*’ user.add name
[root@master ~]# salt 'node1' user.add tom
node1:
True
[root@master ~]#
2.5.2 user.info
user.info:返回用户信息。
[root@master ~]# salt 'node1' user.info tom
node1:
----------
fullname:
gid:
1001
groups:
- tom
home:
/home/tom
homephone:
name:
tom
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1001
workphone:
[root@master ~]#
2.5.3 user.getent
user.getent:返回所有系统用户信息的列表
[root@master ~]# salt 'node1' user.getent
node1:
|_
----------
fullname:
root
gid:
0
groups:
- root
home:
/root
homephone:
name:
root
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
0
workphone:
|_
----------
...................省略
2.5.4 user.list_groups
user.list_groups:列出指定用户所属组的列表。
[root@master ~]# salt 'node1' user.list_groups tom
node1:
- tom
[root@master ~]#
2.5.5 user.rename
user.rename:修改指定用户的用户名
[root@master ~]# salt 'node1' user.rename tom jerry
node1:
False //虽然返回False但是操作是成功完成了的
[root@master ~]# salt 'node1' user.info jerry
node1:
----------
fullname:
gid:
1001
groups:
- tom
home:
/home/tom
homephone:
name:
jerry
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1001
workphone:
[root@master ~]#
2.5.6 user.delete
user.delete:在minion端删除一个用户
[root@master ~]# salt 'node1' user.delete jerry
node1:
True
[root@master ~]# salt 'node1' user.info jerry
node1:
----------
[root@master ~]#
2.6 SaltStack常用模块之salt-cp
salt-cp
能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
debug
kernels
[root@master ~]# salt-cp 'node1' /etc/passwd /usr/src/
node1:
----------
/usr/src/passwd:
True
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
debug
kernels
passwd
[root@master ~]#
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp 'node1' /etc/shadow /etc/group /usr/src
node1:
----------
/usr/src/group:
True
/usr/src/shadow:
True
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
debug
group
kernels
passwd
shadow
[root@master ~]#
2.7 SaltStack常用模块之file
2.7.1 file.access
检查指定路径是否存在
[root@master ~]# salt 'node1' file.access /usr/src/passwd f
node1:
True
[root@master ~]# salt 'node1' file.access /usr/src/tom f
node1:
False
[root@master ~]#
检查指定文件的权限信息
[root@master ~]# salt 'node1' cmd.run 'ls -l /usr/src'
node1:
total 12
drwxr-xr-x. 2 root root 6 May 18 2020 debug
-rw-r--r-- 1 root root 524 Nov 4 03:38 group
drwxr-xr-x. 2 root root 6 May 18 2020 kernels
-rw-r--r-- 1 root root 1155 Nov 4 03:37 passwd
-rw-r--r-- 1 root root 800 Nov 4 03:38 shadow
[root@master ~]# salt 'node1' file.access /usr/src/passwd r //是否有读权限
node1:
True
[root@master ~]# salt 'node1' file.access /usr/src/passwd w //是否有写权限
node1:
True
[root@master ~]# salt 'node1' file.access /usr/src/passwd x //是否有执行权限
node1:
False
[root@master ~]#
2.7.2 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/test'
node1:
-rw-r--r-- 1 root root 0 Nov 4 03:42 /root/test
//添加多行
[root@master ~]# salt 'node1' file.append /root/test "nohao" "tom" "jerry"
node1:
Wrote 3 lines to "/root/test"
[root@master ~]# salt 'node1' cmd.run 'cat /root/test'
node1:
nohao
tom
jerry
//添加单行
[root@master ~]# salt 'node1' file.append /root/test "nohao tom jerry"
node1:
Wrote 1 lines to "/root/test"
[root@master ~]# salt 'node1' cmd.run 'cat /root/test'
node1:
nohao
tom
jerry
nohao tom jerry
[root@master ~]#
2.7.3 file.basename
获取指定路径的基名
[root@master ~]# salt 'node1' file.basename '/root/abc/test'
node1:
test
[root@master ~]#
2.7.4 file.dirname
获取指定路径的目录名
[root@master ~]# salt 'node1' file.dirname '/root/abc/test'
node1:
/root/abc
[root@master ~]#
2.7.5 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt 'node1' cmd.run 'md5sum /etc/passwd'
node1:
9e90a725a51cc5954da67e36e26c2d19 /etc/passwd
[root@master ~]# salt 'node1' file.check_hash /etc/passwd 9e90a725a51cc5954da67e36e26c2d19
node1:
True
[root@master ~]#
2.7.6 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件, 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改, 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件, 不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/test
//添加属性
[root@master ~]# salt 'node1' file.chattr /root/test operator=add attributes=ai
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
----ia-------------- /root/test
[root@master ~]#
给指定文件去除属性
[root@master ~]# salt 'node1' file.chattr /root/test operator=remove attributes=a
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
----i--------------- /root/test
[root@master ~]#
2.7.7 file.chown
设置指定文件的属主、属组信息
root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1181 Jul 17 10:59 anaconda-ks.cfg
-rw-r--r-- 1 root root 32 Nov 4 03:44 test
[root@master ~]# salt 'node1' file.chown /root/test tom tom
node1:
None
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1181 Jul 17 10:59 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 32 Nov 4 03:44 test
[root@master ~]#
2.7.8 file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt 'node1' file.copy /root/test /root/test-2
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-------. 1 root root 1181 Jul 17 10:59 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 32 Nov 4 03:44 test
-rw-r--r-- 1 tom tom 32 Nov 4 04:00 test-2
[root@master ~]
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1181 Jul 17 10:59 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 32 Nov 4 03:44 test
drwxr-xr-x 2 root root 6 Nov 4 04:07 testdir
[root@master ~]# salt 'node1' cmd.run 'ls /root/testdir'
node1:
[root@master ~]# salt 'node1' file.copy /tmp /root/testdir recurse=true
node1:
ERROR: Could not copy '/tmp' to '/root/testdir'
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt 'node1' cmd.run 'ls /root/testdir'
node1:
mysql.sock.lock
[root@master ~]#
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt node1 cmd.run 'ls -l /opt/test'
node1:
total 0
-rw-r--r--. 1 root root 0 Nov 4 03:16 12
-rw-r--r--. 1 root root 0 Nov 4 03:16 dei
-rw-r--r--. 1 root root 0 Nov 4 03:16 deidq
-rw-r--r--. 1 root root 0 Nov 4 03:16 olp
[root@master ~]# salt node1 cmd.run 'ls -l /etc/test2'
node1:
total 0
-rw-r--r--. 1 root root 0 Nov 4 03:17 123
-rw-r--r--. 1 root root 0 Nov 4 03:17 456
[root@master ~]# salt node1 file.copy /opt/test /etc/test2 recurse=true remove_existing=true
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /etc/test2'node1:
total 0
-rw-r--r--. 1 root root 0 Nov 4 03:16 12
-rw-r--r--. 1 root root 0 Nov 4 03:16 dei
-rw-r--r--. 1 root root 0 Nov 4 03:16 deidq
-rw-r--r--. 1 root root 0 Nov 4 03:16 olp
2.7.9 file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 64
-rw-r--r--. 1 root root 11 Nov 4 03:11 luo
drwxr-xr-x. 2 root root 51 Nov 4 03:16 luochuran
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
[root@master ~]# salt node1 file.directory_exists /opt/lcr
node1:
False
2.7.10 file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt node1 cmd.run 'du -sb /opt/'
node1:
54171 /opt/
[root@master ~]# salt node1 file.diskusage /opt
node1:
54049
2.7.11 file.file_exists
判断指定文件是否存在
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 64
-rw-r--r--. 1 root root 11 Nov 4 03:11 luo
drwxr-xr-x. 2 root root 51 Nov 4 03:16 luochuran
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
[root@master ~]# salt node1 file.file_exists /opt/lcr
node1:
True
[root@master ~]# salt node1 file.file_exists /opt/luochuran
node1:
False ##返回False是因为abc是目录而非文件
2.7.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(匹配a, b或c)
[!abc] or [^abc] = match anything except a, b, and c(匹配除a, b, c以外的任何东西)
[x-y] = match chars x through y(匹配字符x到y)
[!x-y] or [^x-y] = match anything except chars x through y(匹配任何字符,除了字符x到y)
{a,b,c} = match a or b or c(匹配a或b或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(文件内容的md5摘要)
mode: file permissions (as integer) (文件权限(以整数形式))
mtime: last modification time (as time_t) (最后一次修改时间(作为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 (匹配文件后缀为.bak且大于10M的文件)
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime (匹配在30天前修改的文件且大于10M的文件的路径和大小以及最后一次修改的时间)
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete (匹配文件后缀名为数字结尾,且在30天前修改的并且大于10M的文件,并将它们删除)
2.7.13 file.get_gid
获取指定文件的gid
[root@master ~]# salt node1 cmd.run 'ls -l /opt/lcr'
node1:
-rw-r--r--. 1 root root 11 Nov 4 02:54 /opt/lcr
[root@master ~]# salt node1 file.get_gid /opt/lcr
node1:
0
2.7.14 file.get_group
获取指定文件的组名
[root@master ~]# salt node1 cmd.run 'ls -l /opt/lcr'
node1:
-rw-r--r--. 1 root root 11 Nov 4 02:54 /opt/lcr
[root@master ~]# salt node1 file.get_group /opt/lcr
node1:
root
2.7.15 file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt node1 cmd.run 'sha256sum /opt/lcr'
node1:
487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62 /opt/lcr
[root@master ~]# salt node1 file.get_hash /opt/lcr
node1:
487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62
2.7.16 file.get_mode
获取指定文件的权限,以数字方式显示
[root@master ~]# salt node1 cmd.run 'ls -l /opt/lcr'
node1:
-rw-r--r--. 1 root root 11 Nov 4 02:54 /opt/lcr
[root@master ~]# salt node1 file.get_mode /opt/lcr
node1:
0644
2.7.17 file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt node1 cmd.run 'ls -Z /opt/lcr'
node1:
unconfined_u:object_r:usr_t:s0 /opt/lcr
[root@master ~]# salt node1 file.get_selinux_context /opt/lcr
node1:
unconfined_u:object_r:usr_t:s0
2.7.18 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt node1 cmd.run 'sha256sum /opt/lcr'
node1:
487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62 /opt/lcr
[root@master ~]# salt node1 file.get_sum /opt/lcr
node1:
487aca02c302a0ac061a3a2cf837e68d006169c3a9501af8b9c56d21ef235a62
[root@master ~]# salt node1 cmd.run 'md5sum /opt/lcr'
node1:
861b478acdd37e1a909646b04e82290b /opt/lcr
[root@master ~]# salt node1 file.get_sum /opt/lcr md5
node1:
861b478acdd37e1a909646b04e82290b
2.7.19 file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
[root@master ~]# salt node1 cmd.run 'ls -l /opt/lcr'
node1:
-rw-r--r--. 1 root root 11 Nov 4 02:54 /opt/lcr
[root@master ~]# salt node1 file.get_uid /opt/lcr
node1:
0
[root@master ~]# salt node1 file.get_user /opt/lcr
node1:
root
2.7.20 file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt node1 file.gid_to_group 1000
node1:
luo
[root@master ~]# salt node1 file.gid_to_group 1001
node1:
lcr
[root@master ~]# salt node1 file.gid_to_group 0
node1:
root
2.7.21 file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt node1 file.group_to_gid liu
node1:
1000
[root@master ~]# salt node1 file.group_to_gid root
node1:
0
2.7.22 file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@master ~]# salt node1 file.grep /opt/luo service
node1:
----------
pid:
176727
retcode:
0
stderr:
stdout:
service
[root@master ~]# salt node1 file.grep /opt/luo service -- -i
node1:
----------
pid:
177132
retcode:
0
stderr:
stdout:
service
[root@master ~]# salt node1 file.grep /opt/luo service -- -i -B2
node1:
----------
pid:
177707
retcode:
0
stderr:
stdout:
best
master
service
[root@master ~]# salt node1 file.grep /opt/luo service -- -i -l
node1:
----------
pid:
177997
retcode:
0
stderr:
stdout:
/opt/liu
grep参数:
- -a 或 --text : 不要忽略二进制的数据。
- -A<显示行数> 或 --after-context=<显示行数> : 除了显示符合范本样式的那一列之外,并显示该行之后的内容。
- -b 或 --byte-offset : 在显示符合样式的那一行之前,标示出该行第一个字符的编号。
- -B<显示行数> 或 --before-context=<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前的内容。
- -c 或 --count : 计算符合样式的列数。
- -C<显示行数> 或 --context=<显示行数>或-<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前后的内容。
- -d <动作> 或 --directories=<动作> : 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
- -e<范本样式> 或 --regexp=<范本样式> : 指定字符串做为查找文件内容的样式。
- -E 或 --extended-regexp : 将样式为延伸的正则表达式来使用。
- -f<规则文件> 或 --file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。
- -F 或 --fixed-regexp : 将样式视为固定字符串的列表。
- -G 或 --basic-regexp : 将样式视为普通的表示法来使用。
- -h 或 --no-filename : 在显示符合样式的那一行之前,不标示该行所属的文件名称。
- -H 或 --with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。
- -i 或 --ignore-case : 忽略字符大小写的差别。
- -l 或 --file-with-matches : 列出文件内容符合指定的样式的文件名称。
- -L 或 --files-without-match : 列出文件内容不符合指定的样式的文件名称。
- -n 或 --line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。
- -o 或 --only-matching : 只显示匹配PATTERN 部分。
- -q 或 --quiet或–silent : 不显示任何信息。
- -r 或 --recursive : 此参数的效果和指定"-d recurse"参数相同。
- -s 或 --no-messages : 不显示错误信息。
- -v 或 --invert-match : 显示不包含匹配文本的所有行。
- -V 或 --version : 显示版本信息。
- -w 或 --word-regexp : 只显示全字符合的列。
- -x --line-regexp : 只显示全列符合的列。
- -y : 此参数的效果和指定"-i"参数相同。
2.7.23 file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt node1 cmd.run 'ls -l /dev/sda'
node1:
brw-rw----. 1 root disk 8, 0 Nov 4 02:17 /dev/sda
[root@master ~]# salt node1 file.is_blkdev /dev/sda
node1:
True
2.7.24 file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt node1 cmd.run 'lsattr /opt/lcr'
node1:
-------------------- /opt/lq
[root@master ~]# salt node1 cmd.run 'chattr +i /opt/lcr'
node1:
[root@master ~]# salt node1 file.lsattr /opt/lcr
node1:
----------
/opt/lcr:
- i
2.7.25 file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 60
-rw-------. 1 root root 1023 Jul 16 07:36 anaconda-ks.cfg
-rw-r--r--. 1 root root 7 Nov 4 02:36 luo.sh
[root@master ~]# salt node1 file.mkdir /root/test
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root/test'
node1:
total 0
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 60
-rw-------. 1 root root 1023 Jul 16 07:36 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Nov 4 04:09 test
-rw-r--r--. 1 root root 7 Nov 4 02:36 liu.sh
[root@master ~]# salt node1 file.mkdir /root/test2 lcr lcr 250
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 60
-rw-------. 1 root root 1023 Jul 16 07:36 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Nov 4 04:09 test
-rw-r--r--. 1 root root 7 Nov 4 02:36 liu.sh
d-w-r-x---. 2 lq lq 6 Nov 4 04:10 test2
file.move
移动或重命名
[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
total 64
-rw-r--r--. 1 root root 26 Nov 4 03:57 luo
drwxr-xr-x. 2 root root 51 Nov 4 03:16 luochuran
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
[root@master ~]# salt node1 file.move /opt/liu /mnt/test
node1:
----------
comment:
'/opt/liu' moved to '/mnt/test'
result:
True
[root@master ~]# salt node1 cmd.run 'ls -l /mnt/test'
node1:
-rw-r--r--. 1 root root 26 Nov 4 03:57 /mnt/test
file.prepend
把文本插入指定文件的开头
[root@master ~]# salt node1 cmd.run 'cat /opt/lcr'
node1:
me
is
best
[root@master ~]# salt node1 cmd.run 'cat /opt/luo'
node1:
me
is
best
[root@master ~]# salt node1 file.prepend /opt/luo "dd" "meme"
node1:
Prepended 2 lines to "/opt/luo"
[root@master ~]# salt node1 cmd.run 'cat /opt/luo'node1:
dd
meme
me
is
best
file.sed
修改文本文件的内容
[root@master ~]# salt node1 cmd.run 'cat /opt/luo'node1:
dd
meme
me
is
best
[root@master ~]# salt node1 file.sed /opt/luo "dd" "gege"node1:
----------
pid:
359178
retcode:
0
stderr:
stdout:
[root@master ~]# salt node1 cmd.run 'cat /opt/luo'
node1:
gege
meme
me
is
best
file.read
读取文件内容
[root@master ~]# salt node1 cmd.run 'cat /opt/luo'
node1:
gege
meme
me
is
best
[root@master ~]# salt node1 file.read /opt/luo
node1:
gege
meme
me
is
best
file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt node1 file.readdir /root
node1:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .bash_history
- .config
- !
- luo.sh
- best
- you
- .mysql_history
- 1
- .viminfo
file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 68
-rw-r--r--. 1 root root 22 Nov 7 02:03 luo
drwxr-xr-x. 2 root root 51 Nov 4 03:16 luochuran
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
[root@master ~]# salt node1 file.remove '/opt/luochuran'
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 68
-rw-r--r--. 1 root root 22 Nov 7 02:03 luo
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
file.rename
重命名文件或目录
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 68
-rw-r--r--. 1 root root 22 Nov 7 02:03 luo
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
[root@master ~]# salt node1 file.rename /opt/liu /opt/ran
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 68
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
-rw-r--r--. 1 root root 22 Nov 7 02:03 ran
file.set_mode
给指定文件设置权限
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 68
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
-rw-r--r--. 1 root root 22 Nov 7 02:03 ran
[root@master ~]# salt node1 file.set_mode /opt/ran 0777
node1:
0777
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 68
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
-rwxrwxrwx. 1 root root 22 Nov 7 02:03 ran
file.symlink
给指定的文件创建软链接
[root@master ~]# salt node1 file.symlink /opt/lq /mnt/ltest
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/;ls -l /mnt'
node1:
total 68
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
-rwxrwxrwx. 1 root root 22 Nov 7 02:03 ran
total 8
drwxr-xr-x. 2 root root 6 Jul 16 07:33 hgfs
lrwxrwxrwx. 1 root root 7 Nov 7 02:11 test -> /opt/lq
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
file.touch
创建空文件或更新时间戳
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 68
-rw-r--r--. 1 root root 11 Nov 4 02:54 lcr
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
-rwxrwxrwx. 1 root root 22 Nov 7 02:03 ran
[root@master ~]# salt node1 file.touch /opt/test
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /opt/'
node1:
total 68
-rw-r--r--. 1 root root 11 Nov 4 02:54 lq
-rw-r--r--. 1 root root 0 Nov 7 02:13 test
-rw-r-----. 1 root root 52909 Nov 2 03:00 master
-rw-r--r--. 1 root root 1118 Nov 4 02:39 passwd
-rwxrwxrwx. 1 root root 22 Nov 7 02:03 ran
file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@master ~]# salt node1 file.uid_to_user 0
node1:
root
[root@master ~]# salt node1 file.uid_to_user 1000
node1:
tom
file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@master ~]# salt node1 file.user_to_uid tom
node1:
1000
file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt node1 cmd.run 'cat /opt/test'
node1:
hello
[root@master ~]# salt 'node1' file.write /opt/test "hello" "haha" "xixi"
node1:
Wrote 3 lines to "/opt/liu"
[root@master ~]# salt node1 cmd.run 'cat /opt/test'node1:
hello
haha
xixi