1. SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
1.官方文档
2.官方常用模块
3.官方状态模块
4.官方状态关系语句
5.file状态模块
[root@master ~]# salt-key -L
Accepted Keys:
node1
Denied Keys:
Unaccepted Keys:
master
node3
Rejected Keys:
minion
[root@master ~]# salt '*' test.ping
node1:
True
//查看所有module列表
[root@master ~]# salt '*' sys.list_modules
node1:
- acl
- aliases
- alternatives
- archive
- artifactory
- baredoc
- beacons
- bigip
- btrfs
- buildout
- chroot
- cloud
- cmd
- composer
- config
- consul
- container_resource
- cp
...省略
[root@master ~]# salt '*' sys.list_modules|wc -l
169
//查看指定module的所有function
[root@master ~]# salt '*' sys.list_functions cmd
node1:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.powershell
- cmd.powershell_all
- cmd.retcode
- cmd.run
- cmd.run_all
- cmd.run_bg
- cmd.run_chroot
- cmd.run_stderr
- cmd.run_stdout
- cmd.script
- cmd.script_retcode
- cmd.shell
- cmd.shell_info
- cmd.shells
- cmd.tty
- cmd.which
- cmd.which_bin
[root@master ~]# salt '*' sys.doc cmd|less
。。。//找Example实例,也可以访问,官方模块文档
cmd.shells:
Lists the valid shells on this system via the /etc/shells file
New in version 2015.5.0
CLI Example:
salt '*' cmd.shells
cmd.tty:
Echo a string to a specific tty
CLI Example:
salt '*' cmd.tty tty0 'This is a test'
salt '*' cmd.tty pts3 'This is a test'
。。。
[root@master ~]# salt '*' test.echo,cmd.run,service.status 你好,date,sshd
node1:
----------
cmd.run:
Wed Nov 3 03:29:34 EDT 2021 //时间
service.status: //状态
True
test.echo: //打印
2. SaltStack常用模块
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
//返回所有活动的tcp连接
[root@master ~]# salt '*' network.active_tcp
node1:
----------
0:
----------
local_addr:
192.168.143.102
local_port:
22
remote_addr:
192.168.143.1
remote_port:
4315
1:
----------
local_addr:
192.168.143.102
local_port:
36970
remote_addr:
192.168.143.101
remote_port:
4505
2:
----------
local_addr:
192.168.143.102
local_port:
22
remote_addr:
192.168.143.1
remote_port:
4314
//此三个对应对面的三个
[root@node1 ~]# ss -ant
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
ESTAB 0 0 192.168.143.102:22 //这里 192.168.143.1:4315
ESTAB 0 0 192.168.143.102:36970 //这里 //192.168.143.101:4505
ESTAB 0 36 192.168.143.102:22 //这里 192.168.143.1:4314
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22
[::]:
//对面开启窗口添加一个,22端口号 此时发现有5个连接
[root@master ~]# salt '*' network.active_tcp
node1:
----------
0:
----------
local_addr:
192.168.143.102
local_port:
22
remote_addr:
192.168.143.1
remote_port:
6689
1:
----------
local_addr:
192.168.143.102
local_port:
22
remote_addr:
192.168.143.1
remote_port:
4315
2:
----------
local_addr:
192.168.143.102
local_port:
22
remote_addr:
192.168.143.1
remote_port:
6687
3:
----------
local_addr:
192.168.143.102
local_port:
36970
remote_addr:
192.168.143.101
remote_port:
4505
4:
----------
local_addr:
192.168.143.102
local_port:
22
remote_addr:
192.168.143.1
remote_port:
4314
2.1.2 network.calc_net
//通过IP和子网掩码计算出网段
[root@master ~]# salt '*' network.calc_net 192.168.143.2 255.255.255.0
node1:
192.168.143.0/24
2.1.3 network.connect
//测试minion至某一台服务器的网络是否连通
[root@master ~]# salt '*' network.connect www.sougou.com 80
node1:
----------
comment:
Successfully connected to www.sougou.com (221.122.82.30) on tcp port 80
result:
True
//测试二台主机是否想通
[root@master ~]# salt '*' network.connect 192.168.143.101 22
node1:
----------
comment:
Successfully connected to 192.168.143.101 (192.168.143.101) on tcp port 22
result:
True
2.1.4 network.default_route
//查看默认路由
[root@master ~]# salt '*' network.default_route
node1:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.143.2
interface:
ens33
netmask:
0.0.0.0
|_
----------
addr_family:
inet6
destination:
::/0
flags:
!n
gateway:
::
interface:
lo
netmask:
|_
----------
addr_family:
inet6
destination:
::/0
flags:
!n
gateway:
::
interface:
lo
netmask:
2.1.5 network.get_fqdn
//查看主机的fqdn(完全限定域名)
[root@master ~]# salt '*' network.get_fqdn
node1:
node1
//修改主机名后查看主机fqdn
[root@node1 ~]# hostname node1.example.com
[root@node1 ~]# bash
[root@node1 ~]# hostname
node1.example.com
[root@master ~]# salt '*' network.get_fqdn
node1:
node1.example.com
2.1.6 network.get_hostname
//获取主机名与上面类似
[root@master ~]# salt '*' network.get_hostname
node1:
node1
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt '*' network.get_route 192.168.162.216
node1:
----------
destination: //到这里去
192.168.162.216
gateway:
192.168.143.2 //走的网关
interface:
ens33
source:
192.168.143.102 //node1主机ip
[root@master ~]# salt '*' network.get_route 0.0.0.0
node1:
----------
destination:
0.0.0.0
gateway:
None
interface:
lo
source:
127.0.0.1
2.1.8 network.hw_addr
//返回指定网卡的MAC地址
[root@master ~]# salt '*' network.hw_addr ens33
node1:
00:0c:29:07:05:66
2.1.9 network.ifacestartswith
//从特定CIDR检索接口名称
[root@master ~]# salt '*' network.ifacestartswith 192.168
node1:
- ens33
[root@master ~]# salt '*' network.ifacestartswith 127
node1:
- lo
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt '*' network.in_subnet 192.168.0.0/16
node1:
True
[root@master ~]# salt '*' network.in_subnet 172.16.0.0/16
node1:
False
2.1.11 network.interface
//返回指定网卡的信息
[root@master ~]# salt '*' network.interface ens33
node1:
|_
----------
address:
192.168.143.102
broadcast:
192.168.143.255
label:
ens33
netmask:
255.255.255.0
2.1.12 network.interface_ip
//返回指定网卡的IP地址
[root@master ~]# salt '*' network.interface_ip ens33
node1:
192.168.143.102
2.1.13 network.interfaces
//返回当前系统中所有的网卡信息
[root@master ~]# salt '*' network.interfaces
node1:
----------
ens33:
----------
hwaddr:
00:0c:29:07:05:66
inet:
|_
----------
address:
192.168.143.102
broadcast:
192.168.143.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:fe07:566
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的地址列表
[root@master ~]# salt '*' network.ipaddrs
node1:
- 192.168.143.102
2.1.15 network.netstat
//返回所有打开的端口和状态
[root@master ~]# salt '*' network.netstat
node1:
|_
----------
inode:
28017
local-address:
0.0.0.0:80
program:
967/nginx:
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
27684
local-address:
0.0.0.0:22
program:
909/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
30092
local-address:
192.168.143.102:22
program:
1491/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.143.1:4315
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
30859
local-address:
192.168.143.102:36970
program:
1376/python3.6
proto:
tcp
recv-q:
0
remote-address:
192.168.143.101:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
29400
local-address:
192.168.143.102:22
program:
1375/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.143.1:4314
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
28018
local-address:
:::80
program:
967/nginx:
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
27695
local-address:
:::22
program:
909/sshd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
25874
local-address:
127.0.0.1:323
program:
892/chronyd
proto:
udp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
user:
0
|_
----------
inode:
25875
local-address:
::1:323
program:
892/chronyd
proto:
udp6
recv-q:
0
remote-address:
:::*
send-q:
0
user:
0
2.1.16 network.ping
//使用ping命令测试到某主机的连通性
[root@master ~]# salt '*' network.ping baidu.com
node1:
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=70.4 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=128 time=89.5 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=128 time=59.6 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=128 time=53.8 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 53.766/68.314/89.495/13.603 ms
2.1.17 network.reverse_ip
//返回一个指定的IP地址的反向地址
[root@master ~]# salt '*' network.reverse_ip 192.168.143.1
node1:
1.143.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
//判断指定的服务是否可用
[root@master ~]# salt '*' service.available sshd
node1:
True
[root@master ~]# salt '*' service.available httpd
node1:
False
[root@master ~]# salt '*' service.available nginx
node1:
True
2.2.2 service.get_all
//获取所有正在运行的服务
[root@master ~]# salt '*' service.get_all
node1:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- boot-complete.target
- chrony-dnssrv@
- chrony-dnssrv@.timer
- chrony-wait
- chronyd
。。。省略
2.2.3 service.disabled
//是否是disabled状态
[root@master ~]# salt '*' service.disabled nginx
node1:
False
2.2.4 service.enabled
//是否是enabled状态
[root@master ~]# salt '*' service.enabled nginx
node1:
True
2.2.5 service.disable
//设置diable状态
[root@master ~]# salt '*' service.disable nginx
node1:
True
//node1主机状态
[root@node1 ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disable> //状态为disable
Active: active (running) since Wed 2021-11-03 03:12:04 EDT; 1h>
Main PID: 967 (nginx)
Tasks: 2 (limit: 11208)
Memory: 11.1M
CGroup: /system.slice/nginx.service
├─967 nginx: master process /usr/sbin/nginx
└─969 nginx: worker process
11月 03 03:12:02 node1 systemd[1]: Starting The nginx HTTP and re>
11月 03 03:12:03 node1 nginx[917]: nginx: the configuration file >
11月 03 03:12:03 node1 nginx[917]: nginx: configuration file /etc>
11月 03 03:12:04 node1 systemd[1]: Started The nginx HTTP and rev>
lines 1-14/14 (END)
2.2.6 service.enable
//设置enable状态
[root@master ~]# salt '*' service.enable nginx
node1:
True
//node1主机状态
[root@node1 ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled> //状态为eanbled
Active: active (running) since Wed 2021-11-03 03:12:04 EDT; 1h>
Main PID: 967 (nginx)
Tasks: 2 (limit: 11208)
Memory: 11.1M
CGroup: /system.slice/nginx.service
├─967 nginx: master process /usr/sbin/nginx
└─969 nginx: worker process
11月 03 03:12:02 node1 systemd[1]: Starting The nginx HTTP and re>
11月 03 03:12:03 node1 nginx[917]: nginx: the configuration file >
11月 03 03:12:03 node1 nginx[917]: nginx: configuration file /etc>
11月 03 03:12:04 node1 systemd[1]: Started The nginx HTTP and rev>
lines 1-14/14 (END)
2.2.7 service.reload
//重新加载指定服务
[root@master ~]# salt '*' service.reload nginx
node1:
True
2.2.8 service.stop
//停止指定服务
[root@master ~]# salt '*' service.stop nginx
node1:
True
2.2.9 service.start
//开启指定服务
[root@master ~]# salt '*' service.start nginx
node1:
True
2.2.10 service.restart
//重启指定服务
[root@master ~]# salt '*' service.restart nginx
node1:
True
[root@master ~]#
2.2.11 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
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
//只下载软件包,不安装
[root@master ~]# salt '*' pkg.download wget
node1:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm //下载的位置
2.3.2 pkg.file_list
//查看软件安装的文件信息
[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
2.3.3 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
2.3.4 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
2.3.5 pkg.install
//安装软件包
[root@master ~]# salt '*' cmd.run 'rpm -q wget'
node1:
package wget is not installed
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt '*' pkg.install wget
node1:
----------
wget:
----------
new:
1.19.5-10.el8
old:
[root@master ~]# salt '*' cmd.run 'rpm -q wget'
node1:
wget-1.19.5-10.el8.x86_64
[root@master ~]#
2.3.6 pkg.list_downloaded
//列出本地下载的包
[root@master ~]# salt '*' pkg.list_downloaded
node1:
----------
2.3.7 pkg.list_pkgs
//以字典的方式列出当前已安装的软件包
[root@master ~]# salt '*' pkg.list_pkgs
node1:
----------
NetworkManager:
1:1.30.0-7.el8
NetworkManager-libnm:
1:1.30.0-7.el8
NetworkManager-team:
1:1.30.0-7.el8
NetworkManager-tui:
1:1.30.0-7.el8
acl:
2.2.53-1.el8
audit:
3.0-0.17.20191104git1c2f876.el8
audit-libs:
3.0-0.17.20191104git1c2f876.el8
。。。省略
[root@master ~]# salt '*' pkg.list_pkgs|wc -l
982
2.3.8 pkg.owner
//列出指定文件是由哪个包提供的
[root@master ~]# salt '*' pkg.owner /usr/bin/wget
node1:
wget
[root@master ~]# salt '*' pkg.owner /usr/bin/passwd
node1:
passwd
[root@master ~]# salt '*' pkg.owner /usr/bin/passw
node1:
ERROR: Minions returned with non-zero exit code
2.3.9 pkg.remove
[root@master ~]# salt '*' pkg.remove wget
node1:
----------
wget:
----------
new:
old:
1.19.5-10.el8
[root@master ~]# salt '*' cmd.run 'rpm -q wget'
node1:
package wget is not installed
ERROR: Minions returned with non-zero exit code
[root@master ~]#
2.3.10 pkg.upgrade
//假设有旧版本
[root@node1 ~]# ls
anaconda-ks.cfg wget-1.14-18.el7_6.1.x86_64.rpm
[root@node1 ~]# rm -rf wget-1.14-18.el7_6.1.aarch64.rpm
[root@node1 ~]# rpm -ivh wget-1.14-18.el7_6.1.x86_64.rpm --nodeps
警告:wget-1.14-18.el7_6.1.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...
1:wget-1.14-18.el7_6.1 ################################# [100%]
[root@node1 ~]# rpm -qa|grep wget
wget-1.14-18.el7_6.1.x86_64
//升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt '*' pkg.upgrade wget
node1:
----------
wget:
----------
new:
1.19.5-10.el8
old:
1.14-18.el7_6.1
[root@master ~]# salt '*' cmd.run 'rpm -q wget'
node1:
wget-1.19.5-10.el8.x86_64
2.3.11pkg.group_install
//安装包组
[root@master ~]# salt '*' pkg.group_install 'Development Tools'
node1:
----------
adobe-mappings-cmap:
----------
new:
20171205-3.el8
old:
adobe-mappings-cmap-deprecated:
----------
new:
20171205-3.el8
old:
adobe-mappings-pdf:
----------
new:
20180407-1.el8
old:
annobin:
----------
new:
9.50-1.el8
old:
asciidoc:
----------
new:
8.6.10-0.5.20180627gitf7c2274.el8
old:
。。。省略
2.3.12pkg.modified
//指定软件配置文件发生改变
[root@node1 ~]# echo '###' >> /etc/nginx/nginx.conf
[root@master ~]# salt '*' pkg.modified nginx
node1:
----------
/etc/nginx/nginx.conf:
- size
- checksum
- time
- config
ERROR: Minions returned with non-zero exit code
2.3.13pkg.version
//查看软件版本号
[root@master ~]# salt '*' pkg.version nginx
node1:
1:1.14.1-9.module_el8.0.0+184+e34fea82
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
//显示当前系统中有哪些高级状态
[root@master ~]# salt '*' state.show_highstate
node1:
----------
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.install
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10000
nginx-service:
----------
__env__:
base
__sls__:
web.nginx.install
service:
|_
----------
name:
nginx
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate
//执行高级状态
[root@master ~]# salt '*' state.highstate web.apache.install
node1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 05:30:52.058240
Duration: 1083.848 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: The service nginx is already running
Started: 05:30:53.148174
Duration: 54.081 ms
Changes:
Summary for node1
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.138 s
[root@master ~]#
2.4.3 state.show_state_usage 与state.show_states
//显示当前系统中的高级状态执行情况
[root@master ~]# salt '*' state.show_state_usage
node1:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.apache.apache
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:
//返回将应用于Highstate的状态列表。
[root@master salt]# salt '*' state.show_states
node1:
- web.nginx.install
2.4.4 state.show_top
//返回minion将用于highstate的顶级数据
[root@master ~]# salt '*' state.show_top
node1:
----------
base:
- web.nginx.install
2.4.5 state.top
//执行指定的top file,而不是默认的
[root@master ~]# salt '*' state.top xxx.sls
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:17:20.129390
Duration: 917.659 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 20:17:21.048144
Duration: 46.958 ms
Changes:
Summary for 192.168.69.202
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 964.617 ms
2.4.6 state.show_sls
//显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt '*' 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常用模块之useradd
2.5.0 useradd.add
//添加用户
[root@master ~]# salt '*' user.add foo
node1:
True
2.5.1useradd.chfullname
//更改用户的描述
[root@master ~]# salt '*' user.chfullname foo "Foo Bar"
node1:
True
[root@node1 ~]# cat /etc/passwd
foo:x:1001:1001:Foo Bar:/home/foo:/bin/bash
2.5.2useradd.chgid
//添加指定用户的 uid
[root@master ~]# salt '*' user.chgid foo 1000
node1:
True
[root@node1 ~]# cat /etc/passwd
foo:x:1001:1000:Foo Bar:/home/foo:/bin/bash
2.5.3useradd.chgroups
//更改属组
[root@master ~]# salt '*' user.chgroups foo wheel,root
node1:
True
[root@node1 ~]# id foo
uid=1001(foo) gid=1000(admin) 组=1000(admin),10(wheel),0(root)
2.5.4useradd.chhome
//更改用户的主目录,如果旧的主目录存在,则为持久传递 True 以将文件移动到新的主目录。
[root@master ~]# salt '*' user.chhome foo /home/users/foo True
node1:
True
[root@node1 ~]# cat /etc/passwd
foo:x:1001:1000:Foo Bar:/home/users/foo:/bin/bash
2.5.5useradd.chhomephone
//更改用户的家庭电话
[root@master ~]# salt '*' user.chhomephone foo 7735551234
node1:
True
[root@node1 ~]# cat /etc/passwd
foo:x:1001:1000:Foo Bar,,,7735551234:/home/users/foo:/bin/bash
2.5.6useradd.chloginclass
//更改用户的默认登录类别
[root@master ~]# salt '*' user.chloginclass foo staff
node1:
False
2.5.7useradd.chother
//更改用户的其他 GECOS 属性
salt '*' user.chother foobar
2.5.8useradd.chroomnumber
//更改用户的房间号
[root@master ~]# salt '*' user.chroomnumber foo 123
node1:
True
[root@node1 ~]# cat /etc/passwd
foo:x:1001:1000:Foo Bar,123,,7735551234:/home/users/foo:/bin/bash
2.5.9useradd.chshell
//更改用户的默认shell
[root@master ~]# salt '*' user.chshell foo /bin/zsh
node1:
True
[root@node1 ~]# cat /etc/passwd
foo:x:1001:1000:Foo Bar,123,,7735551234:/home/users/foo:/bin/zsh
2.5.10useradd.chuid
//更改指定用户的 uid
[root@master ~]# salt '*' user.chuid foo 4376
node1:
True
[root@node1 ~]# cat /etc/passwd
foo:x:4376:1000:Foo Bar,123,,7735551234:/home/users/foo:/bin/zsh
2.5.11useradd.chworkphone
//添加用户的工作电话
[root@master ~]# salt '*' user.chworkphone foo 7735550123
node1:
True
[root@node1 ~]# cat /etc/passwd
foo:x:4376:1000:Foo Bar,123,7735550123,7735551234:/home/users/foo:/bin/zsh
2.5.12useradd.delete
//从minion中删除一个用户
[root@master ~]# salt '*' user.delete admin remove=True force=True
node1:
True
[root@node1 ~]# id admin
id: “admin”:无此用户
2.5.13useradd.get_loginclass
//获取用户的登录类 此功能仅适用于 OpenBSD 系统。
[root@master ~]# salt '*' user.get_loginclass foo
node1:
False
2.5.14useradd.getent
//返回所有用户的所有信息列表
[root@master ~]# salt '*' user.getent
node1:
|_
----------
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
。。。。省略
2.5.15useradd.info
//返回用户信息
[root@master ~]# salt '*' user.info root
node1:
----------
fullname:
root
gid:
0
groups:
- root
home:
/root
homephone:
name:
root
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
0
workphone:
2.5.16useradd.list_groups
//返回指定用户所属的组列表
[root@master ~]# salt '*' user.list_groups foo
node1:
- root
- wheel
2.5.17useradd.list_users
//返回所有用户的列表
[root@master ~]# salt '*' user.list_users
node1:
- adm
- apache
- bin
- chrony
- daemon
- dbus
- foo
- ftp
- games
- halt
- lp
- mail
- nginx
- nobody
- operator
- pesign
- polkitd
- root
- shutdown
- sshd
- sssd
- sync
- systemd-coredump
- systemd-resolve
- tss
- unbound
2.5.18useradd.primary_group
//返回指定用户的主要组
[root@master ~]# salt '*' user.primary_group root
node1:
root
2.5.19useradd.rename
//更改用户的用户名
[root@master ~]# salt '*' user.rename foo iii
node1:
False
[root@node1 ~]# cat /etc/passwd
iii:x:4376:1000:Foo Bar,123,7735550123,7735551234:/home/users/foo:/bin/zsh
2.6 SaltStack常用模块之salt-cp
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt-key -L
Accepted Keys:
node1
node2
Denied Keys:
Unaccepted Keys:
master
node3
node4
node5
Rejected Keys:
[root@master ~]# salt '*' test.ping
node1:
True
node2:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src/'
node1:
debug
kernels
node2:
debug
kernels
[root@master ~]# salt-cp '*' /etc/passwd /usr/src/
node1:
----------
/usr/src/passwd:
True
node2:
----------
/usr/src/passwd:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src/'
node1:
debug
kernels
passwd
node2:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp '*' /etc/shadow /etc/group /usr/src/
node1:
----------
/usr/src/group:
True
/usr/src/shadow:
True
node2:
----------
/usr/src/group:
True
/usr/src/shadow:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src/'
node1:
debug
group
kernels
passwd
shadow
node2:
debug
group
kernels
passwd
shadow
[root@master ~]# vim /root/test.sh
#!/bin/bash
echo "How are you "
[root@master ~]# salt-cp '*' /root/test.sh /usr/src/
node1:
----------
/usr/src/test.sh:
True
node2:
----------
/usr/src/test.sh:
True
[root@master ~]# salt '*' cmd.run '/bin/bash /usr/src/test.sh'
node1:
How are you
node2:
How are you
2.7 SaltStack常用模块之file
2.7.1 file.access
//检查指定路径是否存在
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
debug
group
kernels
passwd
shadow
test.sh
node2:
debug
group
kernels
passwd
shadow
test.sh
[root@master ~]# salt '*' file.access /usr/src/passwd f
node1:
True
node2:
True
[root@master ~]# salt '*' file.access /usr/src/pass f
node1:
False
node2:
False
//检查指定文件的权限信息
[root@master ~]# salt '*' cmd.run 'ls -l /usr/src/'
node1:
total 16
drwxr-xr-x. 2 root root 6 Nov 3 2020 debug
-rw-r--r-- 1 root root 501 Nov 4 05:13 group
drwxr-xr-x. 3 root root 42 Nov 3 05:18 kernels
-rw-r--r-- 1 root root 1044 Nov 4 05:11 passwd
-rw-r--r-- 1 root root 661 Nov 4 05:13 shadow
-rw-r--r-- 1 root root 33 Nov 4 05:17 test.sh
node2:
total 16
drwxr-xr-x. 2 root root 6 Nov 3 2020 debug
-rw-r--r-- 1 root root 501 Nov 4 05:13 group
drwxr-xr-x. 2 root root 6 Nov 3 2020 kernels
-rw-r--r-- 1 root root 1044 Nov 4 05:11 passwd
-rw-r--r-- 1 root root 661 Nov 4 05:13 shadow
-rw-r--r-- 1 root root 33 Nov 4 05:17 test.sh
[root@master ~]# salt '*' file.access /usr/src/passwd r
node1:
True
node2:
True
[root@master ~]# salt '*' file.access /usr/src/passwd w
node1:
True
node2:
True
[root@master ~]# salt '*' file.access /usr/src/passwd x
node1:
False
node2:
False
2.7.2 file.append
//往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt '*' cmd.run 'touch a'
node1:
node2:
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
node1:
-rw-r--r-- 1 root root 0 Nov 4 05:26 /root/a
node2:
-rw-r--r-- 1 root root 0 Nov 4 05:26 /root/a
[root@master ~]# salt '*' file.append /root/a "hello world" "goold "hh"
> ^C
[root@master ~]# salt '*' file.append /root/a "hello world" "haha" "xixi"
node1:
Wrote 3 lines to "/root/a"
node2:
Wrote 3 lines to "/root/a"
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
node1:
-rw-r--r-- 1 root root 22 Nov 4 05:28 /root/a
node2:
-rw-r--r-- 1 root root 22 Nov 4 05:28 /root/a
[root@master ~]# salt '*' cmd.run 'cat /root/a'
node1:
hello world
haha
xixi
node2:
hello world
haha
xixi
[root@master ~]# salt '*' file.append /root/b "hello world" "haha" "xixi"
//文件不存在绿色乱码错误
2.7.3 file.basename
//获取指定路径的基名
[root@master ~]# salt '*' cmd.run 'mkdir -p /zabbix/adc'
node1:
node2:
[root@master ~]# salt '*' file.basename '/root/zabbix/abc'
node1:
abc
node2:
abc
2.7.4 file.dirname
//获取指定路径的目录名
[root@master ~]# salt '*' file.dirname '/root/zabbix/abc'
node1:
/root/zabbix
node2:
/root/zabbix
2.6.5 file.check_hash
//检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt '*' cmd.run 'md5sum /etc/passwd'
node1:
265e30e33ee9b7029f3e29577ec5e794 /etc/passwd
node2:
a3ea4aa72ece9c65fd91812d77cef9e3 /etc/passwd
[root@master ~]# salt '*' file.check_hash /etc/passwd 265e30e33ee9b7029f3e29577ec5e794
node1:
True
node2:
False
[root@master ~]# salt '*' file.check_hash /etc/passwd a3ea4aa72ece9c65fd91812d77cef9e3
node2:
True
node1:
False
2.7.6 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件, 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改, 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件, 不允许建立和删除文件 |
[root@master ~]# salt '*' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/wget-1.14-18.el7_6.1.x86_64.rpm
-------------------- /root/a
node2:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/a
[root@master ~]# salt '*' file.chattr /root/a operator=add attributes=ai
node2:
True
node1:
True
[root@master ~]# salt '*' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/wget-1.14-18.el7_6.1.x86_64.rpm
----ia-------------- /root/a
node2:
-------------------- /root/anaconda-ks.cfg
----ia-------------- /root/a
[root@master ~]#
//给指定文件去除属性
[root@master ~]# salt '*' file.chattr /root/a operator=remove attributes=ia
node2:
True
node1:
True
[root@master ~]# salt '*' cmd.run 'lsattr /root'
node2:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/a
node1:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/wget-1.14-18.el7_6.1.x86_64.rpm
-------------------- /root/a
//给指定目录添加属性
[root@master ~]# salt '*' file.chattr /root operator=add attributes=ai
node2:
True
node1:
True
[root@master ~]# salt '*' cmd.run 'lsattr -d /root'
node1:
----ia-------------- /root
node2:
----ia-------------- /root
//给指定目录去除属性
[root@master ~]# salt '*' file.chattr /root operator=remove attributes=ai
node2:
True
node1:
True
[root@master ~]# salt '*' cmd.run 'lsattr -d /root'
node2:
-------------------- /root
node1:
-------------------- /root
2.7.7 file.chown
//设置指定文件的属主、属组信息
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node2:
total 8
-rw-r--r-- 1 root root 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
node1:
total 556
-rw-r--r-- 1 root root 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt '*' file.chown /root/a apache apache
node2:
None
node1:
None
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
total 556
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
node2:
total 8
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
[root@master ~]#
2.7.8 file.copy
//在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node2:
total 8
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
node1:
total 556
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt '*' file.copy /root/a /root/bb
node2:
True
node1:
True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node2:
total 12
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
node1:
total 560
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]#
//覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# mkdir hy
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node2:
total 12
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
node1:
total 560
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt '*' file.copy /tmp/ /root/hy recurse=True
node2:
True
node1:
True
[root@master ~]# salt '*' cmd.run 'ls /root/hy'
node2:
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-chronyd.service-5MOBSi
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-httpd.service-DEE53i
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-mariadb.service-VZ8Bog
vmware-root_870-2731086752
vmware-root_880-2697139639
vmware-root_896-2730693439
node1:
hsperfdata_root
systemd-private-1eaed0dcf64f479c83eecdb8db121adf-chronyd.service-IaGU7i
systemd-private-1eaed0dcf64f479c83eecdb8db121adf-nginx.service-8txDZg
vmware-root_873-4013854327
vmware-root_876-2689209388
vmware-root_879-4013723248
vmware-root_884-2688554034
vmware-root_887-4013330030
vmware-root_897-3979643105
[root@master ~]#
//删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt '*' cmd.run 'ls /root/hy/'
node2:
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-chronyd.service-5MOBSi
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-httpd.service-DEE53i
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-mariadb.service-VZ8Bog
vmware-root_870-2731086752
vmware-root_880-2697139639
vmware-root_896-2730693439
node1:
hsperfdata_root
systemd-private-1eaed0dcf64f479c83eecdb8db121adf-chronyd.service-IaGU7i
systemd-private-1eaed0dcf64f479c83eecdb8db121adf-nginx.service-8txDZg
vmware-root_873-4013854327
vmware-root_876-2689209388
vmware-root_879-4013723248
vmware-root_884-2688554034
vmware-root_887-4013330030
vmware-root_897-3979643105
[root@master ~]# salt '*' cmd.run 'mkdir /opt/yy'
node2:
node1:
//拷贝目录
[root@master ~]# salt '*' file.copy /root/hy/ /opt/yy/ recurse=True remove_existing=True
node1:
True
node2:
True
[root@master ~]# salt '*' cmd.run 'ls /opt/yy/'
node1:
hsperfdata_root
systemd-private-1eaed0dcf64f479c83eecdb8db121adf-chronyd.service-IaGU7i
systemd-private-1eaed0dcf64f479c83eecdb8db121adf-nginx.service-8txDZg
vmware-root_873-4013854327
vmware-root_876-2689209388
vmware-root_879-4013723248
vmware-root_884-2688554034
vmware-root_887-4013330030
vmware-root_897-3979643105
node2:
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-chronyd.service-5MOBSi
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-httpd.service-DEE53i
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-mariadb.service-VZ8Bog
vmware-root_870-2731086752
vmware-root_880-2697139639
vmware-root_896-2730693439
2.7.9 file.ditectory_exists
//判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt '*' cmd.run 'ls /opt/yy'
node2:
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-chronyd.service-5MOBSi
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-httpd.service-DEE53i
systemd-private-522dc596aa7b4cfc90a588601ac2d05d-mariadb.service-VZ8Bog
vmware-root_870-2731086752
vmware-root_880-2697139639
vmware-root_896-2730693439
node1:
hsperfdata_root
systemd-private-1eaed0dcf64f479c83eecdb8db121adf-chronyd.service-IaGU7i
systemd-private-1eaed0dcf64f479c83eecdb8db121adf-nginx.service-8txDZg
vmware-root_873-4013854327
vmware-root_876-2689209388
vmware-root_879-4013723248
vmware-root_884-2688554034
vmware-root_887-4013330030
vmware-root_897-3979643105
[root@master ~]# salt '*' file.directory_exists /opt/yy
node2:
True
node1:
True
[root@master ~]# salt '*' file.directory_exists /opt/xx
node1:
False
node2:
False
2.7.10 file.diskusage
//递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt '*' cmd.run 'du -sb /opt'
node2:
4229 /opt
node1:
37011 /opt
[root@master ~]# salt '*' file.diskusage /opt
node1:
32768
node2:
0
2.7.11 file.file_exists
//判断指定文件是否存在
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
total 564
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
drwxrwxrwt 16 root root 4096 Nov 4 05:53 hy
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
node2:
total 16
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
drwxrwxrwt 13 root root 4096 Nov 4 05:53 hy
[root@master ~]# salt '*' file.file_exists /root/a
node2:
True
node1:
True
[root@master ~]# salt '*' file.file_exists /root/hy
node2:
False
node1:
False //返回False是因为abc是目录而非文件
[root@master ~]#
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
示例:
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
实例:
[root@master ~]# salt '*' file.find / type=f name=\*.bak size=-10m
node2:
- /etc/nsswitch.conf.bak
node1:
- /etc/nsswitch.conf.bak
[root@master ~]# salt '*' cmd.run 'du -sh * /etc/nsswitch.conf.bak'
node1:
4.0K a
0 abc
4.0K anaconda-ks.cfg
4.0K bb
36K hy
548K wget-1.14-18.el7_6.1.x86_64.rpm
4.0K /etc/nsswitch.conf.bak
node2:
4.0K a
0 abc
4.0K anaconda-ks.cfg
4.0K bb
4.0K hy
[root@master ~]# salt '*' file.find /root mtime=-30d size=+500k
node1:
- /root/wget-1.14-18.el7_6.1.x86_64.rpm
node2:
4.0K /etc/nsswitch.conf.bak
[root@master ~]# salt '*' file.find /root mtime=-30d size=+500k delete
node2:
node1:
- /root/wget-1.14-18.el7_6.1.x86_64.rpm
2.7.13 file.get_gid
、
//获取指定文件的gid
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
node2:
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 /root/a
node1:
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 /root/a
[root@master ~]# salt '*' file.get_gid /root/a
node2:
48
node1:
48
2.7.14 file.get_group
//获取指定文件的组名
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
node2:
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 /root/a
node1:
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 /root/a
[root@master ~]# salt '*' file.get_group /root/a
node2:
apache
node1:
apache
2.7.15 file.get_hash
//获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt '*' cmd.run 'sha256sum /root/a'
node2:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/a
node1:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/a
[root@master ~]# salt '*' file.get_hash /root/a
node2:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
node1:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
2.7.16 file.get_mode
//获取指定文件的权限,以数字方式显示
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
node2:
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 /root/a
node1:
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 /root/a
[root@master ~]# salt '*' file.get_mode /root/a
node1:
0644
node2:
0644
2.7.17 file.get_selinux_context
//获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt 'node1' cmd.run 'ls -Z /root/a'
node1:
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /root/a
[root@master ~]# salt 'ndoe1' file.get_selinux_context /root/a
node1:
unconfined_u:object_r:admin_home_t:s0
2.7.18 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt '*' cmd.run 'sha256sum /root/a'
node2:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/a
node1:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/a
[root@master ~]# salt '*' file.get_sum /root/a
node1:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
node2:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
[root@master ~]# salt '*' cmd.run 'md5sum /root/a'
node1:
671ded4ec86c82a8779c8df17823f810 /root/a
node2:
671ded4ec86c82a8779c8df17823f810 /root/a
[root@master ~]# salt '*' file.get_sum /root/a md5
node2:
671ded4ec86c82a8779c8df17823f810
node1:
671ded4ec86c82a8779c8df17823f810
2.7.19 file.get_uid与file.get_user
//获取指定文件的 uid 或 用户名
[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
node2:
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 /root/a
node1:
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 /root/a
[root@master ~]# salt '*' file.get_uid /root/a
node2:
48
node1:
48
[root@master ~]# salt '*' file.get_user /root/a
node2:
apache
node1:
apache
[root@master ~]#
2.7.20 file.gid_to_group
//将指定的 gid 转换为组名并显示
[root@master ~]# salt '*' file.gid_to_group 1000
node1:
1000
node2:
admin
[root@master ~]# salt '*' file.gid_to_group 0
node1:
root
node2:
root
2.7.21 file.group_to_gid
//将指定的组名转换为 gid 并显示
[root@master ~]# salt '*' file.group_to_gid root
node1:
0
node2:
0
[root@master ~]# salt '*' file.group_to_gid admin
node2:
1000
node1:
2.7.22 file.grep
//在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i -B2
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
//实例
[root@master ~]# salt '*' file.grep /etc/passwd "^apache"
node2:
----------
pid:
2645
retcode:
0
stderr:
stdout:
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
node1:
----------
pid:
2543
retcode:
0
stderr:
stdout:
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
2.7.23 file.is_blkdev
//判断指定的文件是否是块设备文件
[root@master ~]# salt '*' cmd.run 'ls -l /dev/sr0'
node1:
brw-rw---- 1 root cdrom 11, 0 Nov 4 03:57 /dev/sr0
node2:
brw-rw---- 1 root cdrom 11, 0 Nov 4 03:57 /dev/sr0
[root@master ~]# salt '*' file.is_blkdev /dev/sr0
node1:
True
node2:
True
2.7.24 file.lsattr
//检查并显示出指定文件的属性信息
[root@master ~]# salt '*' cmd.run 'lsattr /root/a'
node2:
-------------------- /root/a
node1:
-------------------- /root/a
[root@master ~]# salt '*' cmd.run 'chattr +i /root/a'
node2:
node1:
[root@master ~]# salt '*' cmd.run 'lsattr /root/a'
node2:
----i--------------- /root/a
node1:
----i--------------- /root/a
[root@master ~]# salt '*' file.lsattr /root/a
node1:
----------
/root/a:
- i
node2:
----------
/root/a:
- i
2.7.25 file.mkdir
//创建目录并设置属主、属组及权限
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node2:
total 16
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
drwxrwxrwt 13 root root 4096 Nov 4 05:53 hy
node1:
total 564
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
drwxrwxrwt 16 root root 4096 Nov 4 05:53 hy
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt '*' file.mkdir /root/abc
node1:
True
node2:
True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
node2:
total 16
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
drwxr-xr-x 2 root root 6 Nov 4 06:31 abc
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
drwxrwxrwt 13 root root 4096 Nov 4 05:53 hy
node1:
total 564
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
drwxr-xr-x 2 root root 6 Nov 4 06:31 abc
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
drwxrwxrwt 16 root root 4096 Nov 4 05:53 hy
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt '*' file.mkdir /root/abc apache apache 777
node1:
True
node2:
True
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
node1:
total 564
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
drwxr-xr-x 2 root root 6 Nov 4 06:31 abc
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
drwxrwxrwt 16 root root 4096 Nov 4 05:53 hy
-rw-r--r-- 1 root root 560272 Nov 3 05:03 wget-1.14-18.el7_6.1.x86_64.rpm
node2:
total 16
-rw-r--r-- 1 apache apache 22 Nov 4 05:28 a
drwxr-xr-x 2 root root 6 Nov 4 06:31 abc
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 apache apache 22 Nov 4 05:51 bb
drwxrwxrwt 13 root root 4096 Nov 4 05:53 hy
2.7.26 file.move
//重命名
[root@node1 ~]# echo 'Hao are you' > abc
[root@master ~]# salt 'node1' test.ping
node1:
True
[root@master ~]# salt 'node1' file.move /root/abc /root/hehe
node1:
----------
comment:
'/root/abc' moved to '/root/hehe'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls'
node1:
anaconda-ks.cfg
hehe
[root@master ~]# salt 'node1' cmd.run 'cat hehe'
node1:
Hao are you
//移动
[root@master ~]# salt 'node1' file.move /root/hehe /opt/
node1:
----------
comment:
'/root/hehe' moved to '/opt/'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls /opt/'
node1:
hehe
yy
[root@master ~]#
2.7.27 file.prepend
//把文本插入指定文件的开头
[root@master ~]# salt 'node1' cmd.run 'cat abc'
node1:
Hao are you
[root@master ~]# salt 'node1' file.prepend /root/abc "hehe" "xixi" "haha"
node1:
Prepended 3 lines to "/root/abc"
[root@master ~]# salt 'node1' cmd.run 'cat abc'
node1:
hehe
xixi
haha
Hao are you
[root@master ~]# salt 'node1' file.prepend /root/abc "hehe" "xixi" "haha"
node1:
Prepended 3 lines to "/root/abc"
[root@master ~]# salt 'node1' cmd.run 'cat abc'
node1:
hehe
xixi
haha
hehe
xixi
haha
Hao are you
[root@master ~]#
2.7.28 file.sed
//修改文本文件的内容
[root@master ~]# salt 'node1' cmd.run 'cat abc'
node1:
hehe
xixi
haha
hehe
xixi
haha
Hao are you
[root@master ~]#
[root@master ~]# salt 'node1' file.sed /root/abc hehe zhangshan
node1:
----------
pid:
1799
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'node1' cmd.run 'cat abc'
node1:
zhangshan
xixi
haha
zhangshan
xixi
haha
Hao are you
[root@master ~]#
//修改匹配值第二个值flags=2
[root@master ~]# salt 'node1' file.prepend /root/abc "hello world hello tom hello jerry" "hello hehe hello hello hello"
node1:
Prepended 2 lines to "/root/abc"
[root@master ~]# salt 'node1' file.append /root/abc "world hello tom jerry hello"
node1:
Wrote 1 lines to "/root/abc"
[root@master ~]# salt 'node1' cmd.run 'cat abc'
node1:
hello world hello tom hello jerry
hello hehe hello hello hello
zhangshan
xixi
haha
zhangshan
xixi
haha
Hao are you
world hello tom jerry hello
[root@master ~]# salt 'node1' file.sed /root/abc hello runtime flags=2
node1:
----------
pid:
1824
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'node1' cmd.run 'cat abc'
node1:
hello world runtime tom hello jerry
hello hehe runtime hello hello
zhangshan
xixi
haha
zhangshan
xixi
haha
Hao are you
world hello tom jerry runtime
2.7.29 file.read
//读取文件内容相当于cat
[root@master ~]# salt 'node1' file.read /root/abc
node1:
hello world runtime tom hello jerry
hello hehe runtime hello hello
zhangshan
xixi
haha
zhangshan
xixi
haha
Hao are you
world hello tom jerry runtime
2.7.30 file.readdir
//列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt 'node1' file.readdir /root
node1:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- .bash_history
- .config
- .ssh
- .viminfo
- anaconda-ks.cfg
- abc.bak
- abc
2.7.31 file.remove
//删除指定的文件或目录,若给出的是目录,将递归删除,只支持一个参数
[root@master ~]# salt 'node1' file.mkdir /root/a/b/c/d
node1:
True
[root@master ~]# salt 'node1' cmd.run 'tree a'
node1:
a
`-- b
`-- c
`-- d
3 directories, 0 files
[root@master ~]# salt 'node1' file.touch aaa
node1:
True
//删除指定的文件
[root@master ~]# salt 'node1' file.remove /root/aaa
node1:
False
[root@master ~]# salt 'node1' cmd.run 'ls'
node1:
a
abc
abc.bak
anaconda-ks.cfg
//删除指定的目录
[root@master ~]# salt 'node1' file.remove /root/a
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls'
node1:
abc
abc.bak
anaconda-ks.cfg
2.7.32 file.rename
//仅重命名文件或目录
[root@master ~]# salt 'node1' file.readdir /root
node1:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- .bash_history
- .config
- .ssh
- .viminfo
- anaconda-ks.cfg
- abc.bak
- abc
- 2
- 3
[root@master ~]# salt 'node1' file.rename /root/2 /root/xx
node1:
True
[root@master ~]# salt 'node1' file.readdir /root
node1:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- .bash_history
- .config
- .ssh
- .viminfo
- anaconda-ks.cfg
- abc.bak
- abc
- 3
- xx
2.7.33 file.set_mode
//给指定文件设置权限
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 root root 0 Nov 5 05:02 3
-rw-r--r-- 1 root root 149 Nov 5 04:41 abc
-rw-r--r-- 1 root root 143 Nov 5 04:39 abc.bak
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 5 05:02 xx
[root@master ~]# salt 'node1' file.set_mode /root/xx 0755
node1:
0755
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 root root 0 Nov 5 05:02 3
-rw-r--r-- 1 root root 149 Nov 5 04:41 abc
-rw-r--r-- 1 root root 143 Nov 5 04:39 abc.bak
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rwxr-xr-x 1 root root 0 Nov 5 05:02 xx
2.7.34 file.symlink
//给指定的文件创建软链接
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 root root 0 Nov 5 05:02 3
-rw-r--r-- 1 root root 149 Nov 5 04:41 abc
-rw-r--r-- 1 root root 143 Nov 5 04:39 abc.bak
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rwxr-xr-x 1 root root 0 Nov 5 05:02 xx
[root@master ~]# salt 'node1' file.symlink /root/xx /root/saye
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 root root 0 Nov 5 05:02 3
-rw-r--r-- 1 root root 149 Nov 5 04:41 abc
-rw-r--r-- 1 root root 143 Nov 5 04:39 abc.bak
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
lrwxrwxrwx 1 root root 8 Nov 6 08:00 saye -> /root/xx
-rwxr-xr-x 1 root root 0 Nov 5 05:02 xx
2.7.35 file.touch
//创建空文件或更新时间戳
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 root root 0 Nov 5 05:02 3
-rw-r--r-- 1 root root 149 Nov 5 04:41 abc
-rw-r--r-- 1 root root 143 Nov 5 04:39 abc.bak
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
lrwxrwxrwx 1 root root 8 Nov 6 08:00 saye -> /root/xx
-rwxr-xr-x 1 root root 0 Nov 5 05:02 xx
[root@master ~]# salt 'node1' file.touch /root/goold
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 root root 0 Nov 5 05:02 3
-rw-r--r-- 1 root root 149 Nov 5 04:41 abc
-rw-r--r-- 1 root root 143 Nov 5 04:39 abc.bak
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 6 08:04 goold
lrwxrwxrwx 1 root root 8 Nov 6 08:00 saye -> /root/xx
-rwxr-xr-x 1 root root 0 Nov 5 05:02 xx
//更新时间
[root@master ~]# salt 'node1' file.touch /root/goold
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 root root 0 Nov 5 05:02 3
-rw-r--r-- 1 root root 149 Nov 5 04:41 abc
-rw-r--r-- 1 root root 143 Nov 5 04:39 abc.bak
-rw-------. 1 root root 1087 Jul 26 22:31 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 6 08:05 goold
lrwxrwxrwx 1 root root 8 Nov 6 08:00 saye -> /root/xx
-rwxr-xr-x 1 root root 0 Nov 5 05:02 xx
2.7.36 file.uid_to_user
//将指定的 uid 转换成用户名显示出来
[root@master ~]# salt 'node1' file.uid_to_user 48
node1:
apache
[root@master ~]# salt 'node1' file.uid_to_user 0
node1:
root
2.7.37 file.user_to_uid
//将指定的用户转换成 uid 并显示出来
[root@master ~]# salt 'node1' file.user_to_uid apache
node1:
48
[root@master ~]# salt 'node1' file.user_to_uid root
node1:
0
2.7.38 file.write
//往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
hello world runtime tom hello jerry
hello hehe runtime hello hello
zhangshan
xixi
haha
zhangshan
xixi
haha
Hao are you
world hello tom jerry runtime
[root@master ~]# salt 'node1' file.write /root/abc "hh" "xx" "yy"
node1:
Wrote 3 lines to "/root/abc"
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
hh
xx
yy