文章目录
- 1. SaltStack模块介绍
- 2.SaltStack常用模块
- 2.1 SaltStack常用模块之network
- 2.1.1 network.active_tcp
- 查看端口连接状态
- 2.1.2 network.calc_net
- 2.1.3 network.connect
- 2.1.4 network.default_route
- 2.1.5 network.get_fqdn
- 2.1.6 network.get_hostname
- 2.1.7 network.get_route
- 2.1.8 network.hw_addr
- 2.1.9 network.ifacestartswith
- 2.1.10 network.in_subnet
- 2.1.11 network.interface
- 2.1.12 network.interface_ip
- 2.1.13 network.interfaces
- 2.1.14 network.ip_addrs
- 2.1.15 network.netstat
- 2.1.16 network.ping
- 2.1.17 network.reverse_ip
- 2.2 SaltStack常用模块之service
- 2.3 SaltStack常用模块之pkg
- 2.4 SaltStack常用模块之state
- 2.5 SaltStack常用模块之salt-cp
- 2.6 SaltStack常用模块之file
- 2.6.1 file.access 检查指定路径是否存在
- 2.6.2 file.append 为存在的文件最近内容
- 2.6.3 file.basename 获取指定路径的基名
- 2.6.4 file.dirname 获取指定路径的目录名
- 2.6.5 file.check_hash
- 2.6.6 file.chattr 修改指定文件的属性
- 2.6.7 file.chown 设置指定文件的属主、属组信息
- 2.6.8 file.copy 在远程主机上复制文件或目录
- 2.6.9 file.ditectory_exists 判断目录是否存在
- 2.6.10 file.diskusage 递归计算磁盘使用情况
- 2.6.11 file.file_exists 判断指定文件是否存在
- 2.6.12 file.find
- 2.6.13 file.get_gid 获取指定文件的gid
- 2.6.14 file.get_group 获取指定文件的组名
- 2.6.15 file.get_hash 获取指定文件的hash值
- 2.6.16 file.get_mode 获取指定文件的权限
- 2.6.17 file.get_selinux_context 获取文件的SELINUX信息
- 2.6.18 file.get_sum 指定算法提取文件特征码
- 2.6.19 file.get_uid与file.get_user
- 2.6.20 file.gid_to_group 将 gid 转换为组名
- 2.6.21 file.group_to_gid 将组名转换为 gid
- 2.6.22 file.grep 检索指定文件中的指定内容
- 2.6.23 file.is_blkdev 判断指定的文件是否是块设备文件
- 2.6.24 file.lsattr 检查指定文件的属性信息
- 2.6.25 file.mkdir
- 2.6.26 file.move 移动或重命名
- 2.6.27 file.prepend 把文本插入指定文件的开头
- 2.6.28 file.sed 修改文本文件的内容
- 2.6.29 file.read 读取文件内容
- 2.6.30 file.readdir 列出所有文件,包括隐藏文件
- 2.6.31 file.remove 删除文件或目录
- 2.6.32 file.rename 重命名文件或目录
- 2.6.33 file.set_mode 给指定文件设置权限
- 2.6.34 file.symlink 创建软链接
- 2.6.35 file.touch 创建空文件或更新时间戳
- 2.6.36 file.uid_to_user
- 2.6.37 file.user_to_uid
- 2.6.38 file.write 写入内容至文件,并覆盖原有内容
1. SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
1.1 查看所有module列表
[root@node01-linux ~]# salt 'node01*' sys.list_modules
node01-linux.example.com:
- acl
- aliases
- alternatives
- ansible
- archive
- artifactory
- 省略N行
1.2 查看指定module的所有function
[root@node01-linux ~]# salt 'node01*' sys.list_functions cmd
node01-linux.example.com:
- 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
1.3 查看指定module的用法
[root@node01-linux ~]# salt 'node01*' sys.doc cmd
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'
cmd.which:
Returns the path of an executable available on the minion, None otherwise
CLI Example:
salt '*' cmd.which cat
cmd.which_bin:
Returns the first command found in a list of commands
CLI Example:
salt '*' cmd.which_bin '[pip2, pip, pip-python]'
1.4 SaltStack执行多个Module
SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号–args-separator=@即可
[root@node01-linux ~]# salt 'node01*' test.echo,cmd.run,service.status hello,hostname,salt-minion
node01-linux.example.com:
----------
cmd.run:
node01-linux.example.com
service.status:
True
test.echo:
hello
2.SaltStack常用模块
以下命令均只查询一台被控机node01-linux,简写为node01*
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
返回所有活动的tcp连接
[root@node01-linux ~]# salt 'node01*' network.active_tcp
node01-linux.example.com:
----------
0:
----------
local_addr:
192.168.80.130
local_port:
4505
remote_addr:
192.168.80.130
remote_port:
51142
1:
----------
local_addr:
192.168.80.130
local_port:
4505
remote_addr:
192.168.80.1
remote_port:
20746
2:
----------
local_addr:
192.168.80.130
local_port:
22
remote_addr:
192.168.80.1
remote_port:
59085
3:
----------
local_addr:
192.168.80.130
local_port:
51142
remote_addr:
192.168.80.130
remote_port:
4505
4:
----------
local_addr:
192.168.80.130
local_port:
4506
remote_addr:
192.168.80.1
remote_port:
20739
查看端口连接状态
用法:lsof -i :[端口号]
[root@node01-linux ~]# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1053 root 3u IPv4 19859 0t0 TCP *:ssh (LISTEN)
sshd 1053 root 4u IPv6 19868 0t0 TCP *:ssh (LISTEN)
sshd 50962 root 3u IPv4 161636 0t0 TCP node01-linux.example.com:ssh->192.168.80.1:25279 (ESTABLISHED)
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@node01-linux ~]# salt 'node01*' network.calc_net 192.168.80.130 255.255.255.0
node01-linux.example.com:
192.168.80.0/24
[root@node01-linux ~]# salt 'node01*' network.calc_net 192.168.72.132 255.255.255.0
node01-linux.example.com:
192.168.72.0/24
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
[root@node01-linux ~]# salt 'node01*' network.connect www.baidu.com 80
node01-linux.example.com:
----------
comment:
Successfully connected to www.baidu.com (182.61.200.6) on tcp port 80
result:
True
2.1.4 network.default_route
查看默认路由
[root@node01-linux ~]# salt 'node01*' network.default_route
node01-linux.example.com:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.80.2
interface:
ens33
netmask:
0.0.0.0
|_
----------
addr_family:
inet6
destination:
::/0
flags:
!n
gateway:
::
interface:
lo
netmask:
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@node01-linux ~]# salt 'node01*' network.get_fqdn
node01-linux.example.com:
node01-linux.example.com
2.1.6 network.get_hostname
获取主机名
[root@node01-linux ~]# salt 'node01*' network.get_hostname
node01-linux.example.com:
node01-linux.example.com
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@node01-linux ~]# salt 'node01*' network.get_route 192.168.80.1
node01-linux.example.com:
----------
destination:
192.168.80.1
gateway:
None
interface:
ens33
source:
192.168.80.130
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@node01-linux ~]# salt 'node01*' network.hw_addr ens33
node01-linux.example.com:
00:0c:29:e8:c1:1f
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@node01-linux ~]# salt 'node01*' network.ifacestartswith 192.168
node01-linux.example.com:
- ens33
[root@node01-linux ~]# salt 'node01*' network.ifacestartswith 127.0.0.1
node01-linux.example.com:
- lo
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@node01-linux ~]# salt 'node01*' network.in_subnet 192.168.79.0/24
node01-linux.example.com:
False
[root@node01-linux ~]# salt 'node01*' network.in_subnet 192.168.80.0/24
node01-linux.example.com:
True
2.1.11 network.interface
返回指定网卡的信息
[root@node01-linux ~]# salt 'node01*' network.interface ens33
node01-linux.example.com:
|_
----------
address:
192.168.80.130
broadcast:
192.168.80.255
label:
ens33
netmask:
255.255.255.0
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@node01-linux ~]# salt 'node01*' network.interface_ip ens33
node01-linux.example.com:
192.168.80.130
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@node01-linux ~]# salt 'node01*' network.interfaces
node01-linux.example.com:
----------
ens33:
----------
hwaddr:
00:0c:29:e8:c1:1f
inet:
|_
----------
address:
192.168.80.130
broadcast:
192.168.80.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:fee8:c11f
prefixlen:
64
scope:
link
up:
True
lo:
----------
hwaddr:
00:00:00:00:00:00
inet:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
----------
address:
::1
prefixlen:
128
scope:
host
up:
True
2.1.14 network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址
[root@node01-linux ~]# salt 'node01*' network.ip_addrs
node01-linux.example.com:
- 192.168.80.130
2.1.15 network.netstat
返回所有打开的端口和状态
[root@node01-linux ~]# salt 'node01*' network.netstat
node01-linux.example.com:
|_
----------
inode:
19859
local-address:
0.0.0.0:22
program:
1053/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
56338
local-address:
0.0.0.0:4505
program:
23663/python
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
- 以下省略N行
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@node01-linux ~]# salt 'node01*' network.ping www.baidu.com
node01-linux.example.com:
PING www.a.shifen.com (182.61.200.6) 56(84) bytes of data.
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=1 ttl=128 time=30.2 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=2 ttl=128 time=29.4 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=3 ttl=128 time=29.6 ms
64 bytes from 182.61.200.6 (182.61.200.6): icmp_seq=4 ttl=128 time=29.5 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 29.463/29.713/30.213/0.344 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@node01-linux ~]# salt 'node01*' network.reverse_ip 192.168.80.1
node01-linux.example.com:
1.80.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用(判断是否安装该服务,安装即为True)
[root@node01-linux ~]# salt 'node01*' service.available sshd
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' service.available httpd
node01-linux.example.com:
False
2.2.2 service.get_all
获取所有正在运行的服务
[root@node01-linux ~]# salt 'node01*' service.get_all
node01-linux.example.com:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- brandbot
- 省略N行
2.2.3 service.disabled
检查指定服务是否开机不自动启动(不开机自启返回True)
[root@node01-linux ~]# salt 'node01*' service.disabled firewalld
node01-linux.example.com:
True
2.2.4 service.enabled
检查指定服务是否开机自动启动
[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
False
2.2.5 service.disable
设置指定服务开机不自动启动
[root@node01-linux ~]# salt 'node01*' service.disable firewalld
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
False
2.2.6 service.enable
设置指定服务开机自动启动
[root@node01-linux ~]# salt 'node01*' service.enable firewalld
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' service.enabled firewalld
node01-linux.example.com:
True
2.2.7 service.reload
重新加载指定服务(服务必须启动才可执行)
[root@node01-linux ~]# systemctl start httpd
[root@node01-linux ~]# salt 'node01*' service.reload httpd
node01-linux.example.com:
True
2.2.8 service.stop
停止指定服务
[root@node01-linux ~]# salt 'node01*' service.stop httpd
node01-linux.example.com:
True
2.2.9 service.start
启动指定服务
[root@node01-linux ~]# salt 'node01*' service.start httpd
node01-linux.example.com:
True
2.2.10 service.restart
重启指定服务
[root@node01-linux ~]# salt 'node01*' service.restart httpd
node01-linux.example.com:
True
2.2.11 service.status
查看指定服务的状态
[root@node01-linux ~]# salt 'node01*' service.status httpd
node01-linux.example.com:
True
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@node01-linux ~]# salt 'node01*' pkg.download wget
node01-linux.example.com:
----------
wget:
/var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm
2.3.2 pkg.file_list
列出指定包或系统中已安装的所有包的文件
提供参数时,列出已安装的apache软件包提供的所有文件
[root@node01-linux ~]# salt 'node01*' pkg.file_list httpd
node01-linux.example.com:
----------
errors:
files:
- /etc/httpd
- /etc/httpd/conf
- /etc/httpd/conf.d
- /etc/httpd/conf.d/README
- /etc/httpd/conf.d/autoindex.conf
- /etc/httpd/conf.d/userdir.conf
- /etc/httpd/conf.d/welcome.conf
- /etc/httpd/conf.modules.d
- /etc/httpd/conf.modules.d/00-base.conf
- /etc/httpd/conf.modules.d/00-dav.conf
- /etc/httpd/conf.modules.d/00-lua.conf
- /etc/httpd/conf.modules.d/00-mpm.conf
- /etc/httpd/conf.modules.d/00-proxy.conf
- /etc/httpd/conf.modules.d/00-systemd.conf
- /etc/httpd/conf.modules.d/01-cgi.conf
- /etc/httpd/conf/httpd.conf
- /etc/httpd/conf/magic
- /etc/httpd/logs
- /etc/httpd/modules
- 省略N行
当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@node01-linux ~]# salt '*' pkg.file_list
192.168.69.202:
----------
errors:
files:
- /lib/kbd/keymaps/legacy
- /lib/kbd/keymaps/legacy/amiga
- /lib/kbd/keymaps/legacy/amiga/amiga-de.map.gz
- /lib/kbd/keymaps/legacy/amiga/amiga-us.map.gz
- /lib/kbd/keymaps/legacy/atari
2.3.3 pkg.group_info
查看包组的信息
[root@node01-linux ~]# salt 'node01*' pkg.group_info 'Development Tools'
node01-linux.example.com:
----------
conditional:
default:
- byacc
- cscope
- ctags
- diffstat
- doxygen
- elfutils
- gcc-gfortran
- git
- indent
- intltool
- patchutils
- rcs
- subversion
- swig
- systemtap
description:
A basic development environment.
group:
Development Tools
id:
development
mandatory:
- autoconf
- automake
- binutils
- bison
- flex
- gcc
- gcc-c++
- gettext
- libtool
- make
- patch
- pkgconfig
- redhat-rpm-config
- rpm-build
- rpm-sign
optional:
- ElectricFence
- ant
- babel
- bzr
- ccache
- chrpath
- clips
- clips-devel
- clips-doc
- clips-emacs
- clips-xclips
- clipsmm-devel
- clipsmm-doc
- cmake
- cmucl
- colordiff
- compat-gcc-44
- compat-gcc-44-c++
- cvs
- cvsps
- darcs
- dejagnu
- email2trac
- expect
- ftnchek
- gcc-gnat
- gcc-objc
- gcc-objc++
- ghc
- git
- haskell-platform
- imake
- javapackages-tools
- ksc
- libstdc++-docs
- lua
- mercurial
- mock
- mod_dav_svn
- nasm
- nqc
- nqc-doc
- ocaml
- perltidy
- python-docs
- qgit
- rpmdevtools
- rpmlint
- sbcl
- scorep
- systemtap-sdt-devel
- systemtap-server
- trac
- trac-git-plugin
- trac-mercurial-plugin
- trac-webadmin
- translate-toolkit
type:
package group
2.3.4 pkg.group_list
列出系统中所有的包组
[root@node01-linux ~]# salt 'node01*' pkg.group_list
node01-linux.example.com:
----------
available:
- Additional Development
- Anaconda Tools
- Backup Client
- Backup Server
- Base
- Buildsystem building group
- CentOS Linux Client product core
- CentOS Linux ComputeNode product core
- CentOS Linux Server product core
- CentOS Linux Workstation product core
- Cinnamon
- Common NetworkManager submodules
- Compatibility Libraries
- Conflicts (Client)
- Conflicts (ComputeNode)
- Conflicts (Server)
- Conflicts (Workstation)
- Console Internet Tools
- Core
- DNS Name Server
- Debugging Tools
- Desktop Debugging and Performance Tools
- Development Tools
- Dial-up Networking Support
- Directory Client
- Directory Server
- E-mail Server
- Educational Software
- Electronic Lab
- Emacs
- FTP Server
- Fedora Packager
- File and Storage Server
- Fonts
- GNOME
- GNOME Applications
- General Purpose Desktop
- Graphical Administration Tools
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Hardware Monitoring Utilities
- Haskell
- High Availability
- Hyper-v platform specific packages
- Identity Management Server
- Infiniband Support
- Input Methods
- Internet Applications
- Internet Browser
- Java Platform
- KDE
- KDE Applications
- KDE Multimedia Support
- KVM platform specific packages
- LXQt Desktop
- Large Systems Performance
- Legacy UNIX Compatibility
- Legacy X Window System Compatibility
- Load Balancer
- MATE
- Mainframe Access
- MariaDB Database Client
- MariaDB Database Server
- Milkymist
- Multimedia
- Network File System Client
- Network Infrastructure Server
- Networking Tools
- Office Suite and Productivity
- PHP Support
- Performance Tools
- Perl Support
- Perl for Web
- Platform Development
- PostgreSQL Database Client
- PostgreSQL Database Server
- Print Server
- Printing Client
- Python
- Remote Desktop Clients
- Remote Management for Linux
- Resilient Storage
- Ruby Support
- Scientific Support
- Security Tools
- Smart Card Support
- System Administration Tools
- System Management
- Technical Writing
- TurboGears application framework
- VMware platform specific packages
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Web Server
- Web Servlet Engine
- X Window System
- Xfce
available environments:
- Minimal Install
- Compute Node
- Infrastructure Server
- File and Print Server
- Cinnamon Desktop
- MATE Desktop
- Basic Web Server
- Virtualization Host
- Server with GUI
- GNOME Desktop
- KDE Plasma Workspaces
- Development and Creative Workstation
available languages:
----------
installed:
installed environments:
2.3.5 pkg.install
安装软件
[root@node01-linux ~]# salt 'node01*' pkg.install wget
node01-linux.example.com:
----------
wget:
----------
new:
1.14-18.el7_6.1
old:
2.3.6 pkg.list_downloaded
列出已下载到本地的软件包
[root@node01-linux ~]# salt 'node01*' pkg.list_downloaded
node01-linux.example.com:
----------
wget:
----------
1.14-18.el7_6.1:
----------
creation_date_time:
2020-02-20T17:53:39
creation_date_time_t:
1582192419
path:
/var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm
size:
560272
2.3.7 pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@node01-linux ~]# salt 'node01*' pkg.list_pkgs
node01-linux.example.com:
----------
GeoIP:
1.5.0-11.el7
NetworkManager:
1:1.8.0-9.el7
NetworkManager-config-server:
1:1.8.0-9.el7
NetworkManager-libnm:
1:1.8.0-9.el7
NetworkManager-team:
1:1.8.0-9.el7
NetworkManager-tui:
1:1.8.0-9.el7
PyYAML:
3.11-1.el7
Red_Hat_Enterprise_Linux-Release_Notes-7-en-US:
7-2.el7
Red_Hat_Enterprise_Linux-Release_Notes-7-zh-CN:
7-2.2.el7
acl:
2.2.51-12.el7
...省略N行
2.3.8 pkg.owner
列出指定文件是由哪个包提供的
[root@node01-linux ~]# salt 'node01*' pkg.owner /usr/sbin/apachectl
node01-linux.example.com:
httpd
[root@node01-linux ~]# salt 'node01*' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
node01-linux.example.com:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
2.3.9 pkg.remove 卸载指定软件
1.查询是否存在
[root@node01-linux ~]# salt 'node01*' cmd.run 'rpm -qa|grep wget'
node01-linux.example.com:
wget-1.14-18.el7_6.1.x86_64
2.卸载
[root@node01-linux ~]# salt 'node01*' pkg.remove wget
node01-linux.example.com:
----------
wget:
----------
new:
old:
1.14-18.el7_6.1
//若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade 升级软件包
升级系统中所有的软件包或升级指定的软件包
[root@node01-linux ~]# salt 'node01*' pkg.upgrade name=openssl
node01-linux.example.com:
----------
openssl:
----------
new:
1:1.0.2k-19.el7
old:
1:1.0.2k-8.el7
openssl-libs:
----------
new:
1:1.0.2k-19.el7
old:
1:1.0.2k-8.el7
*若想升级系统中所有的软件包则把 name 参数去掉即可(升级包过多存在风险)
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@node01-linux salt]# salt 'node01*' state.show_highstate
node01-linux.example.com:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.apache
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.apache
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate 执行高级状态
[root@node01-linux salt]# salt 'node01*' state.highstate web.apache.apache
node01-linux.example.com:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 11:22:36.887962
Duration: 688.295 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 11:22:37.577381
Duration: 47.744 ms
Changes:
Summary for node01-linux.example.com
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 736.039 ms
2.4.3 state.show_state_usage 显示高级状态执行情况
显示当前系统中的高级状态执行情况
[root@node01-linux salt]# salt 'node01*' state.show_state_usage
node01-linux.example.com:
----------
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:
2.4.4 state.show_top 返回minion将要执行的高级状态
返回minion将用于highstate的顶级数据
[root@node01-linux salt]# salt 'node01*' state.show_top
node01-linux.example.com:
----------
base:
- web.apache.apache
2.4.5 state.top 执行指定的top file
执行指定的top file,而不是默认的
- 默认top file为top.sls,state.top可执行不使用默认名字的top file
- 执行时必须带.sls后缀
[root@node01-linux salt]# salt 'node01*' state.top runtop.sls //runtop.sls为自定义的top file
node01-linux.example.com:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 11:31:53.452974
Duration: 688.117 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 11:31:54.142923
Duration: 43.703 ms
Changes:
Summary for node01-linux.example.com
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 731.820 ms
2.4.6 state.show_sls 显示sls的状态数据
显示 master 上特定sls或sls文件列表中的状态数据
[root@node01-linux salt]# salt 'node01*' state.show_sls web.apache.apache
node01-linux.example.com:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.apache
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.apache
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.5 SaltStack常用模块之salt-cp
salt-cp将master 上的文件批量传到 minion上
只用于将master上的文件批量传送到minion端,不能使minion之间进行传送
注:本次实验在同一台主机演示效果不明显,以下操作仅对node02-linux主机进行操作
拷贝单个文件到目标主机的/usr/src目录下
1.查看主机node02的/usr/src/目录下的文件
[root@node01-linux salt]# salt 'node02*' cmd.run 'ls /usr/src/'
node02-linux.example.com:
debug
kernels
2.将master端的passwd文件拷贝到node02主机
[root@node01-linux salt]# salt-cp 'node02*' /etc/passwd /usr/src/
node02-linux.example.com:
----------
/usr/src/passwd:
True
3.查看在node02是否存在passwd文件
[root@node01-linux salt]# salt 'node02*' cmd.run 'ls /usr/src/'
node02-linux.example.com:
debug
kernels
passwd
拷贝多个文件到目标主机的/usr/src目录下
拷贝多个文件到同一个目录下操作通cp命令类似
1.将group和shadow文件拷贝至node02的/usr/src目录下
[root@node01-linux salt]# salt-cp 'node02*' /etc/group /etc/shadow /usr/src/
node02-linux.example.com:
----------
/usr/src/group:
True
/usr/src/shadow:
True
2.查看执行结果
[root@node01-linux salt]# salt 'node02*' cmd.run 'ls /usr/src/'
node02-linux.example.com:
debug
group
kernels
passwd
shadow
将一个目录拷贝至/usr/src目录下需加上参数 --chunked
[root@node01-linux salt]# salt-cp 'node02*' /root/time /usr/src/ --chunked
node02-linux.example.com:
----------
/usr/src/time/a:
True
/usr/src/time/b:
True
/usr/src/time/c:
True
[root@node01-linux salt]# salt 'node02*' cmd.run 'ls /usr/src/'
node02-linux.example.com:
debug
group
kernels
passwd
shadow
time
[root@node01-linux salt]# salt 'node02*' cmd.run 'ls /usr/src/time'
node02-linux.example.com:
a
b
c
2.6 SaltStack常用模块之file
2.6.1 file.access 检查指定路径是否存在
用法: salt ‘*’ file.access [文件路径] [参数]
可用参数:
- f:检测路径是否存在
- r:检测路径是否可读
- w:检测路径是否可写
- x:检测路径是否可执行
注:是返回True,否返回False
1.查看passwd文件状态
[root@node01-linux salt]# salt 'node02*' cmd.run 'ls -ld /usr/src/passwd'
node02-linux.example.com:
-rw-r--r--. 1 root root 899 Feb 22 22:48 /usr/src/passwd
2.判断passwd是否存在
[root@node01-linux salt]# salt 'node02*' file.access /usr/src/passwd f
node02-linux.example.com:
True
3.判断passwd是否可读
[root@node01-linux salt]# salt 'node02*' file.access /usr/src/passwd r
node02-linux.example.com:
True
4.判断passwd是否可写
[root@node01-linux salt]# salt 'node02*' file.access /usr/src/passwd w
node02-linux.example.com:
True
5.判断passwd是否可执行
[root@node01-linux salt]# salt 'node02*' file.access /usr/src/passwd x
node02-linux.example.com:
False
2.6.2 file.append 为存在的文件最近内容
往一个文件里追加内容,若此文件不存在则会报异常
注:添加多行内容中间用空格隔开,为了美观可以使用‘\’换行继续输入
[root@node01-linux salt]# salt 'node02*' cmd.run 'ls -l /root/a'
node02-linux.example.com:
-rw-r--r--. 1 root root 0 Feb 23 01:41 /root/a
[root@node01-linux salt]# salt 'node02*' file.append /root/a 'Hello Word' 'jj' 'yy'
node02-linux.example.com:
Wrote 3 lines to "/root/a"
[root@node01-linux salt]# salt 'node02*' cmd.run 'cat /root/a'
node02-linux.example.com:
Hello Word
jj
yy
2.6.3 file.basename 获取指定路径的基名
作用同basename命令一致
basename用法
[root@node01-linux salt]# basename /a/b/c
c
file.basename用法
[root@node01-linux salt]# salt 'node02*' file.basename '/a/b/c'
node02-linux.example.com:
c
2.6.4 file.dirname 获取指定路径的目录名
作用同dirname相同
dirname用法
[root@node01-linux salt]# dirname /a/b/c
/a/b
file.dirname用法
[root@node01-linux salt]# salt 'node02*' file.dirname '/a/b/c'
node02-linux.example.com:
/a/b
2.6.5 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@node01-linux salt]# salt 'node02*' cmd.run 'md5sum /etc/passwd'
node02-linux.example.com:
b18cc2fc4d0d94ad87983011217de3da /etc/passwd
[root@node01-linux salt]# salt 'node02*' file.check_hash /etc/passwd b18cc2fc4d0d94ad87983011217de3da
node02-linux.example.com:
True
2.6.6 file.chattr 修改指定文件的属性
file.chattr作用与chattr相同,修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件,而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改,不能删除、更改、移动 | 任何的进程只能修改目录之下的文件,不允许建立和删除文件 |
chattr用法
lsattr查看属性
-R为递归添加,即为目录及目录中的所有文件添加权限
添加权限使用+
[root@node01-linux salt]# chattr -R +a /root/time
[root@node01-linux salt]# lsattr /root/time
-----a---------- /root/time/a
-----a---------- /root/time/b
-----a---------- /root/time/c
[root@node01-linux salt]# lsattr -d /root/time
-----a---------- /root/time
检验效果
[root@node01-linux time]# touch d
[root@node01-linux time]# rm -f d
rm: 无法删除"d": 不允许的操作
[root@node01-linux time]# echo '123'>d
[root@node01-linux time]# sed -i s/1//g d
sed: 无法移除 ./sedJWPBfo:不允许的操作
移除权限使用-
[root@node01-linux time]# chattr -R -a /root/time
[root@node01-linux time]# lsattr /root/time
---------------- /root/time/a
---------------- /root/time/b
---------------- /root/time/c
---------------- /root/time/d
---------------- /root/time/sedJWPBfo
[root@node01-linux time]# lsattr -d /root/time
---------------- /root/time
file.chattr用法
1.给指定文件添加属性
查看当前属性
[root@node01-linux ~]# salt 'node01*' cmd.run 'lsattr /root/time'
node01-linux.example.com:
---------------- /root/time/a
---------------- /root/time/b
---------------- /root/time/c
---------------- /root/time/d
添加属性
[root@node01-linux ~]# salt 'node01*' file.chattr /root/time/a operator=add attributes=ai
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'lsattr /root/time/a'
node01-linux.example.com:
----ia---------- /root/time/a
2.给指定文件去除属性
[root@node01-linux ~]# salt 'node01*' file.chattr /root/time/a operator=remove attributes=ai
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'lsattr /root/time/a'
node01-linux.example.com:
---------------- /root/time/a
2.6.7 file.chown 设置指定文件的属主、属组信息
作用同chown命令类似
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/a'
node01-linux.example.com:
-rw-r--r--. 1 tom tom 0 Feb 23 16:56 /root/a
[root@node01-linux ~]# salt 'node01*' file.chown /root/a root root
node01-linux.example.com:
None
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/a'
node01-linux.example.com:
-rw-r--r--. 1 root root 0 Feb 23 16:56 /root/a
2.6.8 file.copy 在远程主机上复制文件或目录
注:复制操作仅在被操作主机内复制
拷贝文件
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
drwxr-xr-x. 2 root root 42 Feb 23 16:44 time
[root@node01-linux ~]# salt 'node01*' file.copy /root/a /root/b
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
drwxr-xr-x. 2 root root 42 Feb 23 16:44 time
覆盖并拷贝目录,将会覆盖同名文件或目录
注:复制目录需添加参数recurse=True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
drwxr-xr-x. 2 root root 42 Feb 23 16:44 time
[root@node01-linux ~]# salt 'node01*' file.copy /tmp/ /root/abc recurse=True
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 8
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
drwxrwxrwt. 12 root root 4096 Feb 23 17:05 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
drwxr-xr-x. 2 root root 42 Feb 23 16:44 time
删除目标目录中所有文件或目录,并拷贝新内容至其中
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/abc'
node01-linux.example.com:
total 8
-rw-r--r--. 1 root root 7 Feb 23 17:09 a
-rw-r--r--. 1 root root 10 Feb 23 17:09 b
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/time'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 11:59 a
-rw-r--r--. 1 root root 0 Feb 23 11:59 b
-rw-r--r--. 1 root root 0 Feb 23 11:59 c
-rw-r--r--. 1 root root 4 Feb 23 16:03 d
[root@node01-linux ~]# salt 'node01*' file.copy /root/abc/ /root/time/ recurse=True remove_existing=True
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/time'
node01-linux.example.com:
total 8
-rw-r--r--. 1 root root 7 Feb 23 17:09 a
-rw-r--r--. 1 root root 10 Feb 23 17:09
2.6.9 file.ditectory_exists 判断目录是否存在
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
drwxr-xr-x. 2 root root 24 Feb 23 17:09 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
drwxr-xr-x. 2 root root 24 Feb 23 17:09 time
[root@node01-linux ~]# salt 'node01*' file.directory_exists /root/time
node01-linux.example.com:
True
2.6.10 file.diskusage 递归计算磁盘使用情况
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@node01-linux ~]# salt 'node01*' cmd.run 'du -sb /opt'
node01-linux.example.com:
25 /opt
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /opt'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 10 Feb 23 17:21 a
[root@node01-linux ~]# salt 'node01*' file.diskusage /opt
node01-linux.example.com:
10
2.6.11 file.file_exists 判断指定文件是否存在
注:只判断文件,不判断目录,目录存在时,检测该目录依然返回False
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
drwxr-xr-x. 2 root root 24 Feb 23 17:09 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
drwxr-xr-x. 2 root root 24 Feb 23 17:09 time
[root@node01-linux ~]# salt 'node01*' file.file_exists /root/a
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' file.file_exists /root/time
node01-linux.example.com:
False
2.6.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
file.find示例
salt 'node01*' 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
2.6.13 file.get_gid 获取指定文件的gid
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/a'
node01-linux.example.com:
-rw-r--r--. 1 root root 0 Feb 23 16:56 /root/a
[root@node01-linux ~]# salt 'node01*' file.get_gid /root/a
node01-linux.example.com:
0
2.6.14 file.get_group 获取指定文件的组名
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/a'
node01-linux.example.com:
-rw-r--r--. 1 root root 0 Feb 23 16:56 /root/a
[root@node01-linux ~]# salt 'node01*' file.get_group /root/a
node01-linux.example.com:
root
2.6.15 file.get_hash 获取指定文件的hash值
获取指定文件的hash值,该值通过 sha256 算法得来
[root@node01-linux ~]# salt 'node01*' cmd.run 'sha256sum /root/a'
node01-linux.example.com:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 /root/a
[root@node01-linux ~]# salt 'node01*' file.get_hash /root/a
node01-linux.example.com:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
2.6.16 file.get_mode 获取指定文件的权限
获取指定文件的权限,以数字方式显示
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/a'
node01-linux.example.com:
-rw-r--r--. 1 root root 0 Feb 23 16:56 /root/a
[root@node01-linux ~]# salt 'node01*' file.get_mode /root/a
node01-linux.example.com:
0644
2.6.17 file.get_selinux_context 获取文件的SELINUX信息
获取指定文件的 SELINUX 上下文信息
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -Z /root/a'
node01-linux.example.com:
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /root/a
[root@node01-linux ~]# salt 'node01*' file.get_selinux_context /root/a
node01-linux.example.com:
unconfined_u:object_r:admin_home_t:s0
2.6.18 file.get_sum 指定算法提取文件特征码
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@node01-linux ~]# salt 'node01*' cmd.run 'sha256sum /root/a'
node01-linux.example.com:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 /root/a
[root@node01-linux ~]# salt 'node01*' file.get_sum /root/a
node01-linux.example.com:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
[root@node01-linux ~]# salt 'node01*' cmd.run 'md5sum /root/a'
node01-linux.example.com:
d41d8cd98f00b204e9800998ecf8427e /root/a
[root@node01-linux ~]# salt 'node01*' file.get_sum /root/a md5
node01-linux.example.com:
d41d8cd98f00b204e9800998ecf8427e
2.6.19 file.get_uid与file.get_user
file.get_uid 获取指定文件的UID
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/a'
node01-linux.example.com:
-rw-r--r--. 1 root root 0 Feb 23 16:56 /root/a
[root@node01-linux ~]# salt 'node01*' file.get_uid /root/a
node01-linux.example.com:
0
file.get_user 获取指定文件的用户名
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/a'
node01-linux.example.com:
-rw-r--r--. 1 root root 0 Feb 23 16:56 /root/a
[root@node01-linux ~]# salt 'node01*' file.get_user /root/a
node01-linux.example.com:
root
2.6.20 file.gid_to_group 将 gid 转换为组名
将指定的 gid 转换为组名并显示
[root@node01-linux ~]# salt 'node01*' file.gid_to_group 1000
node01-linux.example.com:
tom
[root@node01-linux ~]# salt 'node01*' file.gid_to_group 0
node01-linux.example.com:
root
2.6.21 file.group_to_gid 将组名转换为 gid
将指定的组名转换为 gid 并显示
[root@node01-linux ~]# salt 'node01*' file.group_to_gid root
node01-linux.example.com:
0
[root@node01-linux ~]# salt 'node01*' file.group_to_gid tom
node01-linux.example.com:
1000
2.6.22 file.grep 检索指定文件中的指定内容
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
注:-i不区分大小写
查询指定内容
[root@node01-linux ~]# salt 'node01*' file.grep /etc/passwd nobody
node01-linux.example.com:
----------
pid:
127253
retcode:
0
stderr:
stdout:
nobody:x:99:99:Nobody:/:/sbin/nologin
不区分大小写查询指定内容
[root@node01-linux ~]# salt 'node01*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
node01-linux.example.com:
----------
pid:
127569
retcode:
0
stderr:
stdout:
IPADDR=192.168.80.130
查询指定行相邻内容
1.查询指定行及前n行 -Bn
[root@node01-linux ~]# salt 'node01*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
node02-linux.example.com:
----------
pid:
3853
retcode:
0
stderr:
stdout:
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.80.132
2.查询指定行及后n行 -An
[root@node01-linux ~]# salt 'node01*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -A2
node01-linux.example.com:
----------
pid:
127946
retcode:
0
stderr:
stdout:
IPADDR=192.168.80.130
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
3.查询指定行及前n行和后n行 -Cn
[root@node01-linux ~]# salt 'node01*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -C2
node01-linux.example.com:
----------
pid:
127968
retcode:
0
stderr:
stdout:
NAME=ens33
DEVICE=ens33
IPADDR=192.168.80.130
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
查询所有包含查询内容的文件
[root@node01-linux ~]# salt 'node01*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
node01-linux.example.com:
----------
pid:
129104
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifdown-ipv6
/etc/sysconfig/network-scripts/ifup-aliases
/etc/sysconfig/network-scripts/ifup-eth
/etc/sysconfig/network-scripts/ifup-ippp
/etc/sysconfig/network-scripts/ifup-ipv6
/etc/sysconfig/network-scripts/ifup-isdn
/etc/sysconfig/network-scripts/ifup-plip
/etc/sysconfig/network-scripts/ifup-plusb
/etc/sysconfig/network-scripts/ifup-post
/etc/sysconfig/network-scripts/ifup-ppp
/etc/sysconfig/network-scripts/ifup-tunnel
/etc/sysconfig/network-scripts/network-functions
/etc/sysconfig/network-scripts/ifcfg-ens33
2.6.23 file.is_blkdev 判断指定的文件是否是块设备文件
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /dev/sr0'
node01-linux.example.com:
brw-rw----. 1 root cdrom 11, 0 Oct 30 17:59 /dev/sr0
[root@node01-linux ~]# salt 'node01*' file.is_blkdev /dev/sr0
node01-linux.example.com:
True
2.6.24 file.lsattr 检查指定文件的属性信息
检查并显示出指定文件的属性信息
[root@node01-linux ~]# salt 'node01*' cmd.run 'lsattr /root/a'
node01-linux.example.com:
---------------- /root/a
[root@node01-linux ~]# salt 'node01*' file.lsattr /root/a
node01-linux.example.com:
----------
/root/a:
[root@node01-linux ~]# salt 'node01*' cmd.run 'chattr +i /root/a'
node01-linux.example.com:
[root@node01-linux ~]# salt 'node01*' file.lsattr /root/a
node01-linux.example.com:
----------
/root/a:
- i
2.6.25 file.mkdir
创建目录并设置属主、属组及权限
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
drwxr-xr-x. 2 root root 24 Feb 23 17:09 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
drwxr-xr-x. 2 root root 24 Feb 23 17:09 time
[root@node01-linux ~]# salt 'node01*' file.mkdir /root/xixi
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
drwxr-xr-x. 2 root root 24 Feb 23 17:09 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
drwxr-xr-x. 2 root root 24 Feb 23 17:09 time
drwxr-xr-x. 2 root root 6 Feb 27 15:31 xixi
[root@node01-linux ~]#
[root@node01-linux ~]#
[root@node01-linux ~]# salt 'node01*' file.mkdir /root/haha tom tom 700
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
drwxr-xr-x. 2 root root 24 Feb 23 17:09 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
drwx------. 2 tom tom 6 Feb 27 15:31 haha
drwxr-xr-x. 2 root root 24 Feb 23 17:09 time
drwxr-xr-x. 2 root root 6 Feb 27 15:31 xixi
2.6.26 file.move 移动或重命名
重命名
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
drwxr-xr-x. 2 root root 24 Feb 23 17:09 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 b
[root@node01-linux ~]# salt 'node01*' file.move /root/b /root/band
node01-linux.example.com:
----------
comment:
'/root/b' moved to '/root/band'
result:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
drwxr-xr-x. 2 root root 24 Feb 23 17:09 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 band
移动
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 0 Feb 23 16:56 a
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Feb 23 17:02 band
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /opt'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 10 Feb 23 17:21 a
[root@node01-linux ~]# salt 'node01*' file.move /root/band /opt/
node01-linux.example.com:
----------
comment:
'/root/band' moved to '/opt/'
result:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /opt'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 10 Feb 23 17:21 a
-rw-r--r--. 1 root root 0 Feb 23 17:02 band
2.6.27 file.prepend 把文本插入指定文件的开头
[root@node01-linux ~]# salt 'node01*' cmd.run 'cat /root/a'
node01-linux.example.com:
haha word
xixi word
[root@node01-linux ~]# salt 'node01*' file.prepend /root/a "hehe" "xixi"
node01-linux.example.com:
Prepended 2 lines to "/root/a"
[root@node01-linux ~]# salt 'node01*' cmd.run 'cat /root/a'
node01-linux.example.com:
hehe
xixi
haha word
xixi word
2.6.28 file.sed 修改文本文件的内容
注:该命令执行直接修改文本文件内容
[root@node01-linux ~]# salt 'node01*' cmd.run 'cat /root/a'
node01-linux.example.com:
hehe
xixi
haha word
xixi word
haha haha hehe
[root@node01-linux ~]# salt 'node01*' file.sed /root/a 'xixi' 'runtime'
node01-linux.example.com:
----------
pid:
3661
retcode:
0
stderr:
stdout:
[root@node01-linux ~]# salt 'node01*' cmd.run 'cat /root/a'
node01-linux.example.com:
hehe
runtime
haha word
runtime word
haha haha hehe
[root@node01-linux ~]# salt 'node01*' file.sed /root/a 'haha' 'date' flags=2
node01-linux.example.com:
----------
pid:
4744
retcode:
0
stderr:
stdout:
[root@node01-linux ~]# salt 'node01*' cmd.run 'cat /root/a'
node01-linux.example.com:
hehe
runtime
haha word
runtime word
haha date hehe
2.6.29 file.read 读取文件内容
[root@node01-linux ~]# salt 'node01*' cmd.run 'cat /root/a'
node01-linux.example.com:
hehe
runtime
haha word
runtime word
haha date hehe
[root@node01-linux ~]# salt 'node01*' file.read /root/a
node01-linux.example.com:
hehe
runtime
haha word
runtime word
haha date hehe
2.6.30 file.readdir 列出所有文件,包括隐藏文件
列出指定目录下的所有文件或目录,包括隐藏文件
[root@node01-linux ~]# salt 'node01*' file.readdir /root
node01-linux.example.com:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .bash_history
- .pki
- .viminfo
- a.bak
- a
2.6.31 file.remove 删除文件或目录
删除指定的文件或目录,若给出的是目录,将递归删除
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 12
-rw-r--r--. 1 root root 51 Feb 27 16:08 a
-rw-r--r--. 1 root root 51 Feb 27 16:07 a.bak
drwxr-xr-x. 2 root root 17 Feb 27 16:13 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
删除文件
[root@node01-linux ~]# salt 'node01*' file.remove /root/a.bak
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-r--r--. 1 root root 51 Feb 27 16:08 a
drwxr-xr-x. 2 root root 17 Feb 27 16:13 abc
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
删除目录
[root@node01-linux ~]# salt 'node01*' file.remove /root/abc
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-r--r--. 1 root root 51 Feb 27 16:08 a
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
2.6.32 file.rename 重命名文件或目录
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-r--r--. 1 root root 51 Feb 27 16:08 a
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
[root@node01-linux ~]# salt 'node01*' file.rename /root/a /root/b
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 51 Feb 27 16:08 b
2.6.33 file.set_mode 给指定文件设置权限
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-rw-r--r--. 1 root root 51 Feb 27 16:08 b
[root@node01-linux ~]# salt 'node01*' file.set_mode /root/b 0400
node01-linux.example.com:
0400
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-r--------. 1 root root 51 Feb 27 16:08 b
2.6.34 file.symlink 创建软链接
给指定的文件创建软链接
当链接不存在时,查看软链接时软链接指向文件会闪烁
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-r--------. 1 root root 51 Feb 27 16:08 b
[root@node01-linux ~]# salt 'node01*' file.symlink /root/b /opt/bind
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /opt/'
node01-linux.example.com:
total 4
-rw-r--r--. 1 root root 10 Feb 23 17:21 a
-rw-r--r--. 1 root root 0 Feb 23 17:02 band
lrwxrwxrwx. 1 root root 7 Feb 27 16:19 bind -> /root/b
2.6.35 file.touch 创建空文件或更新时间戳
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-r--------. 1 root root 51 Feb 27 16:08 b
更新时间戳
[root@node01-linux ~]# salt 'node01*' file.touch /root/b
node01-linux.example.com:
True
创建新文件
[root@node01-linux ~]# salt 'node01*' file.touch /root/a
node01-linux.example.com:
True
[root@node01-linux ~]# salt 'node01*' cmd.run 'ls -l /root/'
node01-linux.example.com:
total 8
-rw-r--r--. 1 root root 0 Feb 27 16:27 a
-rw-------. 1 root root 1444 Feb 23 16:07 anaconda-ks.cfg
-r--------. 1 root root 51 Feb 27 16:26 b
2.6.36 file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@node01-linux ~]# salt 'node01*' file.uid_to_user 0
node01-linux.example.com:
root
[root@node01-linux ~]# salt 'node01*' file.uid_to_user 1000
node01-linux.example.com:
tom
2.6.37 file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@node01-linux ~]# salt 'node01*' file.user_to_uid 1000
node01-linux.example.com:
1000
[root@node01-linux ~]# salt 'node01*' file.user_to_uid 0
node01-linux.example.com:
0
2.6.38 file.write 写入内容至文件,并覆盖原有内容
往一个指定的文件里覆盖写入指定内容
[root@node01-linux ~]# salt 'node01*' cmd.run 'cat /root/b'
node01-linux.example.com:
lall
till
hehe
[root@node01-linux ~]# salt '*' file.write /root/b "I'm tom" "haha" "xixi"
node01-linux.example.com:
Wrote 3 lines to "/root/b"
[root@node01-linux ~]# salt 'node01*' cmd.run 'cat /root/b'
node01-linux.example.com:
I'm tom
haha
xixi