SaltStack常用的模块
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltStack常用的模块相关的知识,希望对你有一定的参考价值。
1.SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
//查看所有module列表
[root@master ~]# salt ‘192.168.69.202‘ sys.list_modules
192.168.69.202:
- acl
- aliases
- alternatives
- archive
- artifactory
- blockdev
- btrfs
- buildout
- cloud
- cmd
- composer
- config
- 此处省略N行
//查看指定module的所有function
[root@master ~]# salt ‘192.168.69.202‘ sys.list_functions cmd
192.168.69.202:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.retcode
- cmd.run
- cmd.run_all
- cmd.run_chroot
- cmd.run_stderr
- cmd.run_stdout
- cmd.script
- cmd.script_retcode
- cmd.shell
- cmd.shells
- cmd.tty
- cmd.which
- cmd.which_bin
//查看指定module的用法
[root@master ~]# salt ‘192.168.69.202‘ sys.doc cmd
‘cmd.exec_code:‘
Pass in two strings, the first naming the executable language, aka -
python2, python3, ruby, perl, lua, etc. the second string containing
the code you wish to execute. The stdout will be returned.
CLI Example:
salt ‘*‘ cmd.exec_code ruby ‘puts "cheese"‘
...此处省略N行...
//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt ‘192.168.69.202‘ test.echo,cmd.run,service.status hello,hostname,salt-minion
192.168.69.202:
----------
cmd.run:
minion
service.status:
True
test.echo:
hello
2. SaltStack常用模块
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
返回所有活动的tcp连接
[root@master ~]# salt ‘*‘ network.active_tcp
192.168.69.202:
----------
0:
----------
local_addr:
::
local_port:
80
remote_addr:
::
remote_port:
0
1:
----------
local_addr:
::
local_port:
22
remote_addr:
::
remote_port:
0
2:
----------
local_addr:
::1
local_port:
25
remote_addr:
::
remote_port:
0
3:
----------
local_addr:
::
local_port:
873
remote_addr:
::
remote_port:
0
4:
----------
local_addr:
192.168.69.202
local_port:
43640
remote_addr:
192.168.69.201
remote_port:
4505
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt ‘*‘ network.calc_net 192.168.69.202 255.255.255.0
192.168.69.202:
192.168.69.0/24
[root@master ~]# salt ‘*‘ network.calc_net 192.168.69.202 255.255.255.240
192.168.69.202:
192.168.69.192/28
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt ‘*‘ network.connect baidu.com 80
192.168.69.202:
----------
comment:
Successfully connected to baidu.com (123.125.115.110) on tcp port 80
result:
True
2.1.4 network.default_route
查看默认路由
[root@master ~]# salt ‘*‘ network.default_route
192.168.69.202:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.69.1
interface:
ens32
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
192.168.69.202:
minion
2.1.6 network.get_hostname
获取主机名
[root@master ~]# salt ‘*‘ network.get_hostname
192.168.69.202:
minion
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt ‘*‘ network.get_route 192.168.70.200
192.168.69.202:
----------
destination:
192.168.70.200
gateway:
192.168.69.1
interface:
ens32
source:
192.168.69.202
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt ‘*‘ network.hw_addr ens32
192.168.69.202:
00:0c:29:d8:05:54
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt ‘*‘ network.ifacestartswith 192.168
192.168.69.202:
- ens32
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt ‘*‘ network.in_subnet 192.168.69.0/24
192.168.69.202:
True
2.1.11 network.interface
返回指定网卡的信息
[root@master ~]# salt ‘*‘ network.interface ens32
192.168.69.202:
|_
----------
address:
192.168.69.202
broadcast:
192.168.69.255
label:
ens32
netmask:
255.255.255.0
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt ‘*‘ network.interface_ip ens32
192.168.69.202:
192.168.69.202
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt ‘*‘ network.interfaces
192.168.69.202:
----------
ens32:
----------
hwaddr:
00:0c:29:d8:05:54
inet:
|_
----------
address:
192.168.69.202
broadcast:
192.168.69.255
label:
ens32
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::6860:c429:1c06:1639
prefixlen:
64
scope:
link
up:
True
lo:
----------
hwaddr:
00:00:00:00:00:00
inet:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
----------
address:
::1
prefixlen:
128
scope:
host
up:
True
2.1.14 network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址
[root@master ~]# salt ‘*‘ network.ip_addrs
192.168.69.202:
- 192.168.69.202
2.1.15 network.netstat
返回所有打开的端口和状态
[root@master ~]# salt ‘*‘ network.netstat
192.168.69.202:
|_
----------
inode:
20245
local-address:
0.0.0.0:22
program:
1155/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
14152
local-address:
127.0.0.1:25
program:
1386/master
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
142715
local-address:
0.0.0.0:873
program:
54664/rsync
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
498916
local-address:
192.168.69.202:22
program:
9388/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.160.25:57723
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
339297
local-address:
192.168.69.202:43640
program:
9407/python
proto:
tcp
recv-q:
0
remote-address:
192.168.69.201:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
324060
local-address:
:::80
program:
24889/httpd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
20247
local-address:
:::22
program:
1155/sshd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
14153
local-address:
::1:25
program:
1386/master
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
142716
local-address:
:::873
program:
54664/rsync
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt ‘*‘ network.ping baidu.com
192.168.69.202:
PING baidu.com (220.181.57.216) 56(84) bytes of data.
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=1 ttl=43 time=42.7 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=43 time=50.6 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=43 time=42.7 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=4 ttl=43 time=42.7 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 42.720/44.709/50.652/3.437 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt ‘*‘ network.reverse_ip 192.168.69.30
192.168.69.202:
30.69.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用
[root@master ~]# salt ‘*‘ service.available sshd
192.168.69.202:
True
[root@master ~]# salt ‘*‘ service.available vsftpd
192.168.69.202:
False
2.2.2 service.get_all
获取所有正在运行的服务
[root@master ~]# salt ‘*‘ service.get_all
192.168.69.202:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- brandbot
- brandbot.path
- console-getty
- console-shell
- container-getty@
- cpupower
- crond
- 此处省略N行
2.2.3 service.disabled
检查指定服务是否开机不自动启动
[root@master ~]# salt ‘*‘ service.disabled httpd
192.168.69.202:
False
2.2.4 service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt ‘*‘ service.enabled httpd
192.168.69.202:
True
2.2.5 service.disable
设置指定服务开机不自动启动
[root@master ~]# salt ‘*‘ service.disable httpd
192.168.69.202:
True
[root@master ~]# salt ‘*‘ service.enabled httpd
192.168.69.202:
False
2.2.6 service.enable
设置指定服务开机自动启动
[root@master ~]# salt ‘*‘ service.enable httpd
192.168.69.202:
True
[root@master ~]# salt ‘*‘ service.enabled httpd
192.168.69.202:
True
2.2.7 service.reload
重新加载指定服务
[root@master ~]# salt ‘*‘ service.reload httpd
192.168.69.202:
True
2.2.8 service.stop
停止指定服务
[root@master ~]# salt ‘*‘ service.stop httpd
192.168.69.202:
True
2.2.9 service.start
启动指定服务
[root@master ~]# salt ‘*‘ service.start httpd
192.168.69.202:
True
2.2.10 service.restart
重启指定服务
[root@master ~]# salt ‘*‘ service.restart httpd
192.168.69.202:
True
2.2.11 service.status
查看指定服务的状态
[root@master ~]# salt ‘*‘ service.status httpd
192.168.69.202:
True
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@master ~]# salt ‘*‘ pkg.download wget
192.168.69.202:
----------
wget:
/var/cache/yum/packages/wget-1.14-18.el7.x86_64.rpm //下载好的软件放在这里
2.3.2 pkg.file_list
列出指定包或系统中已安装的所有包的文件
//列出已安装的apache软件包提供的所有文件
[root@master ~]# salt ‘*‘ pkg.file_list httpd
192.168.69.202:
----------
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
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# 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
- /lib/kbd/keymaps/legacy/atari/atari-de.map.gz
- /lib/kbd/keymaps/legacy/atari/atari-se.map.gz
- /lib/kbd/keymaps/legacy/atari/atari-uk-falcon.map.gz
- /lib/kbd/keymaps/legacy/atari/atari-us.map.gz
- /lib/kbd/keymaps/legacy/i386
- /lib/kbd/keymaps/legacy/i386/azerty
- /lib/kbd/keymaps/legacy/i386/azerty/azerty.map.gz
- /lib/kbd/keymaps/legacy/i386/azerty/be-latin1.map.gz
- /lib/kbd/keymaps/legacy/i386/azerty/fr-latin0.map.gz
- /lib/kbd/keymaps/legacy/i386/azerty/fr-latin1.map.gz
- /lib/kbd/keymaps/legacy/i386/azerty/fr-latin9.map.gz
2.3.3 pkg.group_info
查看包组的信息
[root@master ~]# salt ‘*‘ pkg.group_info ‘Development Tools‘
192.168.69.202:
----------
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
- chrpath
- cmake
- compat-gcc-44
- compat-gcc-44-c++
- cvs
- dejagnu
- expect
- gcc-gnat
- gcc-objc
- gcc-objc++
- imake
- javapackages-tools
- ksc
- libstdc++-docs
- mercurial
- mod_dav_svn
- nasm
- perltidy
- python-docs
- rpmdevtools
- rpmlint
- systemtap-sdt-devel
- systemtap-server
type:
package group
2.3.4 pkg.group_list
列出系统中所有的包组
[root@master ~]# salt ‘*‘ pkg.group_list
192.168.69.202:
----------
available:
- Additional Development
- Anaconda Tools
- Backup Client
- Backup Server
- Base
- CentOS Linux Client product core
- CentOS Linux ComputeNode product core
- CentOS Linux Server product core
- CentOS Linux Workstation product core
- 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
- Emacs
- FTP Server
- File and Storage Server
- Fonts
- GNOME
- GNOME Applications
- Graphical Administration Tools
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Hardware Monitoring Utilities
- 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
- Large Systems Performance
- Legacy UNIX Compatibility
- Legacy X Window System Compatibility
- Load Balancer
- Mainframe Access
- MariaDB Database Client
- MariaDB Database Server
- 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
- VMware platform specific packages
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Web Server
- Web Servlet Engine
- X Window System
available environments:
- Minimal Install
- Compute Node
- Infrastructure Server
- File and Print Server
- 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@master ~]# salt ‘*‘ pkg.install wget
192.168.69.202:
----------
wget:
----------
new:
1.14-18.el7
old:
2.3.6 pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt ‘*‘ pkg.list_downloaded
192.168.69.202:
----------
wget:
----------
1.14-18.el7:
----------
creation_date_time:
2019-02-22T17:11:52
creation_date_time_t:
1550826712
path:
/var/cache/yum/packages/wget-1.14-18.el7.x86_64.rpm
size:
560152
2.3.7 pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt ‘*‘ pkg.list_pkgs
192.168.69.202:
----------
GeoIP:
1.5.0-11.el7
NetworkManager:
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
NetworkManager-wifi:
1:1.8.0-9.el7
PyYAML:
3.11-1.el7
...此处省略N行
2.3.8 pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt ‘*‘ pkg.owner /usr/sbin/apachectl
192.168.69.202:
httpd
[root@master ~]# salt ‘*‘ pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
192.168.69.202:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
2.3.9 pkg.remove
卸载指定软件
[root@master ~]# salt ‘*‘ cmd.run ‘rpm -qa|grep wget‘
192.168.69.202:
wget-1.14-18.el7.x86_64
[root@master ~]# salt ‘*‘ pkg.remove wget
192.168.69.202:
----------
wget:
----------
new:
old:
1.14-18.el7
//若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt ‘*‘ pkg.upgrade name=openssl
192.168.69.202:
----------
openssl:
----------
new:
1:1.0.2k-16.el7
old:
1:1.0.2k-8.el7
openssl-libs:
----------
new:
1:1.0.2k-16.el7
old:
1:1.0.2k-8.el7
//若想升级系统中所有的软件包则把 name 参数去掉即可
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt ‘*‘ state.show_highstate
192.168.69.202:
----------
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@master ~]# salt ‘*‘ state.highstate web.apache.apache
192.168.69.202:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:07:37.012733
Duration: 1050.73 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 20:07:38.065796
Duration: 94.747 ms
Changes:
Summary for 192.168.69.202
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.145 s
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt ‘*‘ state.show_state_usage
192.168.69.202:
----------
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将用于highstate的顶级数据
[root@master ~]# salt ‘*‘ state.show_top
192.168.69.202:
----------
base:
- web.apache.apache
2.4.5 state.top
执行指定的top file,而不是默认的
[root@master ~]# salt ‘*‘ state.top runtime.sls
192.168.69.202:
----------
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.apache
192.168.69.202:
----------
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上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt ‘*‘ cmd.run ‘ls /usr/src/‘
192.168.69.202:
debug
kernels
[root@master ~]# salt-cp ‘*‘ /etc/passwd /usr/src/
192.168.69.202:
----------
/usr/src/passwd:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls /usr/src‘
192.168.69.202:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp ‘*‘ /etc/shadow /etc/group /usr/src
192.168.69.202:
----------
/usr/src/group:
True
/usr/src/shadow:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls /usr/src‘
192.168.69.202:
debug
group
kernels
passwd
shadow
2.6 SaltStack常用模块之file
2.6.1 file.access
检查指定路径是否存在
[root@master ~]# salt ‘*‘ cmd.run ‘ls /usr/src‘
192.168.69.202:
debug
group
kernels
passwd
shadow
zabbix-4.0.3
[root@master ~]# salt ‘*‘ file.access /usr/src/passwd f
192.168.69.202:
True
[root@master ~]# salt ‘*‘ file.access /usr/src/abc f
192.168.69.202:
False
检查指定文件的权限信息
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /usr/src/‘
192.168.69.202:
total 12
drwxr-xr-x. 2 root root 6 Nov 5 2016 debug
-rw-r--r--. 1 root root 504 Feb 23 16:06 group
drwxr-xr-x. 2 root root 6 Nov 5 2016 kernels
-rw-r--r--. 1 root root 1035 Feb 23 16:05 passwd
-rw-r--r--. 1 root root 679 Feb 23 16:06 shadow
drwxr-xr-x. 13 root root 208 Feb 23 16:13 zabbix-4.0.3
[root@master ~]# salt ‘*‘ file.access /usr/src/passwd r //是否有读权限
192.168.69.202:
True
[root@master ~]# salt ‘*‘ file.access /usr/src/passwd w //是否有写权限
192.168.69.202:
True
[root@master ~]# salt ‘*‘ file.access /usr/src/passwd x //是否有执行权限
192.168.69.202:
False
2.6.2 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/a‘
192.168.69.202:
-rw-r--r--. 1 root root 0 Feb 23 16:46 /root/a
[root@master ~]# salt ‘*‘ file.append /root/a "hello world" "haha" "xixi"
192.168.69.202:
Wrote 3 lines to "/root/a"
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/a‘
192.168.69.202:
-rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt ‘*‘ cmd.run ‘cat /root/a‘
192.168.69.202:
hello world
haha
xixi
2.6.3 file.basename
获取指定路径的基名
[root@master ~]# salt ‘*‘ file.basename ‘/root/zabbix/abc‘
192.168.69.202:
abc
2.6.4 file.dirname
获取指定路径的目录名
[root@master ~]# salt ‘*‘ file.dirname ‘/root/zabbix/abc‘
192.168.69.202:
/root/zabbix
2.6.5 file.checkhash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt ‘*‘ cmd.run ‘md5sum /etc/passwd‘
192.168.69.202:
83c9a0ffa6c002fb45a0c37a767f55f8 /etc/passwd
[root@master ~]# salt ‘*‘ file.check_hash /etc/passwd 83c9a0ffa6c002fb45a0c37a767f55f8
192.168.69.202:
True
2.6.6 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件,而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改,不能删除、更改、移动 | 任何的进程只能修改目录之下的文件,不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@master ~]# salt ‘*‘ cmd.run ‘lsattr /root‘
192.168.69.202:
---------------- /root/anaconda-ks.cfg
---------------- /root/a
//添加属性
[root@master ~]# salt ‘*‘ file.chattr /root/a operator=add attributes=ai
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘lsattr /root‘
192.168.69.202:
---------------- /root/anaconda-ks.cfg
----ia---------- /root/a
给指定文件去除属性
[root@master ~]# salt ‘*‘ file.chattr /root/a operator=remove attributes=i
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘lsattr /root‘
192.168.69.202:
---------------- /root/anaconda-ks.cfg
-----a---------- /root/a
2.6.7 file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 8
-r--------. 1 tom tom 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Jan 8 15:06 anaconda-ks.cfg
[root@master ~]# salt ‘*‘ file.chown /root/a root root
192.168.69.202:
None
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 8
-r--------. 1 root root 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Jan 8 15:06 anaconda-ks.cfg
2.6.8 file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 8
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Jan 8 15:06 anaconda-ks.cfg
[root@master ~]# salt ‘*‘ file.copy /root/a /root/cc
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 12
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Jan 8 15:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 22 Feb 24 14:57 cc
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 12
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 22 Feb 24 14:57 cc
[root@master ~]# salt ‘*‘ file.copy /tmp/ /root/abc recurse=True
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 16
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
drwxrwxrwt. 12 root root 4096 Feb 24 15:02 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 22 Feb 24 14:57 cc
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/abc‘
192.168.69.202:
total 336
drwxr-xr-x. 2 root root 6 Feb 24 15:02 abc
-rw-r--r--. 1 root root 1259 Feb 24 15:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 219068 Feb 24 15:02 libevent-2.0.21-4.el7.x86_64.rpm
-rw-r--r--. 1 root root 86564 Feb 24 15:02 libevent-devel-2.0.21-4.el7.x86_64.rpm
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-httpd.service-qGPJuL
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vgauthd.service-8p0jur
drwxr-xr-x. 3 root root 17 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vmtoolsd.service-ouRK2O
drwxr-xr-x. 2 root root 6 Feb 24 15:02 systemd-private-07ec2f23d5a348e5b7cf1d1c0799ebd4-vmtoolsd.service-ouRK2Ovmware-root
-rw-r--r--. 1 root root 493 Feb 24 15:02 yum_save_tx.2019-02-22.14-22.RXRzka.yumtx
-rw-r--r--. 1 root root 22692 Feb 24 15:02 zabbix_agentd.log
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /opt/abc‘
192.168.69.202:
total 0
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vgauthd.service-kI2C1K
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vmtoolsd.service-ZzRiqC
//拷贝目录
[root@master ~]# salt ‘*‘ file.copy /opt/abc/ /root/abc/ recurse=True remove_existing=True
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/abc‘
192.168.69.202:
total 0
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vgauthd.service-kI2C1K
drwx------. 3 root root 17 Jan 7 16:41 systemd-private-fdaaf26c12984e209f544671b67c1951-vmtoolsd.service-ZzRiqC
2.6.9 file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /opt‘
192.168.69.202:
total 40
-rw-r--r--. 1 root root 1664 Aug 30 2017 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Aug 30 2017 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Aug 30 2017 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 630 Aug 30 2017 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Aug 30 2017 CentOS-Sources.repo
-rw-r--r--. 1 root root 3830 Aug 30 2017 CentOS-Vault.repo
-rw-r--r--. 1 root root 314 Aug 30 2017 CentOS-fasttrack.repo
drwxr-xr-x. 4 root root 165 Feb 19 09:20 abc
drwxr-xr-x. 81 root root 8192 Feb 22 11:21 etc
drwxr-xr-x. 4 root root 165 Feb 19 09:20 test
[root@master ~]# salt ‘*‘ file.directory_exists /opt/abc
192.168.69.202:
True
2.6.10 file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt ‘*‘ cmd.run ‘du -sb /opt‘
192.168.69.202:
27166275 /opt
[root@master ~]# salt ‘*‘ file.diskusage /opt
192.168.69.202:
27060683
2.6.11 file.file_exists
判断指定文件是否存在
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 12
-rw-r--r--. 1 root root 22 Feb 23 16:51 a
drwxr-xr-x. 4 root root 165 Feb 19 09:20 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 22 Feb 24 14:57 cc
[root@master ~]# salt ‘*‘ file.file_exists /root/a
192.168.69.202:
True
[root@master ~]# salt ‘*‘ file.file_exists /root/abc
192.168.69.202:
False //返回False是因为abc是目录而非文件
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
示例:
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
2.6.13 file.get_gid
获取指定文件的gid
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/a‘
192.168.69.202:
-rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt ‘*‘ file.get_gid /root/a
192.168.69.202:
0
2.6.14 file.get_group
获取指定文件的组名
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/a‘
192.168.69.202:
-rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt ‘*‘ file.get_group /root/a
192.168.69.202:
root
2.6.15 file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt ‘*‘ cmd.run ‘sha256sum /root/a‘
192.168.69.202:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/a
[root@master ~]# salt ‘*‘ file.get_hash /root/a
192.168.69.202:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
2.6.16 file.get_mode
获取指定文件的权限,以数字方式显示
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/a‘
192.168.69.202:
-rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt ‘*‘ file.get_mode /root/a
192.168.69.202:
0644
2.6.17 file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt ‘*‘ cmd.run ‘ls -Z /root/a‘
192.168.69.202:
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /root/a
[root@master ~]# salt ‘*‘ file.get_selinux_context /root/a
192.168.69.202:
unconfined_u:object_r:admin_home_t:s0
2.6.18 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt ‘*‘ cmd.run ‘sha256sum /root/a‘ 192.168.69.202: 11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/a [root@master ~]# salt ‘*‘ file.get_sum /root/a 192.168.69.202: 11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a [root@master ~]# salt ‘*‘ cmd.run ‘md5sum /root/a‘ 192.168.69.202: 671ded4ec86c82a8779c8df17823f810 /root/a [root@master ~]# salt ‘*‘ file.get_sum /root/a md5 192.168.69.202: 671ded4ec86c82a8779c8df17823f810
2.6.19 file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/a‘
192.168.69.202:
-rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt ‘*‘ file.get_uid /root/a
192.168.69.202:
0
[root@master ~]# salt ‘*‘ file.get_user /root/a
192.168.69.202:
root
2.6.20 file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt ‘*‘ file.gid_to_group 1000
192.168.69.202:
tom
[root@master ~]# salt ‘*‘ file.gid_to_group 0
192.168.69.202:
root
2.6.21 file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt ‘*‘ file.group_to_gid root
192.168.69.202:
0
[root@master ~]# salt ‘*‘ file.group_to_gid tom
192.168.69.202:
1000
2.6.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
2.6.23 file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /dev/sr0‘
192.168.69.202:
brw-rw----. 1 root cdrom 11, 0 Feb 23 17:13 /dev/sr0
[root@master ~]# salt ‘*‘ file.is_blkdev /dev/sr0
192.168.69.202:
True
2.6.24 file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt ‘*‘ cmd.run ‘lsattr /root/a‘
192.168.69.202:
---------------- /root/a
[root@master ~]# salt ‘*‘ cmd.run ‘chattr +i /root/a‘
192.168.69.202:
[root@master ~]# salt ‘*‘ cmd.run ‘lsattr /root/a‘
192.168.69.202:
----i----------- /root/a
[root@master ~]# salt ‘*‘ file.lsattr /root/a
192.168.69.202:
----------
/root/a:
- i
2.6.25 file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 16
-rw-r--r--. 1 root root 26 Feb 24 16:16 a
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 26 Feb 24 16:16 cc
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
[root@master ~]# salt ‘*‘ file.mkdir /root/abc
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 16
-rw-r--r--. 1 root root 26 Feb 24 16:16 a
drwxr-xr-x. 2 root root 6 Feb 24 17:05 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 26 Feb 24 16:16 cc
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
[root@master ~]# salt ‘*‘ file.mkdir /root/haha tom tom 400
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 16
-rw-r--r--. 1 root root 26 Feb 24 16:16 a
drwxr-xr-x. 2 root root 6 Feb 24 17:05 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 26 Feb 24 16:16 cc
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
2.6.26 file.move
移动或重命名
//重命名
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 20
-rw-r--r--. 1 root root 26 Feb 24 16:16 a
drwxr-xr-x. 2 root root 6 Feb 24 17:05 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 26 Feb 24 16:16 b
-rw-r--r--. 1 root root 26 Feb 24 16:16 cc
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
[root@master ~]# salt ‘*‘ file.move /root/a /root/b
192.168.69.202:
----------
comment:
‘/root/a‘ moved to ‘/root/b‘
result:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 16
drwxr-xr-x. 2 root root 6 Feb 24 17:05 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 26 Feb 24 16:16 b
-rw-r--r--. 1 root root 26 Feb 24 16:16 cc
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
//移动
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 16
drwxr-xr-x. 2 root root 6 Feb 24 17:05 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 26 Feb 24 16:16 b
-rw-r--r--. 1 root root 26 Feb 24 16:16 cc
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /opt‘
192.168.69.202:
total 0
[root@master ~]# salt ‘*‘ file.move /root/cc /opt/
salt ‘*‘ file.move /root/cc /opt/
192.168.69.202:
----------
comment:
‘/root/cc‘ moved to ‘/opt/‘
result:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /opt‘
192.168.69.202:
total 4
-rw-r--r--. 1 root root 26 Feb 24 16:16 cc
2.6.28 file.sed
修改文本文件的内容
[root@master ~]# salt ‘*‘ cmd.run ‘cat /root/b‘
192.168.69.202:
hehe
xixi
haha
hello hello hello hello world
hello runtime
[root@master ~]# salt ‘*‘ file.sed /root/b ‘hello‘ ‘runtime‘
192.168.69.202:
----------
pid:
25170
retcode:
0
stderr:
stdout:
[root@master ~]# salt ‘*‘ cmd.run ‘cat /root/b‘
192.168.69.202:
hehe
xixi
haha
runtime runtime runtime runtime world
runtime runtime
[root@master ~]# salt ‘*‘ cmd.run ‘cat /root/b‘
192.168.69.202:
hehe
xixi
haha
hello hello hello hello world
hello hello
[root@master ~]# salt ‘*‘ file.sed /root/b ‘hello‘ ‘runtime‘ flags=2
192.168.69.202:
----------
pid:
25590
retcode:
0
stderr:
stdout:
[root@master ~]# salt ‘*‘ cmd.run ‘cat /root/b‘
192.168.69.202:
hehe
xixi
haha
hello runtime hello hello world
hello runtime
2.6.29 file.read
读取文件内容
[root@master ~]# salt ‘*‘ cmd.run ‘cat /root/cca‘
192.168.69.202:
hello world
hello runtime
[root@master ~]# salt ‘*‘ file.read /root/cca
192.168.69.202:
hello world
hello runtime
2.6.30 file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt ‘*‘ file.readdir /root
192.168.69.202:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- .bash_history
- .local
- .config
- .pki
- .ICE-unix
- .X11-unix
- .XIM-unix
- .font-unix
- .Test-unix
- anaconda-ks.cfg
- cca
- abc
- haha
- b.bak
- b
- .viminfo
2.6.31 file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 16
drwxr-xr-x. 2 root root 6 Feb 24 17:05 abc
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 62 Feb 24 17:29 b
-rw-r--r--. 1 root root 58 Feb 24 17:29 b.bak
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
[root@master ~]# salt ‘*‘ file.remove /root/b
192.168.69.202:
True
[root@master ~]# salt ‘*‘ file.remove /root/abc
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 12
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 58 Feb 24 17:29 b.bak
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
2.6.32 file.rename
重命名文件或目录
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 12
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 58 Feb 24 17:29 b.bak
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
[root@master ~]# salt ‘*‘ file.rename /root/b.bak /root/b
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 12
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 58 Feb 24 17:29 b
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
2.6.33 file.set_mode
给指定文件设置权限
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 12
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-rw-r--r--. 1 root root 58 Feb 24 17:29 b
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
[root@master ~]# salt ‘*‘ file.set_mode /root/b 0400
192.168.69.202:
0400
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root‘
192.168.69.202:
total 12
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-r--------. 1 root root 58 Feb 24 17:29 b
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
2.6.34 file.symlink
给指定的文件创建软链接
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root/‘
192.168.69.202:
total 12
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-r--------. 1 root root 58 Feb 24 17:29 b
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
[root@master ~]# salt ‘*‘ file.symlink /root/b /opt/a
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /root;ls -l /opt/‘
192.168.69.202:
total 12
-rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
-r--------. 1 root root 58 Feb 24 17:29 b
-rw-r--r--. 1 root root 26 Feb 24 16:18 cca
dr--------. 2 tom tom 6 Feb 24 17:07 haha
total 4
lrwxrwxrwx. 1 root root 7 Mar 1 18:18 a -> /root/b
-rw-rw-r--. 1 zabbix zabbix 4 Feb 28 08:38 jjyy
2.6.35 file.touch
创建空文件或更新时间戳
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /opt‘
192.168.69.202:
total 4
lrwxrwxrwx. 1 root root 7 Mar 1 18:18 a -> /root/b
-rw-r--r--. 1 root root 0 Mar 1 18:21 aa
-rw-rw-r--. 1 zabbix zabbix 4 Feb 28 08:38 jjyy
[root@master ~]# salt ‘*‘ file.touch /opt/cc
192.168.69.202:
True
[root@master ~]# salt ‘*‘ file.touch /opt/aa
192.168.69.202:
True
[root@master ~]# salt ‘*‘ cmd.run ‘ls -l /opt‘
192.168.69.202:
total 4
lrwxrwxrwx. 1 root root 7 Mar 1 18:18 a -> /root/b
-rw-r--r--. 1 root root 0 Mar 1 18:22 aa
-rw-r--r--. 1 root root 0 Mar 1 18:22 cc
-rw-rw-r--. 1 zabbix zabbix 4 Feb 28 08:38 jjyy
2.6.36 file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@master ~]# salt ‘*‘ file.uid_to_user 0
192.168.69.202:
root
[root@master ~]# salt ‘*‘ file.uid_to_user 1000
192.168.69.202:
tom
2.6.37 file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@master ~]# salt ‘*‘ file.user_to_uid tom
192.168.69.202:
1000
[root@master ~]# salt ‘*‘ file.user_to_uid root
192.168.69.202:
0
2.6.38 file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt ‘*‘ cmd.run ‘cat /root/b‘
192.168.69.202:
I‘m sean
[root@master ~]# salt ‘*‘ file.write /root/b "I‘m tom" "haha" "xixi"
192.168.69.202:
Wrote 3 lines to "/root/b"
[root@master ~]# salt ‘*‘ cmd.run ‘cat /root/b‘
192.168.69.202:
I‘m tom
haha
xixi
以上是关于SaltStack常用的模块的主要内容,如果未能解决你的问题,请参考以下文章