SaltStack常用模块
SaltStack模块介绍
环境说明
角色 | 主机名 | IP地址 |
---|---|---|
salt-master | master | 192.168.47.115 |
salt-minion | minion | 192.168.47.120 |
官方文档:https://docs.saltproject.io/en/latest/
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
//查看所有module列表
[root@master ~]# salt 'minion' sys.list_modules
minion:
- acl
- aliases
- alternatives
- apache
- archive
- artifactory
- baredoc
- beacons
- bigip
- btrfs
- buildout
- chroot
- cloud
- cmd
- composer
- config
- consul
- container_resource
- 此处省略N行
//查看指定module的所有function
[root@master ~]# salt 'minion' sys.list_functions cmd
minion:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.powershell
- cmd.powershell_all
- cmd.retcode
- cmd.run
- cmd.run_all
- cmd.run_bg
- cmd.run_chroot
- cmd.run_stderr
- cmd.run_stdout
- cmd.script
- cmd.script_retcode
- cmd.shell
- cmd.shell_info
- cmd.shells
- cmd.tty
- cmd.which
- cmd.which_bin
//查看指定module的用法
- cmd.which_bin
[root@master ~]# salt 'minion' 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"}'
cmd.exec_code_all:
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. All cmd artifacts (stdout, stderr, retcode, pid)
will be returned.
All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.
...此处省略N行...
//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt 'minion' test.echo,cmd.run,service.status apache,date,salt-minion
minion:
----------
cmd.run:
Thu Nov 4 02:00:33 CST 2021
service.status:
True
test.echo:
apache
SaltStack常用模块
SaltStack常用模块之network
network.active_tcp
返回所有活动的tcp连接
[root@master ~]# salt 'minion' network.active_tcp
minion:
----------
0:
----------
local_addr:
192.168.47.120
local_port:
22
remote_addr:
192.168.47.1
remote_port:
52971
1:
----------
local_addr:
192.168.47.120
local_port:
56166
remote_addr:
192.168.47.115
remote_port:
4505
network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt 'minion' network.calc_net 192.168.47.0 255.255.255.0
minion:
192.168.47.0/24
[root@master ~]# salt 'minion' network.calc_net 192.168.47.115 255.255.248.0
minion:
192.168.40.0/21
network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt 'minion' network.connect baidu.com 80
minion:
----------
comment:
Successfully connected to baidu.com (220.181.38.148) on tcp port 80
result:
True
[root@master ~]# salt 'minion' network.connect 192.168.47.120 22
minion:
----------
comment:
Successfully connected to 192.168.47.120 (192.168.47.120) on tcp port 22
result:
True
network.default_route
查看默认路由
[root@master ~]# salt 'minion' network.default_route
minion:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.47.2
interface:
ens160
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:
network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt 'minion' network.get_fqdn
minion:
minion
network.get_hostname
获取主机名
[root@master ~]# salt 'minion' network.get_hostname
minion:
minion
#这里修改受控端的主机名是为了能看到效果,看完以后修改回来
[root@minion ~]# hostnamectl set-hostname minion_minion
[root@minion ~]# bash
[root@master ~]# salt 'minion' network.get_hostname
minion:
minion_minion
network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt 'minion' network.get_route 192.168.200.0
minion:
----------
destination:
192.168.200.0
gateway:
192.168.47.2
interface:
ens160
source:
192.168.47.120
network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt 'minion' network.hw_addr ens160
minion:
00:0c:29:da:55:0e
network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt 'minion' network.ifacestartswith 192
minion:
- ens160
network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt 'minion' network.in_subnet 192.168.144.0/24
minion:
False
[root@master ~]# salt 'minion' network.in_subnet 192.168.47.0/24
minion:
True
[root@master ~]# salt 'minion' network.in_subnet 192.168.0.0/16
minion:
True
network.interface
返回指定网卡的信息
[root@master ~]# salt 'minion' network.interface ens160
minion:
|_
----------
address:
192.168.47.120
broadcast:
192.168.47.255
label:
ens160
netmask:
255.255.255.0
network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt 'minion' network.interface_ip ens160
minion:
192.168.47.120
network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt 'minion' network.interfaces
minion:
----------
ens160:
----------
hwaddr:
00:0c:29:da:55:0e
inet:
|_
----------
address:
192.168.47.120
broadcast:
192.168.47.255
label:
ens160
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:feda:550e
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 'minion' network.ip_addrs
minion:
- 192.168.47.120
network.netstat
返回所有打开的端口和状态
[root@master ~]# salt 'minion' network.netstat
minion:
|_
----------
inode:
26515
local-address:
0.0.0.0:22
program:
1017/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
0
local-address:
192.168.47.120:39798
program:
-
proto:
tcp
recv-q:
0
remote-address:
192.168.47.115:4506
send-q:
0
state:
TIME_WAIT
user:
0
|_
----------
inode:
0
local-address:
192.168.47.120:39794
program:
-
proto:
tcp
recv-q:
0
remote-address:
192.168.47.115:4506
send-q:
0
state:
TIME_WAIT
user:
0
|_
----------
inode:
32049
local-address:
192.168.47.120:22
program:
1900/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.47.1:52971
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
0
local-address:
192.168.47.120:39796
program:
-
proto:
tcp
recv-q:
0
remote-address:
192.168.47.115:4506
send-q:
0
state:
TIME_WAIT
user:
0
|_
----------
inode:
30920
local-address:
192.168.47.120:56166
program:
1599/python3.6
proto:
tcp
recv-q:
0
remote-address:
192.168.47.115:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
26554
local-address:
:::80
program:
1011/httpd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
26517
local-address:
:::22
program:
1017/sshd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
26107
local-address:
127.0.0.1:323
program:
992/chronyd
proto:
udp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
user:
0
|_
----------
inode:
26108
local-address:
::1:323
program:
992/chronyd
proto:
udp6
recv-q:
0
remote-address:
:::*
send-q:
0
user:
0
network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt 'minion' network.ping baidu.com
minion:
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=24.6 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=29.3 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=50.8 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=72.5 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 9ms
rtt min/avg/max/mdev = 24.575/44.296/72.489/19.051 ms
network.reverse_ip
返回一个指定的IP地址的反向地址
root@master ~]# salt 'minion' network.reverse_ip 192.168.200.0
minion:
0.200.168.192.in-addr.arpa
SaltStack常用模块之service
service.available
判断指定的服务是否可用
//查看minion端的httpd服务状态
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-11-04 01:54:53 CST; 21min ago
Docs: man:httpd.service(8)
Main PID: 1011 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11300)
Memory: 45.9M
CGroup: /system.slice/httpd.service
├─1011 /usr/sbin/httpd -DFOREGROUND
├─1062 /usr/sbin/httpd -DFOREGROUND
├─1065 /usr/sbin/httpd -DFOREGROUND
├─1067 /usr/sbin/httpd -DFOREGROUND
└─1069 /usr/sbin/httpd -DFOREGROUND
11月 04 01:54:53 minion systemd[1]: Starting The Apache HTTP Server...
11月 04 01:54:53 minion httpd[1011]: AH00558: httpd: Could not reliably determine the server's f>
11月 04 01:54:53 minion systemd[1]: Started The Apache HTTP Server.
11月 04 01:54:53 minion httpd[1011]: Server configured, listening on: port 80
//在master端执行模块查看httpd状态
[root@master ~]# salt 'minion' service.available httpd
minion:
True
//在master端执行模块查看nginx状态
[root@minion ~]# systemctl status nginx
Unit nginx.service could not be found.
[root@master ~]# salt 'minion' service.available nginx
minion:
False
service.get_all
获取所有正在运行的服务
[root@master ~]# salt 'minion' service.get_all
minion:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- console-getty
- container-getty@
- cpupower
- crond
- 此处省略N行
service.disabled
检查指定服务是否开机不自动启动
#过去式的是查看状态是否满足
#是False说明是开机自启
[root@master ~]# salt 'minion' service.disabled httpd
minion:
False
service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt 'minion' service.enabled httpd
minion:
True
service.disable
设置指定服务开机不自动启动
[root@master ~]# salt 'minion' service.disable httpd
minion:
True
[root@master ~]# salt 'minion' service.enabled httpd
minion:
False
service.enable
设置指定服务开机自动启动
[root@master ~]# salt 'minion' service.enable httpd
minion:
True
[root@master ~]# salt 'minion' service.enabled httpd
minion:
True
service.reload
重新加载指定服务
[root@master ~]# salt 'minion' service.reload httpd
minion:
True
service.stop
停止指定服务
[root@master ~]# salt 'minion' service.stop httpd
minion:
True
//查看效果
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2021-11-04 02:23:29 CST; 8s ago
Docs: man:httpd.service(8)
Process: 45471 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Process: 1011 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 1011 (code=exited, status=0/SUCCESS)
Status: "Configuration loaded."
11月 04 01:54:53 minion systemd[1]: Starting The Apache HTTP Server...
11月 04 01:54:53 minion httpd[1011]: AH00558: httpd: Could not reliably determine the server's f>
11月 04 01:54:53 minion systemd[1]: Started The Apache HTTP Server.
11月 04 01:54:53 minion httpd[1011]: Server configured, listening on: port 80
11月 04 02:23:05 minion systemd[1]: Reloading The Apache HTTP Server.
11月 04 02:23:16 minion httpd[45471]: AH00558: httpd: Could not reliably determine the server's >
11月 04 02:23:16 minion systemd[1]: Reloaded The Apache HTTP Server.
11月 04 02:23:26 minion httpd[1011]: Server configured, listening on: port 80
11月 04 02:23:28 minion systemd[1]: Stopping The Apache HTTP Server...
11月 04 02:23:29 minion systemd[1]: Stopped The Apache HTTP Server.
service.start
启动指定服务
[root@master ~]# salt 'minion' service.start httpd
minion:
True
//查看效果
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: reloading (reload) since Thu 2021-11-04 02:24:02 CST; 7s ago
Docs: man:httpd.service(8)
Process: 45471 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 47253 (httpd)
Status: "Reading configuration..."
Tasks: 1 (limit: 11300)
Memory: 3.4M
CGroup: /system.slice/httpd.service
└─47253 /usr/sbin/httpd -DFOREGROUND
11月 04 02:24:02 minion systemd[1]: Starting The Apache HTTP Server...
11月 04 02:24:02 minion httpd[47253]: AH00558: httpd: Could not reliably determine the server's >
11月 04 02:24:02 minion systemd[1]: Started The Apache HTTP Server.
service.restart
重启指定服务
[root@master ~]# salt 'minion' service.restart httpd
minion:
True
service.status
查看指定服务的状态
[root@master ~]# salt 'minion' service.status httpd
minion:
True
SaltStack常用模块之pkg
pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils
,可以使用 cmd.run 进行远程安装
[root@master ~]# salt 'minion' pkg.download wget
minion:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm #下载好的软件放在这里
//查看效果
[root@minion ~]# ll /var/cache/yum/packages/
总用量 736
-rw-r--r-- 1 root root 751256 11月 4 02:24 wget-1.19.5-10.el8.x86_64.rpm
pkg.file_list
列出指定包或系统中已安装的所有包的文件
//列出已安装的apache软件包提供的所有文件
[root@master ~]# salt 'minion' pkg.file_list httpd
minion:
----------
errors:
files:
- /etc/logrotate.d/httpd
- /etc/httpd/fastcgi.conf
- /etc/httpd/fastcgi.conf.default
- /etc/httpd/fastcgi_params
- /etc/httpd/fastcgi_params.default
- /etc/httpd/koi-utf
- /etc/httpd/koi-win
- /etc/httpd/mime.types
- /etc/httpd/mime.types.default
- /etc/httpd/httpd.conf
- /etc/httpd/httpd.conf.default
- /etc/httpd/scgi_params
- /etc/httpd/scgi_params.default
- /etc/httpd/uwsgi_params
- /etc/httpd/uwsgi_params.default
- /etc/httpd/win-utf
- /usr/bin/httpd-upgrade
- /usr/lib/.build-id
- /usr/lib/.build-id/2d
- /usr/lib/.build-id/2d/da6018ae12edb856ad3d2cf61bf586b6b4873c
- /usr/lib/systemd/system/httpd.service
- /usr/lib64/httpd/modules
- /usr/sbin/httpd
- /usr/share/doc/httpd
- /usr/share/doc/httpd/CHANGES
- /usr/share/doc/httpd/README
- /usr/share/doc/httpd/README.dynamic
- /usr/share/licenses/httpd
- /usr/share/licenses/httpd/LICENSE
- /usr/share/man/man3/httpd.3pm.gz
- /usr/share/man/man8/httpd-upgrade.8.gz
- /usr/share/man/man8/httpd.8.gz
- /usr/share/httpd/html/404.html
- /usr/share/httpd/html/50x.html
- /usr/share/httpd/html/index.html
- /usr/share/httpd/html/httpd-logo.png
- /usr/share/httpd/html/poweredby.png
- /usr/share/vim/vimfiles/ftdetect/httpd.vim
- /usr/share/vim/vimfiles/indent/httpd.vim
- /usr/share/vim/vimfiles/syntax/httpd.vim
- /var/lib/httpd
- /var/lib/httpd/tmp
- /var/log/httpd
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt 'minion' pkg.file_list
minion:
----------
errors:
files:
VALUE_TRIMMED
pkg.group_info
查看包组的信息
[root@master ~]# salt 'minion' pkg.group_info 'Development Tools'
minion:
----------
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 'minion' pkg.group_list
minion:
----------
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
- Development 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
- VMware platform specific packages
installed environments:
- Minimal Install
pkg.install
安装软件
#说明wget已经安装
[root@master ~]# salt 'minion' pkg.install wget
minion:
----------
[root@master ~]# salt 'minion' pkg.install httpd
minion:
----------
httpd:
----------
new:
2.4.37-39.module_el8.4.0+950+0577e6ac.1
old:
2.4.37-39.module_el8.4.0+778+c970deab
httpd-filesystem:
----------
new:
2.4.37-39.module_el8.4.0+950+0577e6ac.1
old:
2.4.37-39.module_el8.4.0+778+c970deab
httpd-tools:
----------
new:
2.4.37-39.module_el8.4.0+950+0577e6ac.1
old:
2.4.37-39.module_el8.4.0+778+c970deab
pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt 'minion' pkg.list_downloaded
minion:
----------
pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt 'minion' pkg.list_pkgs
minion:
----------
NetworkManager:
1:1.22.8-4.el8
NetworkManager-libnm:
1:1.22.8-4.el8
NetworkManager-team:
1:1.22.8-4.el8
NetworkManager-tui:
1:1.22.8-4.el8
acl:
2.2.53-1.el8
...此处省略N行
pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt 'minion' pkg.owner /usr/bin/ls
minion:
coreutils
[root@master ~]# salt 'minion' pkg.owner /etc/httpd/conf/httpd.conf
minion:
httpd
pkg.remove
卸载指定软件
[root@master ~]# salt 'minion' cmd.run 'rpm -qa|grep wget'
minion:
wget-1.19.5-10.el8.x86_64
[root@master ~]# salt 'minion' pkg.remove wget
minion:
----------
wget:
----------
new:
old:
1.19.5-10.el8
#若要卸载多个文件,中间需要用逗号隔开
pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt 'minion' pkg.upgrade name=httpd
minion:
----------
#若想升级系统中所有的软件包则把 name 参数去掉即可
SaltStack常用模块之state
state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# cat /srv/salt/base/web/apache/install.sls
httpd-install:
pkg.installed:
- name: httpd
[root@master ~]# cat /srv/salt/base/top.sls
base:
'os:Redhat':
- match: grain
- web.apache.install
[root@master ~]# salt 'minion' state.show_highstate
minion:
----------
httpd-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
state.highstate
执行高级状态
[root@master ~]# salt 'minion' state.highstate web.apache.install
minion:
----------
ID: httpd-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 02:49:29.353797
Duration: 583.489 ms
Changes:
Summary for minion
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 583.489 ms
state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt 'minion' state.show_state_usage
minion:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.apache.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 'minion' state.show_top
minion:
----------
base:
- web.apache.install
state.top
执行指定的top file,而不是默认的
[root@master ~]# salt 'minion' state.top top.sls
minion:
----------
ID: httpd-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 02:50:27.247421
Duration: 570.522 ms
Changes:
Summary for minion
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 570.522 ms
state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt 'minion' state.show_sls web.apache.install
minion:
----------
httpd-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
SaltStack常用模块之user
- 查看关于user模块的相关函数
[root@master ~]# salt 'minion' sys.list_functions user
minion:
- 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
user.add
在minion端上创建一个用户,用法:salt ‘*’ user.add name
[root@master ~]# salt 'minion' user.add tanjie
minion:
True
user.info
返回指定用户信息
[root@master ~]# salt 'minion' user.info tanjie
minion:
----------
fullname:
gid:
1001
groups:
- tanjie
home:
/home/tanjie
homephone:
name:
tanjie
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1001
workphone:
user.getent
返回所有系统用户信息的列表
[root@master ~]# salt 'minion' user.getent
minion:
|_
----------
fullname:
root
gid:
0
groups:
- root
home:
/root
homephone:
name:
root
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
0
workphone:
|_
----------
fullname:
bin
gid:
1
groups:
- bin
home:
/bin
homephone:
name:
bin
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
1
workphone:
|_
----------
fullname:
daemon
gid:
2
groups:
- daemon
home:
/sbin
homephone:
name:
daemon
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
2
workphone:
|_
----------
fullname:
adm
gid:
4
groups:
- adm
home:
/var/adm
homephone:
name:
adm
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
3
workphone:
|_
----------
fullname:
lp
gid:
7
groups:
- lp
home:
/var/spool/lpd
homephone:
name:
lp
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
4
workphone:
|_
----------
fullname:
sync
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
sync
other:
passwd:
x
roomnumber:
shell:
/bin/sync
uid:
5
workphone:
|_
----------
fullname:
shutdown
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
shutdown
other:
passwd:
x
roomnumber:
shell:
/sbin/shutdown
uid:
6
workphone:
|_
----------
fullname:
halt
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
halt
other:
passwd:
x
roomnumber:
shell:
/sbin/halt
uid:
7
workphone:
|_
----------
fullname:
mail
gid:
12
groups:
- mail
home:
/var/spool/mail
homephone:
name:
mail
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
8
workphone:
|_
----------
fullname:
operator
gid:
0
groups:
- root
home:
/root
homephone:
name:
operator
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
11
workphone:
|_
----------
fullname:
games
gid:
100
groups:
- users
home:
/usr/games
homephone:
name:
games
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
12
workphone:
|_
----------
fullname:
FTP User
gid:
50
groups:
- ftp
home:
/var/ftp
homephone:
name:
ftp
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
14
workphone:
|_
----------
fullname:
Kernel Overflow User
gid:
65534
groups:
- nobody
home:
/
homephone:
name:
nobody
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
65534
workphone:
|_
----------
fullname:
System message bus
gid:
81
groups:
- dbus
home:
/
homephone:
name:
dbus
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
81
workphone:
|_
----------
fullname:
systemd Core Dumper
gid:
997
groups:
- systemd-coredump
home:
/
homephone:
name:
systemd-coredump
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
999
workphone:
|_
----------
fullname:
systemd Resolver
gid:
193
groups:
- systemd-resolve
home:
/
homephone:
name:
systemd-resolve
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
193
workphone:
|_
----------
fullname:
Account used by the trousers package to sandbox the tcsd daemon
gid:
59
groups:
- tss
home:
/dev/null
homephone:
name:
tss
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
59
workphone:
|_
----------
fullname:
User for polkitd
gid:
996
groups:
- polkitd
home:
/
homephone:
name:
polkitd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
998
workphone:
|_
----------
fullname:
Unbound DNS resolver
gid:
995
groups:
- unbound
home:
/etc/unbound
homephone:
name:
unbound
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
997
workphone:
|_
----------
fullname:
User for sssd
gid:
993
groups:
- sssd
home:
/
homephone:
name:
sssd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
996
workphone:
|_
----------
fullname:
Privilege-separated SSH
gid:
74
groups:
- sshd
home:
/var/empty/sshd
homephone:
name:
sshd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
74
workphone:
|_
----------
fullname:
gid:
992
groups:
- chrony
home:
/var/lib/chrony
homephone:
name:
chrony
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
995
workphone:
|_
----------
fullname:
Random Number Generator Daemon
gid:
991
groups:
- rngd
home:
/var/lib/rngd
homephone:
name:
rngd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
994
workphone:
|_
----------
fullname:
tj
gid:
1000
groups:
- tj
home:
/home/tj
homephone:
name:
tj
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1000
workphone:
|_
----------
fullname:
Apache
gid:
48
groups:
- apache
home:
/usr/share/httpd
homephone:
name:
apache
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
48
workphone:
|_
----------
fullname:
gid:
1001
groups:
- tanjie
home:
/home/tanjie
homephone:
name:
tanjie
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1001
workphone:
user.chgid
更改用户GID
[root@master ~]# salt 'minion' cmd.run 'id tanjie'
minion:
uid=1001(tanjie) gid=1001(tanjie) groups=1001(tanjie)
[root@master ~]# salt 'minion' user.chgid tanjie 1000
minion:
True
[root@master ~]# salt 'minion' cmd.run 'id tanjie'
minion:
uid=1001(tanjie) gid=1000(tj) groups=1000(tj)
user.chuid
更改用户UID
[root@master ~]# salt 'minion' cmd.run 'id tanjie'
minion:
uid=1001(tanjie) gid=1000(tj) groups=1000(tj)
[root@master ~]# salt 'minion' user.chuid tanjie 6666
minion:
True
[root@master ~]# salt 'minion' user.chuid tanjie 6666
minion:
True
[root@master ~]# salt 'minion' cmd.run 'id tanjie'
minion:
uid=6666(tanjie) gid=1000(tj) groups=1000(tj)
user.list_groups
列出指定用户所属组的列表
[root@master ~]# salt 'minion' user.list_groups tanjie
minion:
- tj
user.rename
修改指定用户的用户名
[root@master ~]# salt 'minion' cmd.run 'id tanjie'
minion:
uid=6666(tanjie) gid=1000(tj) groups=1000(tj)
[root@master ~]# salt 'minion' user.rename tanjie jie
minion:
False
minion:
False #虽然返回的是False但是操作是成功了
[root@master ~]# salt 'minion' cmd.run 'id 6666'
minion:
uid=6666(jie) gid=1000(tj) groups=1000(tj)
user.delete
在minion端删除一个用户
[root@master ~]# salt 'minion' cmd.run 'id 6666'
minion:
uid=6666(jie) gid=1000(tj) groups=1000(tj)
[root@master ~]# salt 'minion' user.delete jie
minion:
True
[root@master ~]# salt 'minion' cmd.run 'id jie'
minion:
id: 'jie': no such user
ERROR: Minions returned with non-zero exit code
SaltStack常用模块之cp
- 查看关于cp模块的相关函数
[root@master ~]# salt 'minion' sys.list_functions cp
minion:
- cp.cache_dest
- cp.cache_dir
- cp.cache_file
- cp.cache_files
- cp.cache_local_file
- cp.cache_master
- cp.envs
- cp.get_dir
- cp.get_file
- cp.get_file_str
- cp.get_template
- cp.get_url
- cp.hash_file
- cp.is_cached
- cp.list_master
- cp.list_master_dirs
- cp.list_master_symlinks
- cp.list_minion
- cp.list_states
- cp.push
- cp.push_dir
- cp.recv
- cp.recv_chunked
- cp.stat_file
cp模块
客户端对文件服务器的操作都在cp模块中。Salt State系统,
也可使用Salt-cp代替
基本解释:
salt根目录:在master中 file_roots 定义的路径
例如:假设在master中有如下定义:
file_roots:
base:
- /srv/salt/base
那么:salt://指的实际路径是:/srv/salt/base。这样做的好处是,可以满足state系统中环境的概念。
cp.get_file
[root@master ~]# cd /srv/salt/base/
[root@master base]# ls
top.sls web
[root@master base]# salt 'minion' cp.get_file salt://top.sls /opt/
minion:
/opt/top.sls
[root@master base]# salt 'minion' cmd.run 'ls /opt'
minion:
top.sls
#如果客户端目录不在,则创建目录
[root@master base]# salt 'minion' cp.get_file salt://top.sls /test/ makedirs=True
minion:
/test/top.sls
注意:cp.get_file默认不在客户端上建立目录,如果客户端上没有这个目录,且未指定makedirs=True,文件拷贝将失败。
cp.get_url
从一个URL地址下载文件,URL可以是msater上的路径(salt://),也可以是http网址。
cp.push
从客户端传文件到master上,处于很明显的安全考虑,默认没有启用此功能’
开启方法如下
[root@master ~]# vim /etc/salt/master
file_recv: True
[root@master ~]# systemctl restart salt-master
SaltStack常用模块之salt-cp
salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt 'minion' cmd.run 'ls /usr/src/'
minion:
debug
kernels
[root@master ~]# salt-cp 'minion' /etc/passwd /usr/src/
minion:
----------
/usr/src/passwd:
True
[root@master ~]# salt 'minion' cmd.run 'ls /usr/src'
minion:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp 'minion' /root/anaconda-ks.cfg /etc/group /usr/src
minion:
----------
/usr/src/anaconda-ks.cfg:
True
/usr/src/group:
True
[root@master ~]# salt 'minion' cmd.run 'ls /usr/src'
minion:
anaconda-ks.cfg
debug
group
kernels
passwd
//在传输脚本时,是不会把执行权限传输过去
[root@master ~]# touch test.sh
[root@master ~]# vim test.sh
[root@master ~]# cat test.sh
#!/bin/bash
echo 'helo word'
[root@master ~]# salt-cp 'minion' /root/test.sh /opt
minion:
----------
/opt/test.sh:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
total 8
drwxr-xr-x 2 root root 6 Nov 4 04:21 123
-rw-r--r-- 1 root root 29 Nov 5 01:39 test.sh
-rw-r--r-- 1 root root 80 Nov 4 03:52 top.sls
[root@master ~]# chmod +x test.sh
[root@master ~]# ll /root/test.sh
-rwxr-xr-x. 1 root root 29 11月 5 01:39 /root/test.sh
[root@master ~]# salt-cp 'minion' /root/test.sh /opt
minion:
----------
/opt/test.sh:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
total 8
drwxr-xr-x 2 root root 6 Nov 4 04:21 123
-rw-r--r-- 1 root root 29 Nov 5 01:42 test.sh
-rw-r--r-- 1 root root 80 Nov 4 03:52 top.sls
SaltStack常用模块之file
file.access
检查指定路径是否存在
[root@master ~]# salt 'minion' cmd.run 'ls /usr/src'
minion:
anaconda-ks.cfg
debug
group
kernels
passwd
[root@master ~]# salt 'minion' file.access /usr/src/passwd f
minion:
True
[root@master ~]# salt 'minion' file.access /usr/src/abc f
minion:
False
检查指定文件的权限信息
[root@master ~]# salt 'minion' cmd.run 'ls -l /usr/src/'
minion:
total 12
-rw-r--r-- 1 root root 1435 Nov 5 01:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 12 2018 debug
-rw-r--r-- 1 root root 524 Nov 5 01:38 group
drwxr-xr-x. 2 root root 6 Aug 12 2018 kernels
-rw-r--r-- 1 root root 1191 Nov 5 01:37 passwd
[root@master ~]# salt 'minion' file.access /usr/src/passwd r #是否有读权限
minion:
True
[root@master ~]# salt 'minion' file.access /usr/src/passwd w #是否有写权限
minion:
True
[root@master ~]# salt 'minion' file.access /usr/src/passwd x #是否有执行权限
minion:
False
file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/tj'
minion:
-rw-r--r-- 1 root root 0 Nov 5 01:48 /root/tj
#添加有空格的内容,用引号引起来。(几对引号就添加几行)
[root@master ~]# salt 'minion' file.append /root/tj "hello world" "123"
minion:
Wrote 2 lines to "/root/tj"
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/tj'
minion:
-rw-r--r-- 1 root root 16 Nov 5 01:49 /root/tj
[root@master ~]# salt 'minion' cmd.run 'cat /root/tj'
minion:
hello world
123
file.basename
获取指定路径的基名
[root@master ~]# salt 'minion' file.basename '/root/test.sh/jjtt'
minion:
jjtt
file.dirname
获取指定路径的目录名
[root@master ~]# salt 'minion' file.dirname '/root/test.sh/jjtt'
minion:
/root/test.sh
file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt 'minion' cmd.run 'md5sum /etc/passwd'
minion:
ab5419e23346415175438cc27de6a949 /etc/passwd
[root@master ~]# salt 'minion' file.check_hash /etc/passwd ab5419e23346415175438cc27de6a949
minion:
True
file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件 不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@master ~]# salt 'minion' cmd.run 'lsattr /root/'
minion:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/ss
-------------------- /root/hh
//添加属性
[root@master ~]# salt 'minion' file.chattr /root/tj operator=add attributes=ai
minion:
True
[root@master ~]# salt 'minion' cmd.run 'lsattr /root/tj'
minion:
----ia-------------- /root/tj
给指定文件去除属性
[root@master ~]# salt 'minion' file.chattr /root/tj operator=remove attributes=ai
minion:
True
[root@master ~]# salt 'minion' cmd.run 'lsattr /root/tj'
minion:
-------------------- /root/tj
file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/tj'
minion:
-rw-r--r-- 1 root root 16 Nov 5 01:49 /root/tj
#属主、属组必须一致
[root@master ~]# salt 'minion' file.chown /root/tj tj tj
minion:
None
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/tj'
minion:
-rw-r--r-- 1 tj tj 16 Nov 5 01:49 /root/tj
file.copy
在远程主机上复制文件或目录
//拷贝文件
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 8
-rw-------. 1 root root 1435 Oct 15 02:20 anaconda-ks.cfg
-rw-r--r-- 1 tj tj 16 Nov 5 01:49 tj
[root@master ~]# salt 'minion' file.copy /root/tj /root/tjj
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
-rw-------. 1 root root 1435 Oct 15 02:20 anaconda-ks.cfg
-rw-r--r-- 1 tj tj 16 Nov 5 01:49 tj
-rw-r--r-- 1 tj tj 16 Nov 5 02:28 tjj
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt/'
minion:
total 8
drwxr-xr-x 2 root root 6 Nov 5 02:31 abc
root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
drwxr-xr-x 2 tj tj 6 Nov 5 02:31 abc
[root@master ~]# salt 'minion' file.copy /root/abc /opt/abc recurse=True
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
total 8
drwxr-xr-x 2 tj tj 6 Nov 5 02:31 abc
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt/'
minion:
total 8
drwxr-xr-x 2 root root 6 Nov 4 04:21 123
drwxr-xr-x 2 tj tj 6 Nov 5 02:31 abc
-rw-r--r-- 1 root root 29 Nov 5 01:42 test.sh
-rw-r--r-- 1 root root 80 Nov 4 03:52 top.sls
[root@master ~]# salt 'minion' file.copy /root/abc/ /opt/abc/ recurse=True remove_existing=True
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
total 8
drwxr-xr-x 2 root root 6 Nov 4 04:21 123
drwxr-xr-x 2 tj tj 6 Nov 5 02:31 abc
-rw-r--r-- 1 root root 29 Nov 5 01:42 test.sh
-rw-r--r-- 1 root root 80 Nov 4 03:52 top.sls
file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
drwxr-xr-x 2 tj tj 6 Nov 5 02:31 abc
-rw-------. 1 root root 1435 Oct 15 02:20 anaconda-ks.cfg
-rw-r--r-- 1 tj tj 16 Nov 5 01:49 tj
-rw-r--r-- 1 tj tj 16 Nov 5 02:28 tjj
[root@master ~]# salt 'minion' file.directory_exists /root/tj
minion:
False #返回False是因为ss是文件而非目录
[root@master ~]# salt 'minion' file.directory_exists /root/abc
minion:
True
file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt 'minion' cmd.run 'du -sb /etc'
minion:
21436648 /etc
[root@master ~]# salt 'minion' file.diskusage /etc
minion:
21710534
file.file_exists
判断指定文件是否存在
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 8
drwxr-xr-x. 2 root root 20 Nov 4 19:05 abc
-rw-------. 1 root root 1184 Jul 12 15:19 anaconda-ks.cfg
-rw-r--r--. 1 root root 23 Nov 4 18:52 hhhh
-rw-r--r--. 1 ran ran 0 Nov 3 22:14 ss
[root@master ~]# salt 'minion' file.file_exists /root/tj
minion:
True
[root@master ~]# salt 'minion' file.file_exists /root/abc
minion:
False #返回False是因为abc是目录而非文件
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
12
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
示例:
//查找类型为普通文件,以.log结尾的,大小在10以上的文件
[root@master ~]# salt 'minion' file.find / type=f name=\*.log size=+1m
//在var目录下查询修改后的时间在10天前,大小在10M以上的文件,打印路径,文件大小,修改后的时间
[root@master ~]# salt 'minion' file.find /var mtime=+10d size=+10m print=path,size,mtime
//在/var/log下查找名字以.任意数字的且30天前修改的,大于10m的文件,删除
[root@master ~]# salt 'minion' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete
file.get_gid
获取指定文件的gid
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/tjj'
minion:
-rw-r--r-- 1 tj tj 16 Nov 5 02:28 /root/tjj
[root@master ~]# salt 'minion' cmd.run 'id tj'
minion:
uid=1000(tj) gid=1000(tj) groups=1000(tj)
[root@master ~]# salt 'minion' file.get_gid /root/tjj
minion:
1000
file.get_group
获取指定文件的组名
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/tjj'
minion:
-rw-r--r-- 1 tj tj 16 Nov 5 02:28 /root/tjj
[root@master ~]# salt 'minion' file.get_group /root/tjj
minion:
tj
file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt 'minion' cmd.run 'sha256sum /root/tjj'
minion:
a7e70eda397c46d76edc16c9b3e8e1c18ed9e81f6f597f4e32d814fb9085ebcc /root/tjj
[root@master ~]# salt 'minion' file.get_hash /root/tjj
minion:
a7e70eda397c46d76edc16c9b3e8e1c18ed9e81f6f597f4e32d814fb9085ebcc
file.get_mode
获取指定文件的权限,以数字方式显示
//获取文件权限
[root@master ~]# salt 'minion' file.get_mode /root/tjj
minion:
0644
//获取目录的权限
[root@master ~]# salt 'minion' file.get_mode /root/abc
minion:
0755
file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt 'minion' cmd.run 'ls -Z /root/anaconda-ks.cfg'
minion:
system_u:object_r:admin_home_t:s0 /root/anaconda-ks.cfg
[root@master ~]# salt 'minion' file.get_selinux_context /root/anaconda-ks.cfg
minion:
system_u:object_r:admin_home_t:s0
file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt 'minion' cmd.run 'sha256sum /root/tjj'
minion:
a7e70eda397c46d76edc16c9b3e8e1c18ed9e81f6f597f4e32d814fb9085ebcc /root/tjj
[root@master ~]# salt 'minion' file.get_sum /root/tjj
minion:
a7e70eda397c46d76edc16c9b3e8e1c18ed9e81f6f597f4e32d814fb9085ebcc
file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 12
-rw-r--r-- 1 tj tj 16 Nov 5 02:28 tjj
[root@master ~]# salt 'minion' file.get_uid /root/tjj
minion:
1000
[root@master ~]# salt 'minion' file.get_user /root/tjj
minion:
tj
file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt 'minion' file.gid_to_group 1000
minion:
tj
[root@master ~]# salt 'minion' file.gid_to_group 0
minion:
root
file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt 'minion' file.group_to_gid root
minion:
0
[root@master ~]# salt 'minion' file.group_to_gid tj
minion:
1000
file.grep
在指定文件中检索指定内容
该函数支持通配符
,若在指定的路径中用通配符则必须用双引号引起来
#匹配关键字nobody
[root@master ~]# salt 'minion' file.grep /etc/passwd nobody
minion:
----------
pid:
168514
retcode:
0
stderr:
stdout:
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
//匹配ifcfg-ens160中的ipaddr,且忽略大小写
[root@master ~]# salt 'minion' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i
minion:
----------
pid:
169107
retcode:
0
stderr:
stdout:
IPADDR=192.168.47.120
-B2 #查找当前行的前两行(包括当前行)
[root@master ~]# salt 'minion' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -B2
minion:
----------
pid:
172269
retcode:
0
stderr:
stdout:
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.47.120
-A2 #查找当前行的后两行(包括当前行)
[root@master ~]# salt 'minion' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -A2
minion:
----------
pid:
173683
retcode:
0
stderr:
stdout:
IPADDR=192.168.47.120
GATEWAY=192.168.47.2
PREFIX=24
-C2 #查找当前行的前后两行(包括当前行)
[root@master ~]# salt 'minion' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -C2
minion:
----------
pid:
174444
retcode:
0
stderr:
stdout:
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.47.120
GATEWAY=192.168.47.2
PREFIX=24
#查看当前行的路径是在哪里
[root@master ~]# salt 'minion' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
minion:
----------
pid:
175245
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-ens160
file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt 'minion' cmd.run 'ls -l /dev/sr0'
minion:
brw-rw---- 1 root cdrom 11, 0 Nov 5 01:34 /dev/sr0
[root@master ~]# salt 'minion' file.is_blkdev /dev/sr0
minion:
True
file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt 'minion' cmd.run 'lsattr /root/tj'
minion:
-------------------- /root/tj
[root@master ~]# salt 'minion' cmd.run 'chattr +i /root/tj'
minion:
[root@master ~]# salt 'minion' cmd.run 'lsattr /root/tj'
minion:
----i--------------- /root/tj
[root@master ~]# salt 'minion' file.lsattr /root/tj
minion:
----------
/root/tj:
- i
file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
drwxr-xr-x 2 tj tj 6 Nov 5 02:31 abc
-rw-------. 1 root root 1435 Oct 15 02:20 anaconda-ks.cfg
-rw-r--r-- 1 tj tj 16 Nov 5 01:49 tj
-rw-r--r-- 1 tj tj 16 Nov 5 02:28 tjj
[root@master ~]# salt 'minion' file.mkdir /root/123
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
drwxr-xr-x 2 root root 6 Nov 5 03:25 123
drwxr-xr-x 2 tj tj 6 Nov 5 02:31 abc
-rw-------. 1 root root 1435 Oct 15 02:20 anaconda-ks.cfg
-rw-r--r-- 1 tj tj 16 Nov 5 01:49 tj
-rw-r--r-- 1 tj tj 16 Nov 5 02:28 tjj
[root@master ~]# salt 'minion' file.mkdir /root/helo root tj 2775
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 12
drwxrwsr-x 2 root tj 6 Nov 5 03:28 helo