saltstack常用模块
文章目录
- saltstack常用模块
- saltstack模块介绍
- saltstack常用模块
- salt-cp命令
- network模块
- service模块
- pkg模块
- state模块
- user模块
- file模块
- file.access
- file.append
- file.basename
- file.dirname
- file.check_hash
- file.chattr
- file.chown
- file.copy
- file.directory_exists
- file.diskusage
- file.file_exists
- file.find
- file.get_gid
- file.get_group
- file.get_hash
- file.get_mode
- file.get_selinux_context
- file.get_sum
- file.get_uid与file.get_user
- file.gid_to_group
- file.group_to_gid
- file.grep
- file.is_blkdev
- file.lsattr
- file.mkdir
- file.move
- 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
saltstack模块介绍
module是日常使用saltstack接触最多的一个组件,其用于管理对象操作,这也是saltstack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过saltstack module实现的
当安装好master和minion包后,系统上会安装很多module,大家可以通过以下命令查看支持的所有module列表
[root@master ~]# salt node1 sys.list_modules
node1:
- acl
- aliases
- alternatives
- 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 user
node1:
- user.add
- user.chfullname
- user.chgid
- user.chgroups
- user.chhome
- user.chhomephone
- user.chloginclass
- user.chother
- user.chroomnumber
- user.chshell
- user.chuid
- user.chworkphone
- user.delete
- user.get_loginclass
- user.getent
- user.info
- user.list_groups
- user.list_users
- user.primary_group
- user.rename
查看指定module的用法
[root@master ~]# salt node1 sys.doc user
user.add:
Add a user to the minion
name
Username LOGIN to add
uid
User ID of the new account
gid
Name or ID of the primary group of the new account
groups
List of supplementary groups of the new account
home
Home directory of the new account
shell
Login shell of the new account
unique
If not True, the user account can have a non-unique UID
system
Create a system account
fullname
GECOS field for the full name
......
SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号–args-separator=@即可
[root@master ~]# salt node1 test.echo,cmd.run,service.status tom,hostname,nginx
node1:
----------
cmd.run:
node1
service.status:
True
test.echo:
tom
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
network模块
network.active_tcp
返回所有活动的tcp连接
node1:
----------
0:
----------
local_addr:
192.168.100.120
local_port:
41308
remote_addr:
192.168.100.110
remote_port:
4505
1:
----------
local_addr:
192.168.100.120
local_port:
22
remote_addr:
192.168.100.1
remote_port:
52401
network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt '*' network.calc_net 192.168.100.120 255.255.255.0
node1:
192.168.100.0/24
[root@master ~]# salt '*' network.calc_net 192.168.100.120 255.255.0.0
node1:
192.168.0.0/16
network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt '*' network.connect baidu.com 80
node1:
----------
comment:
Successfully connected to baidu.com (220.181.38.251) on tcp port 80
result:
True
network.default_route
查看默认路由
[root@master ~]# salt '*' network.default_route
node1:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.100.2
interface:
ens33
netmask:
0.0.0.0
network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt '*' network.get_fqdn
node1:
node1
network.get_hostname
获取主机名
[root@master ~]# salt '*' network.get_hostname
node1:
node1
network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt '*' network.get_route 192.168.100.120
node1:
----------
destination:
192.168.100.120
gateway:
None
interface:
lo
source:
192.168.100.120
network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt '*' network.hw_addr ens33
node1:
00:0c:29:e4:bd:ac
network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt '*' network.ifacestartswith 192.168
node1:
- ens33
[root@master ~]# salt '*' network.ifacestartswith 192
node1:
- ens33
network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt '*' network.in_subnet 192.168.100.0/24
node1:
True
network.interface
返回指定的网卡信息
[root@master ~]# salt '*' network.interface ens33
node1:
|_
----------
address:
192.168.100.120
broadcast:
192.168.100.255
label:
ens33
netmask:
255.255.255.0
network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt '*' network.interface_ip ens33
node1:
192.168.100.120
network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt '*' network.interfaces
node1:
----------
ens33:
----------
hwaddr:
00:0c:29:e4:bd:ac
inet:
|_
----------
address:
192.168.100.120
broadcast:
192.168.100.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:fee4:bdac
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
network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1
的地址
[root@master ~]# salt '*' network.ip_addrs
node1:
- 192.168.100.120
network.netstat
返回所有打开的端口和状态
[root@master ~]# salt '*' network.netstat
node1:
|_
----------
inode:
26913
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:
26914
local-address:
[::1]:323
program:
chronyd
proto:
udp
recv-q:
0
remote-address:
[::]:*
send-q:
0
user:
0
|_
----------
inode:
26286
local-address:
0.0.0.0:80
program:
nginx
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
26141
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:
0
local-address:
192.168.100.120:37644
program:
proto:
tcp
recv-q:
0
remote-address:
192.168.100.110:4506
send-q:
0
state:
TIME-WAIT
user:
0
|_
----------
inode:
36451
local-address:
192.168.100.120:41308
program:
salt-minion
proto:
tcp
recv-q:
0
remote-address:
192.168.100.110:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
52988
local-address:
192.168.100.120:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.100.1:52401
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
26287
local-address:
[::]:80
program:
nginx
proto:
tcp
recv-q:
0
remote-address:
[::]:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
26150
local-address:
[::]:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
[::]:*
send-q:
128
state:
LISTEN
user:
0
network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt '*' network.ping baidu.comnode1: PING baidu.com (220.181.38.148) 56(84) bytes of data. 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=128 time=37.9 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=40.1 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=38.1 ms 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=39.6 ms --- baidu.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3007ms rtt min/avg/max/mdev = 37.927/38.946/40.147/0.954 ms
network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt '*' network.reverse_ip 192.168.100.10node1: 10.100.168.192.in-addr.arpa
service模块
service.available
判断指定的服务是否可用
[root@master ~]# salt '*' service.available nginxnode1: True[root@master ~]# salt '*' service.available httpdnode1: False
service.get_all
获取所有正在运行的服务
[root@master ~]# salt '*' service.get_allnode1: - 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 - console-getty - container-getty@ - cpupower - crond - cryptsetup-pre.target - cryptsetup.target - ctrl-alt-del.target - dbus - dbus-org.freedesktop.hostname1 - dbus-org.freedesktop.locale1 - dbus-org.freedesktop.login1 - dbus-org.freedesktop.nm-dispatcher - dbus-org.freedesktop.portable1 - dbus-org.freedesktop.timedate1 - dbus.socket - debug-shell ......
service.disabled
检查指定服务是否开机不自启
[root@master ~]# salt '*' service.disabled nginxnode1: False
service.enabled
检查指定服务是否开机自启
[root@master ~]# salt '*' service.enabled nginxnode1: True
service.disable
设置指定服务开机不自动启动
[root@master ~]# salt '*' service.disable nginxnode1: True[root@master ~]# salt '*' service.enabled nginxnode1: False
service.enable
设置指定服务开机自启
[root@master ~]# salt '*' service.enable nginxnode1: True[root@master ~]# salt '*' service.enabled nginxnode1: True
service.reload
重新加载指定服务
[root@master ~]# salt '*' service.reload nginxnode1: True
service.stop
停止指定服务
[root@master ~]# salt '*' service.stop nginxnode1: True
service.start
启动指定服务
[root@master ~]# salt '*' service.start nginxnode1: True
service.restart
重启指定服务
[root@master ~]# salt '*' service.restart nginx node1: True
service.status
查看指定服务的状态
[root@master ~]# salt '*' service.status nginx node1: True[root@master ~]# salt '*' service.stop nginx node1: True[root@master ~]# salt '*' service.status nginx node1: False
pkg模块
pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils
,可以使用cmd.run进行远程安装
[root@master ~]# salt '*' pkg.download wget
node1:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
pkg.file_list
列出指定包或系统中已安装的所有包的文件
列出已安装的nginx软件包提供的所有文件
[root@master ~]# salt '*' pkg.file_list nginx
node1:
----------
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
- /etc/nginx/uwsgi_params
- /etc/nginx/uwsgi_params.default
- /etc/nginx/win-utf
- /usr/bin/nginx-upgrade
- /usr/lib/.build-id
- /usr/lib/.build-id/2d
- /usr/lib/.build-id/2d/da6018ae12edb856ad3d2cf61bf586b6b4873c
- /usr/lib/systemd/system/nginx.service
- /usr/lib64/nginx/modules
- /usr/sbin/nginx
- /usr/share/doc/nginx
- /usr/share/doc/nginx/CHANGES
- /usr/share/doc/nginx/README
- /usr/share/doc/nginx/README.dynamic
- /usr/share/licenses/nginx
- /usr/share/licenses/nginx/LICENSE
- /usr/share/man/man3/nginx.3pm.gz
- /usr/share/man/man8/nginx-upgrade.8.gz
- /usr/share/man/man8/nginx.8.gz
- /usr/share/nginx/html/404.html
- /usr/share/nginx/html/50x.html
- /usr/share/nginx/html/index.html
- /usr/share/nginx/html/nginx-logo.png
- /usr/share/nginx/html/poweredby.png
- /usr/share/vim/vimfiles/ftdetect/nginx.vim
- /usr/share/vim/vimfiles/indent/nginx.vim
- /usr/share/vim/vimfiles/syntax/nginx.vim
- /var/lib/nginx
- /var/lib/nginx/tmp
- /var/log/nginx
pkg.group_info
查看包组信息
[root@master ~]# salt '*' 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
pkg.group_list
列出系统中所有的包组
[root@master ~]# salt '*' 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 - Development Tools - 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 available environments: - Server with GUI - Server - Workstation - Virtualization Host - Custom Operating System available languages: ---------- installed: - VMware platform specific packages - Core installed environments: - Minimal Install
pkg.install
安装软件
[root@master ~]# salt '*' pkg.install wgetnode1: ---------- wget: ---------- new: 1.19.5-10.el8 old:
pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt '*' pkg.list_downloaded node1: ----------
pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt '*' 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
......
pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt '*' pkg.owner /usr/sbin/nginxnode1: nginx [root@master ~]# salt '*' pkg.owner /usr/sbin/nginx /usr/sbin/sshdnode1: ---------- /usr/sbin/nginx: nginx /usr/sbin/sshd: openssh-server
pkg.remove
卸载指定软件
若要卸载多个文件,中间要用逗号隔开
[root@master ~]# salt '*' pkg.remove httpdnode1: ---------- apr: ---------- new: old: 1.6.3-12.el8 apr-util: ---------- new: old: 1.6.1-6.el8 apr-util-bdb: ---------- new: old: 1.6.1-6.el8 apr-util-openssl: ---------- new: old: 1.6.1-6.el8 centos-logos-httpd: ---------- new: old: 85.8-1.el8 httpd: ---------- new: old: 2.4.37-40.module_el8.5.0+852+0aafc63b httpd-filesystem: ---------- new: old: 2.4.37-40.module_el8.5.0+852+0aafc63b httpd-tools: ---------- new: old: 2.4.37-40.module_el8.5.0+852+0aafc63b mailcap: ---------- new: old: 2.1.48-3.el8 mod_http2: ---------- new: old: 1.15.7-3.module_el8.4.0+778+c970deab
pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt '*' pkg.upgrade name=opensslnode1: ---------- 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
state模块
state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt '*' state.show_highstate node1: ---------- httpd_install: ---------- __env__: base __sls__: web.httpd.install pkg: |_ ---------- name: httpd - installed |_ ---------- order: 10000 httpd_service: ---------- __env__: base __sls__: web.httpd.install service: |_ ---------- name: httpd |_ ---------- enable: True - running |_ ---------- order: 10001
state.highstate
执行高级状态
[root@master ~]# salt node1 state.highstate
node1:
----------
ID: httpd_install
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 03:53:39.957061
Duration: 16698.942 ms
Changes:
----------
apr:
----------
new:
1.6.3-12.el8
old:
apr-util:
----------
new:
1.6.1-6.el8
old:
apr-util-bdb:
----------
new:
1.6.1-6.el8
old:
apr-util-openssl:
----------
new:
1.6.1-6.el8
old:
centos-logos-httpd:
----------
new:
85.8-1.el8
old:
httpd:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
httpd-filesystem:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
httpd-tools:
----------
new:
2.4.37-40.module_el8.5.0+852+0aafc63b
old:
mailcap:
----------
new:
2.1.48-3.el8
old:
mod_http2:
----------
new:
1.15.7-3.module_el8.4.0+778+c970deab
old:
----------
ID: httpd_service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 03:53:56.696189
Duration: 279.321 ms
Changes:
----------
httpd:
True
Summary for node1
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 16.978 s
state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt node1 state.show_state_usage node1: ---------- base: ---------- count_all: 3 count_unused: 2 count_used: 1 unused: - top - web.nginx.install used: - web.httpd.install 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:
state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt node1 state.show_topnode1: ---------- base: - web.httpd.install
state.top
执行指定的top file,而不是默认的
[root@master ~]# salt node1 state.top test.slsnode1:---------- ID: httpd_install Function: pkg.installed Name: httpd Result: True Comment: All specified packages are already installed Started: 03:57:43.469124 Duration: 421.665 ms Changes: ---------- ID: httpd_service Function: service.running Name: httpd Result: True Comment: The service httpd is already running Started: 03:57:43.892584 Duration: 44.345 ms Changes: Summary for node1------------Succeeded: 2Failed: 0------------Total states run: 2Total run time: 466.010 ms
state.show_sls
显示master上特定sls或sls文件列表中的状态数据
[root@master ~]# salt node1 state.show_sls web.httpd.install
node1:
----------
httpd_install:
----------
__env__:
base
__sls__:
web.httpd.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
httpd_service:
----------
__env__:
base
__sls__:
web.httpd.install
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
user模块
user.add
示例
salt '*' user.add name <uid> <gid> <groups> <home> <shell>
创建用户
[root@master ~]# salt node1 user.add tomnode1: True[root@node1 ~]# id tom uid=1001(tom) gid=1001(tom) groups=1001(tom)
user.chgid
更改用户的默认组
[root@master ~]# salt node1 user.chgid tom 990node1: True [root@node1 ~]# id tom uid=1001(tom) gid=990(nginx) groups=990(nginx)
user.chgroups
更改用户的所属组
[root@master ~]# salt node1 user.chgroups tom tomnode1: True [root@node1 ~]# id tom uid=1001(tom) gid=990(nginx) groups=990(nginx),1001(tom)
user.chhome
更改用户的家目录,如果旧的家目录存在,则将家目录的文件移动到新的家目录
[root@master ~]# salt node1 user.chhome tom /home/tom Truenode1: True
user.chuid
更改指定用户的uid
[root@master ~]# salt node1 user.chuid tom 1666node1: True[root@node1 ~]# id tom uid=1666(tom) gid=990(nginx) groups=990(nginx),1001(tom)
user.delete
从minion中删除一个用户
[root@master ~]# salt node1 user.delete tomnode1: True [root@node1 ~]# id tomid: ‘tom’: no such user
user.rename
更改用户名
[root@master ~]# salt node1 user.rename jjyy jerrynode1: False [root@node1 ~]# id jjyyuid=1001(jjyy) gid=1002(jjyy) groups=1002(jjyy)[root@node1 ~]# id jjyyid: ‘jjyy’: no such user[root@node1 ~]# id jerryuid=1001(jerry) gid=1002(jjyy) groups=1002(jjyy)
file模块
file.access
检查指定路径是否存在
[root@node1 ~]# ls /usr/src/
debug kernels
[root@master ~]# salt node1 file.access /usr/src/debug f
node1:
True
[root@master ~]# salt node1 file.access /usr/src/abcd f
node1:
False
检查指定文件的权限信息
[root@node1 ~]# ll /usr/src/
total 0
drwxr-xr-x. 2 root root 6 May 18 2020 debug
drwxr-xr-x. 2 root root 6 May 18 2020 kernels
[root@master ~]# salt node1 file.access /usr/src/debug r //是否有读权限
node1:
True
[root@master ~]# salt node1 file.access /usr/src/debug w //是否有写权限
node1:
True
[root@master ~]# salt node1 file.access /usr/src/debug x //是否有执行权限
node1:
True
file.append
往一个文件里追加内容,若此文件不存在则会报错
[root@node1 ~]# ll test -rw-r--r--. 1 root root 0 Nov 4 02:26 test[root@master ~]# salt node1 file.append /root/test "hello" "abc" "123"node1: Wrote 3 lines to "/root/test" [root@node1 ~]# cat test helloabc123[root@node1 ~]# ll test -rw-r--r--. 1 root root 14 Nov 4 02:27 test
file.basename
获取指定路径的基名
[root@master ~]# salt node1 file.basename '/usr/src/debug'node1: debug
file.dirname
获取指定路径的目录名
[root@master ~]# salt node1 file.dirname '/usr/src/debug'node1: /usr/src
file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回True否则返回False
[root@node1 ~]# md5sum /etc/passwd6f2bf6845bf8f91dc96cc7088ed0b894 /etc/passwd[root@master ~]# salt node1 file.check_hash /etc/passwd 6f2bf6845bf8f91dc96cc7088ed0b894node1: True
file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件, 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改, 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件, 不允许建立和删除文件 |
给指定文件添加属性
[root@node1 ~]# lsattr /root/-------------------- /root/anaconda-ks.cfg-------------------- /root/test[root@master ~]# salt node1 file.chattr /root/test operator=add attributes=ainode1: True [root@node1 ~]# lsattr /root/-------------------- /root/anaconda-ks.cfg----ia-------------- /root/test
给指定文件去除属性
[root@master ~]# salt node1 file.chattr /root/test operator=remove attributes=inode1: True [root@node1 ~]# lsattr /root/-------------------- /root/anaconda-ks.cfg-----a-------------- /root/test
file.chown
设置指定文件的属主、属组信息
[root@node1 ~]# ll /root/total 8-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg-rw-r--r--. 1 root root 14 Nov 4 02:27 test[root@master ~]# salt node1 file.chown /root/test tom tomnode1: None [root@node1 ~]# lltotal 8-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg-rw-r--r--. 1 tom tom 14 Nov 4 02:27 test
file.copy
在远程主机上复制文件或目录
拷贝文件
[root@node1 ~]# ll
total 8
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:28 test
[root@master ~]# salt node1 file.copy /root/test /root/abc
node1:
True
[root@node1 ~]# ll
total 12
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@node1 ~]# ll
total 12
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.copy /tmp /root/123 recurse=True
node1:
True
[root@node1 ~]# ll
total 16
drwxrwxrwt. 14 root root 4096 Nov 4 02:52 123
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@node1 ~]# ll /root/123/total 0drwxr-xr-x. 2 root root 6 Nov 4 02:52 systemd-private-d9ff981f9b1044058722e73d615e68d6-chronyd.service-7LRV5edrwxr-xr-x. 2 root root 6 Nov 4 02:52 systemd-private-d9ff981f9b1044058722e73d615e68d6-httpd.service-8CpiI7drwxr-xr-x. 2 root root 6 Nov 4 02:52 vmware-root_900-2722108090drwxr-xr-x. 2 root root 6 Nov 4 02:52 vmware-root_905-4013330159drwxr-xr-x. 2 root root 6 Nov 4 02:52 vmware-root_913-4013723377drwxr-xr-x. 2 root root 6 Nov 4 02:52 vmware-root_914-2689209517drwxr-xr-x. 2 root root 6 Nov 4 02:52 vmware-root_916-2689078442[root@node1 ~]# ll /opt/123total 0-rw-r--r--. 1 root root 0 Nov 4 02:54 11-rw-r--r--. 1 root root 0 Nov 4 02:54 22-rw-r--r--. 1 root root 0 Nov 4 02:54 33-rw-r--r--. 1 root root 0 Nov 4 02:54 aa-rw-r--r--. 1 root root 0 Nov 4 02:54 bb-rw-r--r--. 1 root root 0 Nov 4 02:54 cc[root@master ~]# salt node1 file.copy /opt/123 /root/123 recurse=True remove_existing=Truenode1: True[root@node1 ~]# ll /root/123total 0-rw-r--r--. 1 root root 0 Nov 4 02:54 11-rw-r--r--. 1 root root 0 Nov 4 02:54 22-rw-r--r--. 1 root root 0 Nov 4 02:54 33-rw-r--r--. 1 root root 0 Nov 4 02:54 aa-rw-r--r--. 1 root root 0 Nov 4 02:54 bb-rw-r--r--. 1 root root 0 Nov 4 02:54 cc
file.directory_exists
判断指定目录是否存在,存在则返回True,否则返回False
[root@node1 ~]# ll /opt/
total 0
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
[root@master ~]# salt node1 file.directory_exists /opt/123
node1:
True
[root@master ~]# salt node1 file.directory_exists /opt/abc
node1:
False
file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt node1 cmd.run 'du -sb /opt'
node1:
83 /opt
[root@master ~]# salt node1 file.diskusage /opt
node1:
0
file.file_exists
判断指定文件是否存在
[root@node1 ~]# ll
total 12
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.file_exists /root/abc
node1:
True
[root@master ~]# salt node1 file.file_exists /root/aaa
node1:
False
file.find
类似find命令并返回符合指定条件的路径列表
The options include match criteria:
name = path-glob # case sensitive
iname = path-glob # case insensitive
regex = path-regex # case sensitive
iregex = path-regex # case insensitive
type = file-types # match any listed type
user = users # match any listed user
group = groups # match any listed group
size = [+-]number[size-unit] # default unit = byte
mtime = interval # modified since date
grep = regex # search file contents
and/or actions:
delete [= file-types] # default type = 'f'
exec = command [arg ...] # where {} is replaced by pathname
print [= print-opts]
and/or depth criteria:
maxdepth = maximum depth to transverse in path
mindepth = minimum depth to transverse before checking files or directories
The default action is print=path
path-glob:
* = match zero or more chars
? = match any char
[abc] = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y] = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c} = match a or b or c
path-regex
: a Python Regex (regular expression) pattern to match pathnames
file-types
: a string of one or more of the following:
a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket
users
: a space and/or comma separated list of user names and/or uids
groups
: a space and/or comma separated list of group names and/or gids
size-unit
:
b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes
interval:
[<num>w] [<num>d] [<num>h] [<num>m] [<num>s]
where:
w: week
d: day
h: hour
m: minute
s: second
print-opts: a comma and/or space separated list of one or more of the following:
group: group name
md5: MD5 digest of file contents
mode: file permissions (as integer)
mtime: last modification time (as time_t)
name: file basename
path: file absolute path
size: file size in bytes
type: file type
user: user name
示例:
salt '*' file.find / type=f name=\*.bak size=+10m
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete
file.get_gid
获取指定文件的gid
[root@node1 ~]# ll /root/abc
-rw-r--r--. 1 root root 14 Nov 4 02:49 /root/abc
[root@master ~]# salt node1 file.get_gid /root/abc
node1:
0
file.get_group
获取指定文件的组名
[root@node1 ~]# ll /root/abc
-rw-r--r--. 1 root root 14 Nov 4 02:49 /root/abc
[root@master ~]# salt node1 file.get_group /root/abc
node1:
root
file.get_hash
获取指定文件的hash值,该值通过sha256算法得来
[root@node1 ~]# sha256sum /root/test
fd11635eee01016e58eb15d4445de1a82ee697166e3ca9299c188518ca169d70 /root/test
[root@master ~]# salt node1 file.get_hash /root/test
node1:
fd11635eee01016e58eb15d4445de1a82ee697166e3ca9299c188518ca169d70
file.get_mode
获取指定文件的权限,以数字方式显示
[root@node1 ~]# ll /root/test
-rw-r--r--. 1 root root 14 Nov 4 02:27 /root/test
[root@master ~]# salt node1 file.get_mode /root/test
node1:
0644
file.get_selinux_context
获取指定文件的SELINUX上下文信息
[root@master ~]# salt node1 cmd.run 'ls -Z /root/test'
node1:
unconfined_u:object_r:admin_home_t:s0 /root/test
[root@master ~]# salt node1 file.get_selinux_context /root/test
node1:
unconfined_u:object_r:admin_home_t:s0
file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256(default)
- sha384
- sha512
[root@master ~]# salt node1 cmd.run 'sha256sum /root/test'
node1:
fd11635eee01016e58eb15d4445de1a82ee697166e3ca9299c188518ca169d70 /root/test
[root@master ~]# salt node1 file.get_sum /root/test
node1:
fd11635eee01016e58eb15d4445de1a82ee697166e3ca9299c188518ca169d70
[root@master ~]# salt node1 cmd.run 'md5sum /root/test'
node1:
2271a19ba08dd5d198b073bce1cca5ab /root/test
[root@master ~]# salt node1 file.get_sum /root/test md5
node1:
2271a19ba08dd5d198b073bce1cca5ab
file.get_uid与file.get_user
获取指定文件的uid或用户名
[root@master ~]# salt node1 cmd.run 'ls -l /root/test'
node1:
-rw-r--r--. 1 root root 14 Nov 4 02:27 /root/test
[root@master ~]# salt node1 file.get_uid /root/test
node1:
0
[root@master ~]# salt node1 file.get_user /root/test
node1:
root
file.gid_to_group
将指定的gid转换为组名并显示
[root@master ~]# salt node1 file.gid_to_group 1000
node1:
sq
[root@master ~]# salt node1 file.gid_to_group 0
node1:
root
file.group_to_gid
将指定的组名转换为gid并显示
[root@master ~]# salt node1 file.group_to_gid root
node1:
0
[root@master ~]# salt node1 file.group_to_gid sq
node1:
1000
file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@master ~]# salt node1 file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
node1:
----------
pid:
2629
retcode:
0
stderr:
stdout:
IPADDR=192.168.100.120
[root@master ~]# salt node1 file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
node1:
----------
pid:
2634
retcode:
0
stderr:
stdout:
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.120
file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt node1 cmd.run 'ls -l /dev/sr0'
node1:
brw-rw----. 1 root cdrom 11, 0 Nov 3 21:24 /dev/sr0
[root@master ~]# salt node1 file.is_blkdev /dev/sr0
node1:
True
file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt node1 cmd.run 'lsattr /root/test'
node1:
-------------------- /root/test
[root@master ~]# salt node1 cmd.run 'chattr +i /root/test'
node1:
[root@master ~]# salt node1 cmd.run 'lsattr /root/test'
node1:
----i--------------- /root/test
[root@master ~]# salt node1 file.lsattr /root/test
node1:
----------
/root/test:
- i
file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.mkdir /root/aaa
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
drwxr-xr-x. 2 root root 6 Nov 4 03:38 aaa
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.mkdir /root/bbb tom tom 777
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
drwxr-xr-x. 2 root root 6 Nov 4 03:38 aaa
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
drwxrwxrwx. 2 tom tom 6 Nov 4 03:38 bbb
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
file.move
移动或重命名
重命名
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
drwxr-xr-x. 2 root root 6 Nov 4 03:38 aaa
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
drwxrwxrwx. 2 tom tom 6 Nov 4 03:38 bbb
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.move /root/bbb /root/ccc
node1:
----------
comment:
'/root/bbb' moved to '/root/ccc'
result:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
drwxr-xr-x. 2 root root 6 Nov 4 03:38 aaa
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
drwxrwxrwx. 2 tom tom 6 Nov 4 03:38 ccc
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
移动
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
drwxr-xr-x. 2 root root 6 Nov 4 03:38 aaa
-rw-r--r--. 1 root root 14 Nov 4 02:49 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
drwxrwxrwx. 2 tom tom 6 Nov 4 03:38 ccc
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
total 0
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
[root@master ~]# salt node1 file.move /root/ccc /opt/
node1:
----------
comment:
'/root/ccc' moved to '/opt/'
result:
True
[root@master ~]# salt node1 cmd.run 'ls -l /opt'
node1:
total 0
drwxr-xr-x. 2 root root 66 Nov 4 02:54 123
drwxrwxrwx. 2 tom tom 6 Nov 4 03:38 ccc
file.sed
修改文本文件的内容
[root@master ~]# salt node1 cmd.run 'cat /root/abc'
node1:
hello
xixi
haha
[root@master ~]# salt node1 file.sed /root/abc 'hello' 'nihao'
node1:
----------
pid:
1936
retcode:
0
stderr:
stdout:
[root@master ~]# salt node1 cmd.run 'cat /root/abc'
node1:
nihao
xixi
haha
file.read
读取文件内容
[root@master ~]# salt node1 file.read /root/abc
node1:
nihao
xixi
haha
file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt node1 file.readdir /root
node1:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .bash_history
- .viminfo
- test
- abc.bak
- abc
file.remove
删除指定的文件或目录,若给出的是目录,则递归删除
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 16
-rw-r--r--. 1 root root 16 Nov 7 01:55 abc
-rw-r--r--. 1 root root 16 Nov 7 01:54 abc.bak
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.remove /root/abc.bak
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r--. 1 root root 16 Nov 7 01:55 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
file.rename
重命名文件或目录
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r--. 1 root root 16 Nov 7 01:55 abc
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.rename /root/abc /root/123
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r--. 1 root root 16 Nov 7 01:55 123
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
file.set_mode
给指定文件设置权限
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r--. 1 root root 16 Nov 7 01:55 123
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.set_mode /root/123 0700
node1:
0700
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rwx------. 1 root root 16 Nov 7 01:55 123
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
file.symlink
给指定的文件创建软链接
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rwx------. 1 root root 16 Nov 7 01:55 123
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.symlink /root/123 /root/abc
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rwx------. 1 root root 16 Nov 7 01:55 123
lrwxrwxrwx. 1 root root 9 Nov 7 02:02 abc -> /root/123
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
file.touch
创建空文件或更新时间戳
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rwx------. 1 root root 16 Nov 7 01:55 123
lrwxrwxrwx. 1 root root 9 Nov 7 02:02 abc -> /root/123
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
[root@master ~]# salt node1 file.touch /root/456
node1:
True
[root@master ~]# salt node1 file.touch /root/123
node1:
True
[root@master ~]# salt node1 cmd.run 'ls -l /root'
node1:
total 12
-rwx------. 1 root root 16 Nov 7 02:04 123
-rw-r--r--. 1 root root 0 Nov 7 02:04 456
lrwxrwxrwx. 1 root root 9 Nov 7 02:02 abc -> /root/123
-rw-------. 1 root root 1181 Sep 27 05:27 anaconda-ks.cfg
-rw-r--r--. 1 root root 14 Nov 4 02:27 test
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:
sq
file.user_to_uid
将指定的用户转换成uid并显示出来
[root@master ~]# salt node1 file.user_to_uid root
node1:
0
[root@master ~]# salt node1 file.user_to_uid sq
node1:
1000
file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt node1 cmd.run 'cat /root/123'
node1:
nihao
xixi
haha
[root@master ~]# salt node1 file.write /root/123 'dd' 'abc' 'aaa'
node1:
Wrote 3 lines to "/root/123"
[root@master ~]# salt node1 cmd.run 'cat /root/123'
node1:
dd
abc
aaa