概述
SaltStack常用的模块
1. SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有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
- container_resource
- cp
- cron
- cryptdev
- data
- defaults
- devinfo
- devmap
- disk
..........................
//查看指定module的所有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
//查看指定module的用法
[root@master ~]# salt 'node1' sys.doc service
service.available:
New in version 0.10.4
Check that the given service is available taking into account template
units.
CLI Example:
salt '*' service.available sshd
//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
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.72.139
local_port:
22
remote_addr:
192.168.72.1
remote_port:
56504
1:
----------
local_addr:
192.168.72.139
local_port:
22
remote_addr:
192.168.72.1
remote_port:
56502
2:
----------
local_addr:
192.168.72.139
local_port:
58618
remote_addr:
192.168.72.141
remote_port:
4505
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt 'node1' network.calc_net 192.168.72.139 255.255.255.0
node1:
192.168.72.0/24
[root@master ~]# salt 'node1' network.calc_net 192.168.72.139 255.255.255.240
node1:
192.168.72.128/28
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt 'node1' network.connect lol.qq.com 80
node1:
----------
comment:
Successfully connected to lol.qq.com (211.162.179.144) on tcp port 80
result:
True
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.72.2
interface:
ens33
netmask:
0.0.0.0
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt 'node1' network.get_fqdn
node1:
node1
2.1.6 network.get_hostname
获取主机名
[root@master ~]# salt 'node1' network.get_hostname
node1:
node1
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt 'node1' network.get_route 192.168.70.240
node1:
----------
destination:
192.168.70.240
gateway:
192.168.72.2
interface:
ens33
source:
192.168.72.139
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt 'node1' network.hw_addr ens33
node1:
00:0c:29:4c:c0:b2
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt 'node1' network.ifacestartswith 192.168
node1:
- ens33
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt 'node1' network.in_subnet 192.168.72.0/24
node1:
True
2.1.11 network.interface
返回指定网卡的信息
[root@master ~]# salt 'node1' network.interface ens33
node1:
|_
----------
address:
192.168.72.139
broadcast:
192.168.72.255
label:
ens33
netmask:
255.255.255.0
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt 'node1' network.interface_ip ens33
node1:
192.168.72.139
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt 'node1' network.interfaces
node1:
----------
ens33:
----------
hwaddr:
00:0c:29:4c:c0:b2
inet:
|_
----------
address:
192.168.72.139
broadcast:
192.168.72.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::8c5a:fae4:e5a7:a2b7
prefixlen:
64
scope:
link
up:
True
lo:
----------
hwaddr:
00:00:00:00:00:00
inet:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
----------
address:
::1
prefixlen:
128
scope:
host
up:
True
2.1.14 network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址
[root@master ~]# salt 'node1' network.ip_addrs
node1:
- 192.168.72.139
2.1.15 network.netstat
返回所有打开的端口和状态
[root@master ~]# salt 'node1' network.netstat
node1:
|_
----------
inode:
25399
local-address:
127.0.0.1:323
program:
chronyd
proto:
udp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
user:
0
|_
----------
inode:
146716
local-address:
192.168.72.139%ens33:68
program:
NetworkManager
proto:
udp
recv-q:
0
remote-address:
192.168.72.254:67
send-q:
0
user:
0
|_
----------
inode:
25400
local-address:
[::1]:323
program:
chronyd
proto:
udp
recv-q:
0
remote-address:
[::]:*
send-q:
0
user:
0
|_
----------
inode:
28594
local-address:
0.0.0.0:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
150157
local-address:
192.168.72.139:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.72.1:56504
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
0
local-address:
192.168.72.139:39688
program:
proto:
tcp
recv-q:
0
remote-address:
192.168.72.141:4506
send-q:
0
state:
TIME-WAIT
user:
0
|_
----------
inode:
150018
local-address:
192.168.72.139:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.72.1:56502
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
49256
local-address:
192.168.72.139:58618
program:
salt-minion
proto:
tcp
recv-q:
0
remote-address:
192.168.72.141:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
28596
local-address:
[::]:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
[::]:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
30729
local-address:
*:3306
program:
mysqld
proto:
tcp
recv-q:
0
remote-address:
*:*
send-q:
80
state:
LISTEN
user:
993
|_
----------
inode:
74372
local-address:
*:80
program:
httpd
proto:
tcp
recv-q:
0
remote-address:
*:*
send-q:
128
state:
LISTEN
user:
0
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt 'node1' network.ping lol.qq.com
node1:
PING lol.qq.com.x2.sched.dcloudstc.com (211.162.179.143) 56(84) bytes of data.
64 bytes from 211.162.179.143 (211.162.179.143): icmp_seq=1 ttl=128 time=194 ms
64 bytes from 211.162.179.143 (211.162.179.143): icmp_seq=2 ttl=128 time=40.0 ms
64 bytes from 211.162.179.143 (211.162.179.143): icmp_seq=3 ttl=128 time=28.10 ms
64 bytes from 211.162.179.143 (211.162.179.143): icmp_seq=4 ttl=128 time=32.1 ms
--- lol.qq.com.x2.sched.dcloudstc.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 28.976/73.818/194.202/69.619 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt 'node1' network.reverse_ip 192.168.72.77
node1:
77.72.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用
[root@master ~]# salt 'node1' service.available sshd
node1:
True
[root@master ~]# salt 'node1' service.available httpd
node1:
True
2.2.2 service.get_all
获取所有正在运行的服务
[root@master ~]# salt 'node1' service.get_all
node1:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- boot-complete.target
- chrony-dnssrv@
- chrony-dnssrv@.timer
- chrony-wait
- chronyd
..................
2.2.3 service.disabled
检查指定服务是否开机不自动启动
[root@master ~]# salt 'node1' service.disabled httpd
node1:
False
2.2.4 service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt 'node1' service.enabled httpd
node1:
True
2.2.5 service.disable
设置指定服务开机不自动启动
[root@master ~]# salt 'node1' service.disable httpd
node1:
True
[root@master ~]# salt 'node1' service.enabled httpd
node1:
False
2.2.6 service.enable
设置指定服务开机自动启动
[root@master ~]# salt 'node1' service.enable httpd
node1:
True
[root@master ~]# salt 'node1' service.enabled httpd
node1:
True
2.2.7 service.reload
重新加载指定服务
[root@master ~]# salt 'node1' service.reload httpd
node1:
True
2.2.8 service.stop
停止指定服务
[root@master ~]# salt 'node1' service.stop httpd
node1:
True
[root@master ~]# salt 'node1' service.status httpd
node1:
False
2.2.9 service.start
启动指定服务
[root@master ~]# salt 'node1' service.start httpd
node1:
True
[root@master ~]# salt 'node1' service.status httpd
node1:
True
2.2.10 service.restart
重启指定服务
[root@master ~]# salt 'node1' service.restart httpd
node1:
True
2.2.11 service.status
查看指定服务的状态
[root@master ~]# salt 'node1' service.status httpd
node1:
True
2.3 SaltStack常用模块之pkg
2.3.1 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@node1 ~]#
ls /var/cache/yum/packages/
wget-1.19.5-10.el8.x86_64.rpm
2.3.2 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
......................
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt 'node1' pkg.file_list
node1:
----------
errors:
files:
VALUE_TRIMMED
2.3.3 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
2.3.4 pkg.group_list
列出系统中所有的包组
[root@master ~]# salt 'node1' pkg.group_list
node1:
----------
available:
- Backup Client
- base-x
- Conflicts AppStream
- Container Management
- Debugging Tools
- Desktop Debugging and Performance Tools
- .NET Core Development
- FTP Server
- GNOME Applications
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Input Methods
- Internet Applications
- Internet Browser
- Java Platform
- Legacy X Window System Compatibility
- Multimedia
- Office Suite and Productivity
- Atomic Host ostree support
- KVM platform specific packages
- Hyper-v platform specific packages
- Printing Client
- Remote Desktop Clients
- RPM Development Tools
- TeX formatting system
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Basic Web Server
- Additional Development
- Anaconda tools
- Base
- Conflicts BaseOS
- Dial-up Networking Support
- File and Storage Server
- Fonts
- GNOME
- Graphical Administration Tools
- Hardware Monitoring Utilities
- Hardware Support
- Headless Management
- Infiniband Support
- Large Systems Performance
- Legacy UNIX Compatibility
- Mail Server
- Mainframe Access
- Network File System Client
- Network Servers
- Networking Tools
- Common NetworkManager submodules
- Performance Tools
- Platform Development
- Python Web
- Remote Management for Linux
- Scientific Support
- Security Tools
- Server product core
- Smart Card Support
- Windows File Server
- Standard
- System Tools
- Workstation product core
- Critical Path (KDE)
- Fedora Packager
- Firefox Web Browser
- KDE Applications
- KDE
- KDE Educational applications
- KDE Multimedia support
- KDE Office
- KDE Software Development
- KDE Frameworks 5 Software Development
- Xfce
available environments:
- Server with GUI
- Server
- Workstation
- KDE Plasma Workspaces
- Virtualization Host
- Custom Operating System
available languages:
----------
installed:
- VMware platform specific packages
- Core
- Development Tools
installed environments:
- Minimal Install
2.3.5 pkg.install
安装软件
[root@master ~]# salt 'node1' pkg.install wget
node1:
----------
wget:
----------
new:
1.19.5-10.el8
old:
2.3.6 pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt 'node1' pkg.list_downloaded
node1:
----------
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-12.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
at-spi2-core:
2.28.0-1.el8
atk:
2.28.1-1.el8
audit:
3.0-0.17.20191104git1c2f876.el8
audit-libs:
3.0-0.17.20191104git1c2f876.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
2.3.9 pkg.remove
卸载指定软件
[root@master ~]# salt 'node1' pkg.remove wget
node1:
----------
wget:
----------
new:
old:
1.19.5-10.el8
//若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt 'node1' pkg.upgrade name=openssl
node1:
----------
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt 'node1' state.show_highstate
node1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.install
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate
执行高级状态
[root@master ~]# salt 'node1' state.highstate web.apache.install
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 01:24:25.293139
Duration: 789.669 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 01:24:26.089202
Duration: 42.269 ms
Changes:
Summary for node1
------------
Succeeded: 2
Failed:
0
------------
Total states run:
2
Total run time: 831.938 ms
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.apache.install
test:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
2.4.4 state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt 'node1' state.show_top
node1:
----------
base:
- web.apache.install
2.4.5 state.top
执行指定的top file,而不是默认的
[root@master ~]# salt 'node1' state.top top.sls
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 01:26:38.170942
Duration: 760.652 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 01:26:38.933585
Duration: 40.438 ms
Changes:
Summary for node1
------------
Succeeded: 2
Failed:
0
------------
Total states run:
2
Total run time: 801.090 ms
2.4.6 state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt 'node1' state.show_sls web.apache.install
node1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.install
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.5 SaltStack常用模块之salt-cp
salt-cp
能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@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
//拷贝多个文件到目标主机的/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
2.6 SaltStack常用模块之user
2.6.1 user.add
添加用户到节点
[root@master ~]# salt 'node1' user.add xiongke
node1:
True
[root@master ~]# salt 'node1' cmd.run 'tail -1 /etc/passwd'
node1:
xiongke:x:1000:1000::/home/xiongke:/bin/bash
2.6.2 user.chfullname
改变用户全名
[root@master ~]# salt 'node1' user.chfullname xiongke "xk"
node1:
True
2.6.3 user.chuid
更改用户UID
[root@master ~]# salt 'node1' user.chuid xiongke 1001
node1:
True
[root@master ~]# salt 'node1' cmd.run 'tail -1 /etc/passwd'
node1:
xiongke:x:1001:1000:xk:/home/xiongke:/bin/bash
2.6.4 user.chgid
更改用户GID
[root@master ~]# salt 'node1' user.chgid xiongke 999
node1:
True
[root@master ~]# salt 'node1' cmd.run 'tail -1 /etc/passwd'
node1:
xiongke:x:1001:999:xk:/home/xiongke:/bin/bash
2.6.5 user.chgroups
更改用户属组,追加指定用户组
[root@master ~]# salt 'node1' user.chgroups xiongke root true
node1:
True
[root@master ~]# salt 'node1' cmd.run 'id xiongke'
node1:
uid=1001(xiongke) gid=999(input) groups=999(input),0(root)
2.6.6 user.chhome
更改用户家目录,拷贝原家目录到新家目录
[root@master ~]# salt 'node1' user.chhome xiongke /home/xk
node1:
True
[root@master ~]# salt 'node1' cmd.run 'tail -1 /etc/passwd'
node1:
xiongke:x:1001:999:xk:/home/xk:/bin/bash
2.7 SaltStack常用模块之file
2.7.1 file.access
检查指定路径是否存在
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
debug
group
kernels
passwd
shadow
[root@master ~]# salt 'node1' file.access /usr/src/passwd f
node1:
True
[root@master ~]# salt 'node1' file.access /usr/src/sb f
node1:
False
检查指定文件的权限信息
[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
513 Nov
3 01:30 group
drwxr-xr-x. 2 root root
6 May 18
2020 kernels
-rw-r--r--
1 root root 1118 Nov
3 01:29 passwd
-rw-r--r--
1 root root
676 Nov
3 01:30 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
2.7.2 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 5 mysql mysql 4096 Nov
3 21:58 data
-rw-r--r-- 1 root
root
0 Nov
3 22:06 xiongke
[root@master ~]# salt 'node1' file.append /opt/xiongke "xx" "hh" "vv"
node1:
Wrote 3 lines to "/opt/xiongke"
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 8
drwxr-xr-x 5 mysql mysql 4096 Nov
3 21:58 data
-rw-r--r-- 1 root
root
9 Nov
3 22:08 xiongke
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xiongke'
node1:
xx
hh
vv
2.7.3 file.basename
获取指定路径的基名
[root@master ~]# salt 'node1' file.basename '/opt/xiongke'
node1:
xiongke
2.7.4 file.dirname
获取指定路径的目录名
[root@master ~]# salt 'node1' file.dirname '/opt/xiongke'
node1:
/opt
2.7.5 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt 'node1' cmd.run 'md5sum /etc/passwd'
node1:
0b59434fbe7833b2ae393f5e3aa749d6
/etc/passwd
[root@master ~]# salt 'node1' file.check_hash /etc/passwd 0b59434fbe7833b2ae393f5e3aa749d6
node1:
True
2.7.6 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件,而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改,不能删除、更改、移动 | 任何的进程只能修改目录之下的文件,不允许建立和删除文件 |
给指定文件添加属性
[root@master ~]# salt 'node1' cmd.run 'lsattr /opt/xiongke'
node1:
-------------------- /opt/xiongke/xk
-------------------- /opt/xiongke/xx
[root@master ~]# salt 'node1' file.chattr /opt/xiongke/xx operator=add attributes=a
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr /opt/xiongke'
node1:
-------------------- /opt/xiongke/xk
-----a-------------- /opt/xiongke/xx
[root@master ~]# salt 'node1' cmd.run 'echo "hh" >> /opt/xiongke/xx'
node1:
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xiongke/xx'
node1:
hh
[root@master ~]# salt 'node1' cmd.run 'echo "xx" > /opt/xiongke/xx'
node1:
/bin/sh: /opt/xiongke/xx: Operation not permitted
ERROR: Minions returned with non-zero exit code
给指定文件去除属性
[root@master ~]# salt 'node1' file.chattr /opt/xiongke/xx operator=remove attributes=a
node1:
True
[root@master ~]# salt 'node1' cmd.run 'echo "xx" > /opt/xiongke/xx'
node1:
[root@master ~]# salt 'node1' cmd.run 'echo "hh" >> /opt/xiongke/xx'
node1:
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xiongke/xx'
node1:
xx
hh
给指定组添加属性
[root@master ~]# salt 'node1' cmd.run 'touch /opt/xiongke/xk/zz'
node1:
[root@master ~]# salt 'node1' cmd.run 'rm -rf /opt/xiongke/xk/zz'
node1:
rm: cannot remove '/opt/xiongke/xk/zz': Operation not permitted
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt 'node1' cmd.run 'echo "xxx" > /opt/xiongke/xk/zz'
node1:
[root@master ~]# salt 'node1' cmd.run 'echo "zzz" >> /opt/xiongke/xk/zz'
node1:
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xiongke/xk/zz'
node1:
xxx
zzz
2.7.7 file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt 'node1' file.chown /opt/xiongke/xx xiongke xiongke
node1:
None
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/xiongke'
node1:
total 4
drwxr-xr-x 2 root
root
16 Nov
3 22:27 xk
-rw-r--r-- 1 xiongke xiongke
6 Nov
3 22:26 xx
2.7.8 file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt 'node1' cmd.run 'tree /opt/xiongke'
node1:
/opt/xiongke
|-- xk
|
`-- zz
`-- xx
1 directory, 2 files
[root@master ~]# salt 'node1' file.copy /opt/xiongke/xx /opt/xiongke/xk/cc
node1:
True
[root@master ~]# salt 'node1' cmd.run 'tree /opt/xiongke'
node1:
/opt/xiongke
|-- xk
|
|-- cc
|
`-- zz
`-- xx
1 directory, 3 files
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt 'node1' file.copy /opt/xiongke/xx /root/abc recurse=true
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 4
drwxrwxrwx
2 xiongke xiongke
48 Nov
3 23:02 abc
-rw-------. 1 root
root
1087 Oct 12 07:11 anaconda-ks.cfg
drwxr-xr-x
3 root
root
31 Nov
1 00:11 xx
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abc'
node1:
total 8
-rw-r--r-- 1 root root
5 Nov
3 23:02 mysql.sock.lock
-rw-r--r-- 1 root root 40 Nov
3 23:02 tmpvlyz4uzo
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/xiongke'
node1:
total 4
drwxr-xr-x 2 root
root
26 Nov
3 22:46 xk
-rwxrwxrwx 1 xiongke xiongke
6 Nov
3 22:26 xx
[root@master ~]# salt 'node1' file.copy /root/abc/ /opt/xiongke recurse=true remove_existing=true
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/xiongke'
node1:
total 8
-rw-r--r-- 1 root root
5 Nov
3 23:02 mysql.sock.lock
-rw-r--r-- 1 root root 40 Nov
3 23:02 tmpvlyz4uzo
2.7.9 file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt 'node1' cmd.run 'mkdir -p /opt/xk'
node1:
[root@master ~]# salt 'node1' cmd.run 'ls /opt/'
node1:
data
xk
[root@master ~]# salt 'node1' file.directory_exists /opt/xk
node1:
True
[root@master ~]# salt 'node1' cmd.run 'rm -rf /opt/xk'
node1:
[root@master ~]# salt 'node1' cmd.run 'touch /opt/xk'
node1:
[root@master ~]# salt 'node1' cmd.run 'ls /opt/'
node1:
data
xk
[root@master ~]# salt 'node1' file.directory_exists /opt/xk
node1:
False
2.7.10 file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt 'node1' cmd.run 'du -sb /opt'
node1:
139311270
/opt
[root@master ~]# salt 'node1' file.diskusage /opt
node1:
139286666
2.7.11 file.file_exists
判断指定文件是否存在
[root@master ~]# salt 'node1' cmd.run 'ls /opt'
node1:
data
xk
[root@master ~]# salt 'node1' file.file_exists /opt/xk
node1:
True
[root@master ~]# salt 'node1' file.file_exists /opt/xx
node1:
False
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
[!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
示例:
[root@master ~]# salt 'node1' file.find / type=f name=*.cfg size=+1k
node1:
- /boot/grub2/grub.cfg
- /etc/grub2.cfg
- /root/anaconda-ks.cfg
- /usr/lib/python3.6/site-packages/pip/_vendor/distlib/_backport/sysconfig.cfg
- /usr/local/php-8.0.11/win32/build/cppcheck.cfg
- /usr/share/autoconf/autom4te.cfg
- /usr/share/doc/python3-pyyaml/examples/yaml-highlight/yaml_hl.cfg
[root@master ~]# salt 'node1' file.find /var mtime=+1d size=+10m print=path,size,mtime
node1:
|_
- /var/cache/dnf/appstream-773ef6463612e8e2/repodata/bbfd9f998be83279c080a37858e720faf0de47f75a98f2cdff5da20422b95538-filelists.xml.gz
- 13311037
- 1635752782
|_
- /var/cache/dnf/appstream-filenames.solvx
- 12904035
- 1635752785
[root@master ~]# salt 'node1' file.find /var/log name=*.[0-9] mtime=+1d size=+1k delete
node1:
- /var/log/audit/audit.log.4
2.7.13 file.get_gid
获取指定文件的gid
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/xk'
node1:
-rw-r--r-- 1 root root 0 Nov
3 23:12 /opt/xk
[root@master ~]# salt 'node1' file.get_gid /opt/xk
node1:
0
2.7.14 file.get_group
获取指定文件的组名
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/xk'
node1:
-rw-r--r-- 1 root root 0 Nov
3 23:12 /opt/xk
[root@master ~]# salt 'node1' file.get_group /opt/xk
node1:
root
2.7.15 file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt 'node1' cmd.run 'sha256sum /opt/xk'
node1:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
/opt/xk
[root@master ~]# salt 'node1' file.get_hash /opt/xk
node1:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
2.7.16file.get_mode
获取指定文件的权限,以数字方式显示
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/xk'
node1:
-rw-r--r-- 1 root root 0 Nov
3 23:12 /opt/xk
[root@master ~]# salt 'node1' file.get_mode /opt/xk
node1:
0644
2.7.17 ile.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt 'node1' cmd.run 'ls -Z /root/anaconda-ks.cfg'
node1:
system_u:object_r:admin_home_t:s0 /root/anaconda-ks.cfg
[root@master ~]# salt 'node1' file.get_selinux_context /root/anaconda-ks.cfg
node1:
system_u:object_r:admin_home_t:s0
2.7.18 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt 'node1' cmd.run 'sha256sum /opt/xk'
node1:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
/opt/xk
[root@master ~]# salt 'node1' file.get_sum /opt/xk
node1:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
[root@master ~]# salt 'node1' cmd.run 'md5sum /opt/xk'
node1:
d41d8cd98f00b204e9800998ecf8427e
/opt/xk
[root@master ~]# salt 'node1' file.get_sum /opt/xk md5
node1:
d41d8cd98f00b204e9800998ecf8427e
2.7.19 file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/xk'
node1:
-rw-r--r-- 1 root root 0 Nov
3 23:12 /opt/xk
[root@master ~]# salt 'node1' file.get_uid /opt/xk
node1:
0
[root@master ~]# salt 'node1' file.get_user /opt/xk
node1:
root
2.7.20 file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt 'node1' file.gid_to_group 999
node1:
input
[root@master ~]# salt 'node1' file.gid_to_group 0
node1:
root
2.7.21file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt 'node1' file.group_to_gid root
node1:
0
[root@master ~]# salt 'node1' file.group_to_gid xiongke
node1:
1000
2.7.22 file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@master ~]# salt 'node1' file.grep /etc/passwd xiongke
node1:
----------
pid:
226258
retcode:
0
stderr:
stdout:
xiongke:x:1001:999:xk:/home/xk:/bin/bash
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 name -- -i
node1:
----------
pid:
231322
retcode:
0
stderr:
stdout:
NAME=ens33
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 name -- -i -B2
node1:
----------
pid:
232712
retcode:
0
stderr:
stdout:
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
[root@master ~]# salt 'node1' file.grep "/etc/sysconfig/network-scripts/*" name -- -i -l
node1:
----------
pid:
234473
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-ens33
2.723 file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt 'node1' cmd.run 'ls -l /dev/sr0'
node1:
brw-rw---- 1 root cdrom 11, 0 Nov
3 21:58 /dev/sr0
[root@master ~]# salt 'node1' file.is_blkdev /dev/sr0
node1:
True
[root@master ~]# salt 'node1' file.is_blkdev /dev/cdrom
node1:
True
[root@master ~]# salt 'node1' file.is_blkdev /opt/xk
node1:
False
2.7.24 file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt 'node1' cmd.run 'lsattr /opt/xk'
node1:
-------------------- /opt/xk
[root@master ~]# salt 'node1' cmd.run 'chattr +i /opt/xk'
node1:
[root@master ~]# salt 'node1' cmd.run 'lsattr /opt/xk'
node1:
----i--------------- /opt/xk
[root@master ~]# salt 'node1' file.lsattr /opt/xk
node1:
----------
/opt/xk:
- i
[root@master ~]# salt 'node1' cmd.run 'chattr -i /opt/xk'
node1:
[root@master ~]# salt 'node1' file.lsattr /opt/xk
node1:
----------
/opt/xk:
2.7.25 file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 5 mysql mysql 4096 Nov
3 21:58 data
-rw-r--r-- 1 root
root
0 Nov
3 23:12 xk
[root@master ~]# salt 'node1' file.mkdir /opt/xiongke
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 5 mysql mysql 4096 Nov
3 21:58 data
drwxr-xr-x 2 root
root
6 Nov
3 23:53 xiongke
-rw-r--r-- 1 root
root
0 Nov
3 23:12 xk
[root@master ~]# salt 'node1' file.mkdir /opt/zzz xiongke xiongke 777
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 5 mysql
mysql
4096 Nov
3 21:58 data
drwxr-xr-x 2 root
root
6 Nov
3 23:53 xiongke
-rw-r--r-- 1 root
root
0 Nov
3 23:12 xk
drwxrwxrwx 2 xiongke xiongke
6 Nov
3 23:54 zzz
2.7.26 file.move
移动或重命名
//重命名
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 5 mysql
mysql
4096 Nov
3 21:58 data
drwxr-xr-x 2 root
root
6 Nov
3 23:53 xiongke
-rw-r--r-- 1 root
root
0 Nov
3 23:12 xk
drwxrwxrwx 2 xiongke xiongke
6 Nov
3 23:54 zzz
[root@master ~]# salt 'node1' file.move /opt/xiongke /opt/abc
node1:
----------
comment:
'/opt/xiongke' moved to '/opt/abc'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
drwxr-xr-x 5 mysql
mysql
4096 Nov
3 21:58 data
-rw-r--r-- 1 root
root
0 Nov
3 23:12 xk
drwxrwxrwx 2 xiongke xiongke
6 Nov
3 23:54 zzz
//移动
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
drwxr-xr-x 5 mysql
mysql
4096 Nov
3 21:58 data
-rw-r--r-- 1 root
root
0 Nov
3 23:12 xk
drwxrwxrwx 2 xiongke xiongke
6 Nov
3 23:54 zzz
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 4
-rw-------. 1 root root 1087 Oct 12 07:11 anaconda-ks.cfg
[root@master ~]# salt 'node1' file.move /opt/zzz /root/
node1:
----------
comment:
'/opt/zzz' moved to '/root/'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
drwxr-xr-x 5 mysql mysql 4096 Nov
3 21:58 data
-rw-r--r-- 1 root
root
0 Nov
3 23:12 xk
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 4
-rw-------. 1 root
root
1087 Oct 12 07:11 anaconda-ks.cfg
drwxrwxrwx
2 xiongke xiongke
6 Nov
3 23:54 zzz
2.7.27 file.prepend
把文本插入指定文件的开头
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xk'
node1:
大傻逼
[root@master ~]# salt 'node1' file.prepend /opt/xk "熊用民" "申龙飞"
node1:
Prepended 2 lines to "/opt/xk"
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xk'
node1:
熊用民
申龙飞
大傻逼
2.7.28 file.sed
修改文本文件的内容
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xk'
node1:
熊用民
申龙飞
大傻逼
[root@master ~]# salt 'node1' file.sed /opt/xk '申龙飞' '赵杰'
node1:
----------
pid:
241520
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xk'
node1:
熊用民
赵杰
大傻逼
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xk'
node1:
熊用民 熊用民
赵杰 赵杰 赵杰
大傻逼 大傻逼
[root@master ~]# salt 'node1' file.sed /opt/xk '赵杰' '熊用民' flags=2
node1:
----------
pid:
248523
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xk'
node1:
熊用民 熊用民
赵杰 熊用民 赵杰
大傻逼 大傻逼
2.7.29 file.read
读取文件内容
[root@master ~]# salt 'node1' cmd.run 'cat /opt/xk'
node1:
熊用民 熊用民
赵杰 熊用民 赵杰
大傻逼 大傻逼
[root@master ~]# salt 'node1' file.read /opt/xk
node1:
熊用民 熊用民
赵杰 熊用民 赵杰
大傻逼 大傻逼
2.7.30 file.readdir
列出指定目录下的所有文件或目录,包括隐藏文见
[root@master ~]# salt 'node1' file.readdir /root
node1:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .config
- .bash_history
- .mysql_history
- zzz
- .viminfo
2.7.31 file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt 'node1' cmd.run 'tree /opt/xiongke'
node1:
/opt/xiongke
|-- ccc
`-- zzz
1 directory, 1 file
[root@master ~]# salt 'node1' file.remove /opt/xiongke/ccc
node1:
True
[root@master ~]# salt 'node1' cmd.run 'tree /opt/xiongke'
node1:
/opt/xiongke
`-- zzz
0 directories, 1 file
[root@master ~]# salt 'node1' file.remove /opt/xiongke
node1:
True
[root@master ~]# salt 'node1' cmd.run 'tree /opt/xiongke'
node1:
/opt/xiongke [error opening dir]
0 directories, 0 files
2.7.32 file.rename
重命名文件或目录
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 12
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
drwxr-xr-x 5 mysql mysql 4096 Nov
4 21:12 data
-rw-r--r-- 1 root
root
64 Nov
4 00:11 xk
-rw-r--r-- 1 root
root
61 Nov
4 00:09 xk.bak
[root@master ~]# salt 'node1' file.rename /opt/xk /opt/xiongke
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 12
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
drwxr-xr-x 5 mysql mysql 4096 Nov
4 21:12 data
-rw-r--r-- 1 root
root
64 Nov
4 00:11 xiongke
-rw-r--r-- 1 root
root
61 Nov
4 00:09 xk.bak
2.7.33 file.set_mode
给指定文件设置权限
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 12
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
drwxr-xr-x 5 mysql mysql 4096 Nov
4 21:12 data
-rw-r--r-- 1 root
root
64 Nov
4 00:11 xiongke
-rw-r--r-- 1 root
root
61 Nov
4 00:09 xk.bak
[root@master ~]# salt 'node1' file.set_mode /opt/xiongke 000
node1:
0000
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 12
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
drwxr-xr-x 5 mysql mysql 4096 Nov
4 21:12 data
---------- 1 root
root
64 Nov
4 00:11 xiongke
-rw-r--r-- 1 root
root
61 Nov
4 00:09 xk.bak
2.7.34 file.symlink
给指定文件创建软链接
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 12
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
-rwxrwxrwx 1 root
root
0 Nov
4 21:27 ccc
drwxr-xr-x 5 mysql mysql 4096 Nov
4 21:12 data
-rwxrwxrwx 1 root
root
64 Nov
4 00:11 xiongke
-rw-r--r-- 1 root
root
61 Nov
4 00:09 xk.bak
-rw-r--r-- 1 root
root
0 Nov
4 21:29 xxx
[root@master ~]# salt 'node1' file.symlink /opt/xiongke /opt/sb
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 12
drwxr-xr-x 2 root
root
6 Nov
3 23:53 abc
-rwxrwxrwx 1 root
root
0 Nov
4 21:27 ccc
drwxr-xr-x 5 mysql mysql 4096 Nov
4 21:12 data
lrwxrwxrwx 1 root
root
12 Nov
4 21:34 sb -> /opt/xiongke
-rwxrwxrwx 1 root
root
64 Nov
4 00:11 xiongke
-rw-r--r-- 1 root
root
61 Nov
4 00:09 xk.bak
-rw-r--r-- 1 root
root
0 Nov
4 21:29 xxx
2.7.35 file.touch
创建空文件或更新时间戳
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 8
drwxr-xr-x 5 mysql mysql 4096 Nov
4 21:12 data
-rw-r--r-- 1 root
root
61 Nov
4 00:09 xk.bak
[root@master ~]# salt 'node1' file.touch /opt/abc
node1:
True
[root@master ~]# salt 'node1' file.touch /opt/xk.bak
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 8
-rw-r--r-- 1 root
root
0 Nov
4 21:36 abc
drwxr-xr-x 5 mysql mysql 4096 Nov
4 21:12 data
-rw-r--r-- 1 root
root
61 Nov
4 21:36 xk.bak
2.7.36file.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:
1000
[root@master ~]# salt 'node1' file.uid_to_user 999
node1:
systemd-coredump
2.7.37 file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@master ~]# salt 'node1' file.user_to_uid xiongke
node1:
1001
[root@master ~]# salt 'node1' file.user_to_uid root
node1:
0
2.7.38 file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt 'node1' file.write /opt/zzz "我是熊用民" "我是傻逼"
node1:
Wrote 2 lines to "/opt/zzz"
[root@master ~]# salt 'node1' cmd.run 'cat /opt/zzz'
node1:
我是熊用民
我是傻逼
最后
以上就是忐忑服饰为你收集整理的SaltStack常用的模块SaltStack常用的模块的全部内容,希望文章能够帮你解决SaltStack常用的模块SaltStack常用的模块所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复