Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
//查看所有module列表
[root@salt1 ~]# salt 'salt1' sys.list_modules
salt1:
- acl
- aliases
- alternatives
- apache
- archive
- artifactory
- blockdev
- btrfs
- buildout
- cloud
- cmd
- .......
-
//查看指定module的所有function
[root@salt1 ~]# salt 'salt2' sys.list_functions cloud
salt2:
- cloud.action
- cloud.create
- cloud.destroy
- cloud.full_query
- cloud.get_instance
- cloud.has_instance
- cloud.list_images
- cloud.list_locations
- cloud.list_sizes
- cloud.network_create
- cloud.network_list
- cloud.profile
- cloud.query
- cloud.select_query
- cloud.virtual_interface_create
- cloud.virtual_interface_list
- cloud.volume_attach
- cloud.volume_create
- cloud.volume_delete
- cloud.volume_detach
- cloud.volume_list
//查看指定模块(modules)的用法
[root@salt1 ~]# salt 'salt2' 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"'
.....
//saltstack默认也支持同时执行多个module,module之间通过逗号隔开,默认传参之间也用逗号隔开,也可用指定传参分隔符--args-separator=@
[root@salt1 ~]# salt 'salt2' test.echo,cmd.run,service.status hello,hostnama,httpd
salt2:
----------
cmd.run:
/bin/sh: hostnama: command not found
service.status:
True
test.echo:
hello
返回所有活动的tcp连接
[root@salt1 ~]# salt "*" network.active_tcp
salt1:
----------
0:
----------
local_addr:
0.0.0.0
local_port:
22
remote_addr:
0.0.0.0
remote_port:
0
1:
----------
local_addr:
........
通过ip和子网掩码计算出网段
[root@salt1 ~]# salt '*' network.calc_net 192.168.174.222 255.255.255.0
salt2:
192.168.174.0/24
salt1:
192.168.174.0/24
测试minion至某一台服务器的网络是否连通
[root@salt1 ~]# salt '*' network.connect baidu.com 80
salt2:
----------
comment:
Successfully connected to baidu.com (39.156.69.79) on tcp port 80
result:
True
salt1:
----------
comment:
Successfully connected to baidu.com (220.181.38.148) on tcp port 80
result:
True
查看默认路由
[root@salt1 ~]# salt 'salt2' network.default_route
salt2:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.174.2
interface:
ens33
netmask:
0.0.0.0
.....
查看主机的fqdn(完全限定域名)
[root@salt1 ~]# salt '*' network.get_fqdn
salt2:
salt2
salt1:
salt1
获取主机名
[root@salt1 ~]# salt '*' network.get_hostname
salt2:
salt2
salt1:
salt1
查询到一个目标网络的路由信息
[root@salt1 ~]# salt "*" network.get_route 192.168.174.222
salt2:
----------
destination:
192.168.174.222
gateway:
None
interface:
lo
source:
192.168.174.222
salt1:
----------
destination:
192.168.174.222
gateway:
None
interface:
ens33
source:
192.168.174.150
返回指定网卡名的MAC地址
[root@salt1 ~]# salt '*' network.hw_addr ens33
salt2:
00:0c:29:41:73:b2
salt1:
00:0c:29:98:86:f5
从特定CIDR检索接口名称
[root@salt1 ~]# salt '*' network.ifacestartswith 192.168.174
salt1:
- ens33
salt2:
- ens33
判断当前主机是否在某一个网段
[root@salt1 ~]# salt '*' network.in_subnet 192.168.174.0/24
salt1:
True
salt2:
True
返回指定网卡的信息
[root@salt1 ~]# salt '*' network.interface ens33
salt1:
|_
----------
address:
192.168.174.150
broadcast:
192.168.174.255
label:
ens33
netmask:
255.255.255.0
返回指定网卡的IP地址
[root@salt1 ~]# salt '*' network.interface_ip ens33
salt2:
192.168.174.222
salt1:
192.168.174.150
返回当前系统中所有的网卡信息
[root@salt1 ~]# salt '*' network.interfaces
salt2:
----------
ens33:
----------
hwaddr:
00:0c:29:41:73:b2
inet:
|_
----------
address:
192.168.174.222
broadcast:
192.168.174.255
label:
.......
返回一个ip的地址列表
该函数将会忽略掉127.0.0.1的地址
[root@salt1 ~]# salt '*' network.ip_addrs
salt2:
- 192.168.174.222
salt1:
- 192.168.174.150
返回所有打开的端口和状态
|_
----------
inode:
17025
local-address:
:::920
program:
755/rpcbind
proto:
udp6
recv-q:
0
remote-address:
:::*
send-q:
0
user:
0
使用ping命令测试到某主机的连通性
[root@salt1 ~]# salt '*' network.ping baidu.com
salt2:
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=33.1 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=34.0 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=32.6 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=33.7 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 32.623/33.414/34.055/0.581 ms
返回一个指定的ip地址的反向地址
[root@salt1 ~]# salt '*' network.reverse_ip 192.168.174.222
salt1:
222.174.168.192.in-addr.arpa
salt2:
222.174.168.192.in-addr.arpa
判断指定的服务是否可用
[root@salt1 ~]# salt '*' service.available sshd
salt1:
True
salt2:
True
获取所有正在运行的服务
[root@salt1 ~]# salt '*' service.get_all
salt1:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- brandbot
- brandbot.path
- chrony-dnssrv@
- [email protected]
- chrony-wait
- chronyd
- console-getty
- console-shell
- container-getty@
- ....
检查指定服务是否开机不自动启动
[root@salt1 ~]# salt '*' service.disabled sshd
salt1:
False
salt2:
False
检查指定服务是否开机自动启动
[root@salt1 ~]# salt '*' service.enabled sshd
salt2:
True
salt1:
True
设置指定服务开机不自动启动
[root@salt1 ~]# salt '*' service.disable sshd
salt1:
True
salt2:
True
设置指定服务开机自动启动
[root@salt1 ~]# salt '*' service.enable sshd
salt1:
True
salt2:
True
重新加载指定服务
[root@salt1 ~]# salt '*' service.reload sshd
salt1:
True
salt2:
True
停止指定服务
[root@salt1 ~]# salt '*' service.stop sshd
salt2:
True
salt1:
True
启动指定服务
[root@salt1 ~]# salt '*' service.start sshd
salt1:
True
salt2:
True
重启指定服务
[root@salt1 ~]# salt '*' service.restart sshd
salt1:
True
salt2:
True
查看指定服务的状态
[root@salt1 ~]# salt '*' service.status sshd
salt1:
True
salt2:
True
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@salt1 ~]# salt '*' pkg.download wget
salt2:
----------
wget:
/var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm
salt1:
----------
wget:
/var/cache/yum/packages/wget-1.14-18.el7_6.1.x86_64.rpm //下载好的软件存放位置
列出指定包或系统中已安装的所有包的文件
[root@salt1 ~]# salt 'salt2' pkg.file_list httpd
salt2:
----------
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
- ....
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@salt1 ~]# salt '*' pkg.file_list
salt1:
----------
errors:
files:
VALUE_TRIMMED
查看包组的信息
[root@salt1 ~]# salt '*' pkg.group_info 'Development Tools'
salt1:
----------
conditional:
default:
- byacc
- cscope
- ctags
- diffstat
- doxygen
- elfutils
- gcc-gfortran
- git
- indent
- intltool
- patchutils
- rcs
- subversion
- swig
- .....
列出系统中所有的包组
[root@salt1 base]# salt '*' pkg.group_list
salt1:
----------
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
....
安装软件
[root@salt1 ~]# salt '*' pkg.install vim
salt2:
----------
vim-common:
----------
new:
2:7.4.629-6.el7
old:
2:7.4.160-2.el7
vim-enhanced:
----------
new:
2:7.4.629-6.el7
old:
2:7.4.160-2.el7
列出已下载到本地的软件包
......
perl-Test-Harness:
----------
3.28-3.el7:
----------
creation_date_time:
2019-12-20T10:03:02
creation_date_time_t:
1576807382
path:
/var/cache/yum/x86_64/$releasever/base/packages/perl-Test-Harness-3.28-3.el7.noarch.rpm
size:
309212
perl-Thread-Queue:
----------
3.02-2.el7:
----------
.....
以字典的方式列出当前已安装的软件包
......
glibc-common:
2.17-196.el7
gmp:
1:6.0.0-15.el7
gnupg2:
2.0.22-4.el7
gobject-introspection:
1.50.0-1.el7
gpg-pubkey.(none):
de57bfbe-53a9be98,f4a80eb5-53a7ff4b
gpgme:
1.3.2-5.el7
gpm-libs:
1.20.7-6.el7
grep:
2.20-3.el7
groff-base:
1.22.2-8.el7
......
列出指定文件是由哪个包提供的
[root@salt1 ~]# salt '*' pkg.owner /usr/sbin/apachectlsalt1:
httpd
salt2:
httpd
卸载指定软件
[root@salt1 ~]# salt '*' pkg.remove wget
salt1:
----------
wget:
----------
new:
old:
1.14-15.el7
salt2:
----------
wget:
----------
new:
old:
1.14-15.el7
//若要卸载多个软件中间用逗号隔开
升级系统中所有的软件包或升级指定的软件包
[root@salt1 ~]# salt '*' pkg.upgrade name=openssl
salt2:
----------
salt1:
----------
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 参数去掉即可
显示当前系统中有哪些高级状态
[root@salt1 ~]# salt '*' state.show_highstate
salt1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.apache
pkg:
|_
----------
name:
httpd
.....
执行高级状态
[root@salt1 base]# salt '*' state.highstate web.apache.apache
salt2:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:00:39.099985
Duration: 1024.217 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd is set to start
Started: 20:00:40.125511
Duration: 59.104 ms
.....
显示当前系统中的高级状态执行情况
[root@salt1 base]# salt '*' state.show_state_usage
salt2:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.apache.apache
dev:
----------
count_all:
....
显示各主机top file高级状态文件
[root@salt1 ~]# salt '*' state.show_top
salt1:
----------
base:
- web.apache.apache
salt2:
----------
base:
- web.apache.apache
执行指定的top file,而不是默认的
[root@salt1 base]# salt '*' state.top qiang.sls
salt1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 18:20:22.001723
Duration: 7103.119 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 18:20:29.170515
Duration: 71.463 ms
....
显示 master 上特定sls或sls文件列表中的状态数据
[root@salt1 base]# salt '*' state.show_sls web.apache.apache
salt1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.apache
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
....
将master端的文件批量传到minion上
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp'
salt2:
[root@salt1 ~]# salt-cp 'salt2' 123 /tmp
salt2:
----------
/tmp/123:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp'
salt2:
123
//也可同时拷贝多个文件
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp'
salt2:
123
[root@salt1 ~]# salt-cp 'salt2' abc 456 /tmp
salt2:
----------
/tmp/456:
True
/tmp/abc:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp'
salt2:
123
456
abc
检查指定路径是否存在
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp'
salt2:
111
123
456
abc
[root@salt1 ~]# salt 'salt2' file.access /tmp/111 f
salt2:
True
检查指定文件的权限信息
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp'
salt2:
total 0
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 root root 0 Feb 22 18:30 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
[root@salt1 ~]# salt 'salt2' file.access /tmp/111 r //检查是否有读权限
salt2:
True
[root@salt1 ~]# salt 'salt2' file.access /tmp/111 x //检查是否有执行权限
salt2:
True
[root@salt1 ~]# salt 'salt2' file.access /tmp/111 w //检查是否有写权限
salt2:
True
往一个文件里追加内容,若此文件不存在则会报异常
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp'
salt2:
111
123
456
abc
[root@salt1 ~]# salt 'salt2' file.append /tmp/123 "haha" "xixi" "jiji"
salt2:
Wrote 3 lines to "/tmp/123"
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/123'
salt2:
haha
xixi
jiji
[root@salt1 ~]# salt 'salt2' file.append /tmp/188 "haha" "xixi" "jiji"
salt2:
The minion function caused an exception: Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/minion.py", line 1675, in _thread_return
return_data = minion_instance.executors[fname](opts, data, func, args, kwargs)
File "/usr/lib/python2.7/site-packages/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
....
获取指定路径的基名
[root@salt1 ~]# salt 'salt2' file.basename /usr/local/zabbix
salt2:
zabbix
获取指定路径的目录名
[root@salt1 ~]# salt 'salt2' file.dirname /usr/local/zabbix
salt2:
/usr/local
检查指定文件与hash字符串是否匹配
[root@salt1 ~]# salt 'salt2' cmd.run 'md5sum /etc/passwd'
salt2:
6477dc33c5180ea2d5c0479a1fb598f8 /etc/passwd
[root@salt1 ~]# salt '*' file.check_hash /etc/passwd 6477dc33c5180ea2d5c0479a1fb598f8
salt2:
True
修改指定文件的属性,加-R递归修改文件属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件,而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改,不能删除、更改、移动 | 任何的进程只能修改目录之下的文件,不允许建立和删除文件 |
给指定文件添加属性
[root@salt2 ~]# chattr -R +a /tmp/
[root@salt2 ~]# lsattr /tmp/
-----a---------- /tmp/abc
-----a---------- /tmp/111
-----a---------- /tmp/haha
//检验效果
[root@salt2 ~]# touch /tmp/hello
[root@salt2 ~]# rm -f /tmp/haha
rm: 无法删除"/tmp/haha": 不允许的操作
//去除属性
[root@salt2 ~]# lsattr /tmp/
-----a---------- /tmp/abc
-----a---------- /tmp/111
-----a---------- /tmp/haha
[root@salt2 ~]# chattr -R -a /tmp/
[root@salt2 ~]# lsattr /tmp/
---------------- /tmp/abc
---------------- /tmp/111
---------------- /tmp/haha
//添加属性
[root@salt1 skel]# salt 'salt2' cmd.run 'lsattr /tmp/abc'
salt2:
---------------- /tmp/abc
[root@salt1 skel]# salt 'salt2' file.chattr /tmp/abc operator=add attributes=ai
salt2:
True
[root@salt1 skel]# salt 'salt2' cmd.run 'lsattr /tmp/abc'
salt2:
----ia---------- /tmp/abc
//去除属性
[root@salt1 skel]# salt 'salt2' file.chattr /tmp/abc operator=remove attributes=ai
salt2:
True
[root@salt1 skel]# salt 'salt2' cmd.run 'lsattr /tmp/abc'
salt2:
---------------- /tmp/abc
设置指定文件的属主和属组信息
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 4
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 root root 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
[root@salt1 ~]# salt 'salt2' file.chown /tmp/123 tom tomsalt2:
None
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 4
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
[root@salt1 ~]# salt 'salt2' file.chown /tmp/123 haha haha
salt2:
User does not exist
Group does not exist
用户必须存在
在远程主机复制文件(minion端到minion端)
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 4
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
[root@salt1 ~]# salt 'salt2' file.copy /tmp/123 /tmp/haha
salt2:
True
[root@salt1 ~]# salt 'salt*' cmd.run 'ls -l /tmp/'
salt2:
total 8
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /root/'
salt2:
total 11888
-rw-r--r--. 1 root root 0 Feb 22 19:12 456
[root@salt1 ~]# salt 'salt2' file.copy /tmp/ /root recur
se=True
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /root/'
salt2:
total 11896
drwxr-xr-x. 2 root root 6 Feb 22 19:13 111
-rw-r--r--. 1 root root 15 Feb 22 19:13 123
-rw-r--r--. 1 root root 0 Feb 22 19:13 456
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /123/'
salt2:
total 0
drwxr-xr-x. 2 root root 6 Feb 22 19:18 111
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
[root@salt1 ~]# salt 'salt2' file.copy /tmp/ /123/ recurse=True remove_existing=Ture
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /123/'
salt2:
total 8
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 root root 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 root root 15 Feb 22 19:08 haha
判断指定目录是否存在
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
[root@salt1 ~]# salt 'salt2' file.directory_exists /tmp/111
salt2:
True
[root@salt1 ~]# salt 'salt2' file.directory_exists /tmp/123
salt2:
False
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@salt1 ~]# salt 'salt2' cmd.run 'du -sb /tmp'
salt2:
215 /tmp
[root@salt1 ~]# salt 'salt2' file.diskusage /tmp
salt2:
30
判断指定文件是否存在
[root@salt1 ~]# salt 'salt2' file.file_exists /tmp/abc
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
[root@salt1 ~]# salt 'salt2' file.file_exists /tmp/hh
salt2:
False
类似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:
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:
[w] [d] [h] [m] [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
获取指定文件的gid
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 123
-rw-r--r--. 1 root root 0 Feb 22 18:32 456
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
[root@salt1 ~]# salt 'salt2' file.get_gid /tmp/123
salt2:
1000
获取指定文件的组名
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/123'
salt2:
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 /tmp/123
[root@salt1 ~]# salt 'salt2' file.get_group /tmp/123
salt2:
tom
获取指定文件的hash值,该值通过sha256算法的来
[root@salt1 ~]# salt 'salt2' cmd.run 'sha256sum /tmp/123'
salt2:
f780279d83c7dc56ac8f618f7928b0f9eb48ea4f8e20058a992aa9e5c41fe191 /tmp/123
[root@salt1 ~]# salt 'salt2' file.get_hash /tmp/123
salt2:
f780279d83c7dc56ac8f618f7928b0f9eb48ea4f8e20058a992aa9e5c41fe191
获取指定文件的权限,以数字方式显示
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/123'
salt2:
-rw-r--r--. 1 tom tom 15 Feb 22 18:46 /tmp/123
[root@salt1 ~]# salt 'salt2' file.get_mode /tmp/123
salt2:
0644
获取指定文件的SELINUX上下文信息
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -Z /tmp/123'
salt2:
-rw-r--r--. tom tom system_u:object_r:tmp_t:s0 /tmp/123
[root@salt1 ~]# salt 'salt2' file.get_selinux_context /tmp/123
salt2:
system_u:object_r:tmp_t:s0
按照指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
[root@salt1 ~]# salt 'salt2' file.get_sum /tmp/haha
salt2:
f780279d83c7dc56ac8f618f7928b0f9eb48ea4f8e20058a992aa9e5c41fe191
[root@salt1 ~]# salt 'salt2' cmd.run 'sha256sum /tmp/haha'
salt2:
f780279d83c7dc56ac8f618f7928b0f9eb48ea4f8e20058a992aa9e5c41fe191 /tmp/haha
[root@salt1 ~]# salt 'salt2' cmd.run 'md5sum /tmp/haha'
salt2:
5920589aca6ab0a67260158df42fed12 /tmp/haha
[root@salt1 ~]# salt 'salt2' file.get_sum /tmp/haha md5
salt2:
5920589aca6ab0a67260158df42fed12
获取指定文件的uid或用户名
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/haha'
salt2:
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 /tmp/haha
[root@salt1 ~]# salt 'salt2' file.get_uid /tmp/haha
salt2:
1000
[root@salt1 ~]# salt 'salt2' file.get_user /tmp/haha
salt2:
tom
将指定的gid转换为组名并显示
[root@salt1 ~]# salt 'salt2' file.gid_to_group 1000
salt2:
tom
[root@salt1 ~]# salt 'salt2' cmd.run 'id 1000'
salt2:
uid=1000(tom) gid=1000(tom) groups=1000(tom)
将指定的组名转换为gid并显示
[root@salt1 ~]# salt 'salt2' cmd.run 'id 1000'
salt2:
uid=1000(tom) gid=1000(tom) groups=1000(tom)
[root@salt1 ~]# salt 'salt2' file.group_to_gid tom
salt2:
1000
在指定文件中检所制定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@salt1 ~]# salt 'salt2' file.grep /etc/passwd tom
salt2:
----------
pid:
2471
retcode:
0
stderr:
stdout:
tom:x:1000:1000::/home/tom:/bin/bash
[root@salt1 ~]# salt 'salt2' file.grep /etc/passwd tOm -- -i //不区分大小写
salt2:
----------
pid:
2475
retcode:
0
stderr:
stdout:
tom:x:1000:1000::/home/tom:/bin/bash
[root@salt1 ~]# salt 'salt2' file.grep /etc/passwd tOm -- -i -B2 //显示内容和内容的前两行,-A是后几行,-C是前后几行
salt2:
----------
pid:
2479
retcode:
0
stderr:
stdout:
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gluster:x:996:993:GlusterFS daemons:/run/gluster:/sbin/nologin
tom:x:1000:1000::/home/tom:/bin/bash
[root@salt1 ~]# salt 'salt2' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l //-l看哪些文件中有过滤内容
salt2:
----------
pid:
2523
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
判断指定的文件是否是块设备
[root@salt1 ~]# salt 'salt2' file.is_blkdev /dev/sr0
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /dev/sr0'
salt2:
brw-rw----. 1 root cdrom 11, 0 Feb 23 19:18 /dev/sr0
检查并显示出指定文件的属性信息
[root@salt1 ~]# salt 'salt2' cmd.run 'lsattr /tmp/haha'
salt2:
---------------- /tmp/haha
[root@salt1 ~]# salt 'salt2' cmd.run 'chattr +i /tmp/haha'
salt2:
[root@salt1 ~]# salt 'salt2' cmd.run 'lsattr /tmp/haha'
salt2:
----i----------- /tmp/haha
[root@salt1 ~]# salt 'salt2' file.lsattr /tmp/haha
salt2:
----------
/tmp/haha:
- i
创建目录并设置属主,属组及权限
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 4
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-r--r--. 1 root root 0 Feb 23 19:27 hello
[root@salt1 ~]# salt 'salt2' file.mkdir /tmp/word
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 4
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-r--r--. 1 root root 0 Feb 23 19:27 hello
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
[root@salt1 ~]# salt 'salt2' file.mkdir /tmp/hehe tom tom 444
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 4
drwxr-xr-x. 2 root root 6 Feb 22 18:38 111
-rw-r--r--. 1 root root 0 Feb 22 18:32 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
dr--r--r--. 2 tom tom 6 Feb 27 19:10 hehe
-rw-r--r--. 1 root root 0 Feb 23 19:27 hello
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
移动文件或重命名
//重命名
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
-rw-r--r--. 1 root root 0 Feb 27 19:15 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-r--r--. 1 root root 15 Feb 27 19:15 hello
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
[root@salt1 ~]# salt 'salt2' file.move /tmp/hello /tmp/hehe
salt2:
----------
comment:
'/tmp/hello' moved to '/tmp/hehe'
result:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
-rw-r--r--. 1 root root 0 Feb 27 19:15 abc
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-r--r--. 1 root root 15 Feb 27 19:15 hehe
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
//移动
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /root'
salt2:
total 0
[root@salt1 ~]# salt 'salt2' file.move /tmp/hehe /root/
salt2:
----------
comment:
'/tmp/hehe' moved to '/root/'
result:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /root'
salt2:
total 4
-rw-r--r--. 1 root root 15 Feb 27 19:15 hehe
把文本插入指定文件的开头
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/abc'
salt2:
haha
xixi
huhu
gaga
[root@salt1 ~]# salt 'salt2' file.prepend /tmp/abc "shasha"
salt2:
Prepended 1 lines to "/tmp/abc"
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/abc'
salt2:
shasha
haha
xixi
huhu
gaga
修改文本内容
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/abc'
salt2:
shasha
haha
xixi
huhu
gaga
[root@salt1 ~]# salt 'salt2' file.sed /tmp/abc huhu haha
salt2:
----------
pid:
2623
retcode:
0
stderr:
stdout:
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/abc'
salt2:
shasha
haha
xixi
haha
gaga[root@salt1 ~]# salt 'salt2' file.sed /tmp/abc "shasha" "jiji runtime haha" //可以一行加多个字符
salt2:
----------
pid:
2631
retcode:
0
stderr:
stdout:
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/abc'
salt2:
jiji runtime haha
haha
xixi
haha
gaga
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/abc'
salt2:
jiji runtime lolo runtime runtime
lolo runtime runtime
xixi
lolo runtime runtime
xixi
[root@salt1 ~]# salt 'salt2' file.sed /tmp/abc 'runtime' 'gaga' flags=1 //修改文本每行第一个指定字符
salt2:
----------
pid:
2734
retcode:
0
stderr:
stdout:
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/abc'
salt2:
jiji gaga lolo runtime runtime
lolo gaga runtime
xixi
lolo gaga runtime
xixi
读取文件内容
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/abc'
salt2:
jiji gaga lolo runtime runtime
lolo gaga runtime
xixi
lolo gaga runtime
xixi
[root@salt1 ~]# salt 'salt2' file.read /tmp/abc
salt2:
jiji gaga lolo runtime runtime
lolo gaga runtime
xixi
lolo gaga runtime
xixi
列出指定目录下的所有文件或目录,包括隐藏文件
[root@salt1 ~]# salt 'salt2' file.readdir /root
salt2:
- .
- ..
- .bash_history
- .bashrc
- .bash_profile
- hehe
- .viminfo
删除指定的文件或目录,若给出的是目录,将递归删除
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp/'
salt2:
abc
abc.bak
haha
word
[root@salt1 ~]# salt 'salt2' file.remove /tmp/abc.baksalt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp/'
salt2:
abc
haha
word
重命名文件或目录
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp/'
salt2:
abc
haha
word
[root@salt1 ~]# salt 'salt2' file.rename /tmp/abc /tmp/hello
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls /tmp/'
salt2:
haha
hello
word
给指定文件设置权限
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-r--r--. 1 root root 77 Feb 27 19:45 hello
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
[root@salt1 ~]# salt 'salt2' file.set_mode /tmp/hello 0666
salt2:
0666
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-rw-rw-. 1 root root 77 Feb 27 19:45 hello
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
给指定的文件创建软链接
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /root/'
salt2:
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 aaa
dr--------. 2 tom tom 6 Feb 24 17:07 haha
[root@salt1 ~]# salt 'salt2' file.symlink /root/b /opt/a
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /root;ls -l /opt/'
salt2:
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 haha
创建文件或更新时间戳
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
-rw-r--r--. 1 root root 0 Feb 27 21:45 gaga
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-rw-rw-. 1 root root 77 Feb 27 19:45 hello
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
[root@salt1 ~]# salt 'salt2' file.touch /tmp/xx
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
-rw-r--r--. 1 root root 0 Feb 27 21:45 gaga
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-rw-rw-. 1 root root 77 Feb 27 19:45 hello
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
-rw-r--r--. 1 root root 0 Feb 27 21:51 xx
[root@salt1 ~]# salt 'salt2' file.touch /tmp/xx
salt2:
True
[root@salt1 ~]# salt 'salt2' cmd.run 'ls -l /tmp/'
salt2:
total 8
-rw-r--r--. 1 root root 0 Feb 27 21:45 gaga
-rw-r--r--. 1 tom tom 15 Feb 22 19:08 haha
-rw-rw-rw-. 1 root root 77 Feb 27 19:45 hello
drwxr-xr-x. 2 root root 6 Feb 27 19:09 word
-rw-r--r--. 1 root root 0 Feb 27 21:52 xx //创建时间改变
将指定的uid转换成用户名
[root@salt1 ~]# salt 'salt2' file.uid_to_user 0
salt2:
root
[root@salt1 ~]# salt 'salt2' file.uid_to_user 1000
salt2:
tom
将指定的用户转换成uid
[root@salt1 ~]# salt 'salt2' file.user_to_uid root
salt2:
0
[root@salt1 ~]# salt 'salt2' file.user_to_uid tom
salt2:
1000
往一个文件里覆盖写入指定内容
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/xx'
salt2:
bc
haha
jiji
gaga
[root@salt1 ~]# salt 'salt2' file.write /tmp/xx "tata" "menmen"
salt2:
Wrote 2 lines to "/tmp/xx"
[root@salt1 ~]# salt 'salt2' cmd.run 'cat /tmp/xx'
salt2:
tata
menmen