SaltStack常用的模块
文章目录
- SaltStack常用的模块
- 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
- 2.6.14 file.get_group
- 2.6.15 file.get_hash
- 2.6.16 file.get_mode
- 2.6.17 file.get_selinux_context
- 2.6.18 file.get_sum
- 2.6.19 file.get_uid与file.get_user
- 2.6.20 file.gid_to_group
- 2.6.21 file.group_to_gid
- 2.6.22 file.grep
- 2.6.23 file.is_blkdev
- 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列表:
//查看所有module列表
[root@master ~]# salt 'minion' sys.list_modules
minion:
- acl
- aliases
- alternatives
- ansible
- archive
- artifactory
- beacons
- bigip
- btrfs
- buildout
- chroot
- cloud
- cmd
- composer
- config
- consul
- container_resource
- cp
...
//查看指定module的所有function
[root@master ~]# salt 'minion' sys.list_functions file
minion:
- file.access
- file.append
- file.apply_template_on_contents
- file.basename
- file.blockreplace
- file.chattr
- file.check_file_meta
- file.check_hash
- file.check_managed
- file.check_managed_changes
- file.check_perms
- file.chgrp
- file.chown
- file.comment
- file.comment_line
- file.contains
- file.contains_glob
- file.contains_regex
- file.copy
...
//查看指定module的用法
[root@master ~]# salt 'minion' sys.doc file
file.access:
New in version 2014.1.0
Test whether the Salt process has the specified access to the file. One of
the following modes must be specified:
.. code-block::text
f: Test the existence of the path
r: Test the readability of the path
w: Test the writability of the path
x: Test whether the path can be executed
CLI Example:
salt '*' file.access /path/to/file f
salt '*' file.access /path/to/file x
...
//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔
[root@master ~]# salt 'minion' test.echo,cmd.run,service.status hello,hostname,salt-minion
minion:
----------
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 'minion' network.active_tcp
minion:
----------
0:
----------
local_addr:
192.168.79.129
local_port:
22
remote_addr:
192.168.79.1
remote_port:
58784
1:
----------
local_addr:
192.168.79.129
local_port:
51210
remote_addr:
192.168.79.128
remote_port:
4505
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt 'minion' network.calc_net 192.168.79.129 255.255.0.0
minion:
192.168.0.0/16
[root@master ~]# salt 'minion' network.calc_net 192.168.79.129 255.255.255.240
minion:
192.168.79.128/28
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt 'minion' network.connect baidu.com 80
minion:
----------
comment:
Successfully connected to baidu.com (220.181.38.148) on tcp port 80
result:
True
2.1.4 network.default_route
查看默认路由
[root@master ~]# salt 'minion' network.default_route
minion:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.79.2
interface:
ens33
netmask:
0.0.0.0
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt 'minion' network.get_fqdn
minion:
minion
2.1.6 network.get_hostname
获取主机名
[root@master ~]# salt 'minion' network.get_hostname
minion:
minion
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt 'minion' network.get_route 192.168.80.128
minion:
----------
destination:
192.168.80.128
gateway:
192.168.79.2
interface:
ens33
source:
192.168.79.129
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt 'minion' network.hw_addr ens33
minion:
00:0c:29:b3:58:4f
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt 'minion' network.ifacestartswith 192.168
minion:
- ens33
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt 'minion' network.in_subnet 192.168.79.0/24
minion:
True
2.1.11 network.interface
返回指定网卡的信息
[root@master ~]# salt 'minion' network.interface ens33
minion:
|_
----------
address:
192.168.79.129
broadcast:
192.168.79.255
label:
ens33
netmask:
255.255.255.0
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt 'minion' network.interface_ip ens33
minion:
192.168.79.129
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt 'minion' network.interfaces
minion:
----------
ens33:
----------
hwaddr:
00:0c:29:b3:58:4f
inet:
|_
----------
address:
192.168.79.129
broadcast:
192.168.79.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:feb3:584f
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 'minion' network.ip_addrs
minion:
- 192.168.79.129
2.1.15 network.netstat
返回所有打开的端口和状态
[root@master ~]# salt 'minion' network.netstat
minion:
|_
----------
inode:
17076
local-address:
127.0.0.1:323
program:
chronyd
proto:
udp
recv-q:
0
remote-address:
*:*
send-q:
0
user:
0
...
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt 'minion' network.ping baidu.com
minion:
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=25.4 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=24.6 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=25.1 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=24.5 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 24.538/24.955/25.483/0.379 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt 'minion' network.reverse_ip 192.168.79.129
minion:
129.79.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用
[root@master ~]# salt 'minion' service.available sshd
minion:
True
[root@master ~]# salt 'minion' service.available httpd
minion:
False
2.2.2 service.get_all
获取所有正在运行的服务
[root@master ~]# salt 'minion' service.get_all
minion:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- brandbot
- brandbot.path
- chrony-dnssrv@
- chrony-dnssrv@.timer
- chrony-wait
- chronyd
- console-getty
- console-shell
- container-getty@
- cpupower
- crond
...
2.2.3 service.disabled
设置指定服务开机不自动启动
[root@master ~]# salt 'minion' service.disable nginx
minion:
True
2.2.4 service.enable
设置指定服务开机自动启动
[root@master ~]# salt 'minion' service.enable nginx
minion:
True
2.2.5 service.reload
重新加载指定服务
[root@master ~]# salt 'minion' service.reload nginx
minion:
True
2.2.6 service.stop
停止指定服务
[root@master ~]# salt 'minion' service.stop nginx
minion:
True
2.2.7 service.start
启动指定服务
[root@master ~]# salt 'minion' service.start nginx
minion:
True
2.2.8 service.restart
重启指定服务
[root@master ~]# salt 'minion' service.restart nginx
minion:
True
2.2.9 service.status
查看指定服务的状态
[root@master ~]# salt 'minion' service.status nginx
minion:
True
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils
,可以使用 cmd.run 进行远程安装
[root@master ~]# salt 'minion' pkg.download httpd
minion:
----------
httpd:
/var/cache/yum/packages/httpd-2.4.6-93.el7.centos.x86_64.rpm
2.3.2 pkg.file_list
列出指定包或系统中已安装的所有包的文件
[root@master ~]# salt 'minion' pkg.file_list nginx
minion:
----------
errors:
files:
- /etc/logrotate.d/nginx
- /etc/nginx
- /etc/nginx/conf.d
- /etc/nginx/conf.d/default.conf
- /etc/nginx/fastcgi_params
- /etc/nginx/koi-utf
- /etc/nginx/koi-win
- /etc/nginx/mime.types
- /etc/nginx/modules
- /etc/nginx/nginx.conf
- /etc/nginx/scgi_params
- /etc/nginx/uwsgi_params
- /etc/nginx/win-utf
- /etc/sysconfig/nginx
- /etc/sysconfig/nginx-debug
- /usr/lib/systemd/system/nginx-debug.service
- /usr/lib/systemd/system/nginx.service
- /usr/lib64/nginx
- /usr/lib64/nginx/modules
- /usr/libexec/initscripts/legacy-actions/nginx
- /usr/libexec/initscripts/legacy-actions/nginx/check-reload
- /usr/libexec/initscripts/legacy-actions/nginx/upgrade
- /usr/sbin/nginx
- /usr/sbin/nginx-debug
- /usr/share/doc/nginx-1.18.0
- /usr/share/doc/nginx-1.18.0/COPYRIGHT
- /usr/share/man/man8/nginx.8.gz
- /usr/share/nginx
- /usr/share/nginx/html
- /usr/share/nginx/html/50x.html
- /usr/share/nginx/html/index.html
- /var/cache/nginx
- /var/log/nginx
2.3.3 pkg.group_info
查看包组的信息
[root@master ~]# salt 'minion' pkg.group_info 'Development Tools'
minion:
----------
conditional:
default:
- byacc
- cscope
- ctags
- diffstat
- doxygen
- elfutils
- gcc-gfortran
- git
- indent
- intltool
- patchutils
- rcs
- subversion
- swig
- systemtap
...
2.3.4 pkg.group_list
列出系统中所有的包组
[root@master ~]# salt 'minion' pkg.group_list
minion:
----------
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
...
2.3.5 pkg.install
安装软件
[root@master ~]# salt 'minion' pkg.install httpd
minion:
----------
apr:
----------
new:
1.4.8-5.el7
old:
apr-util:
----------
new:
1.5.2-6.el7
old:
httpd:
----------
new:
2.4.6-93.el7.centos
old:
httpd-tools:
----------
new:
2.4.6-93.el7.centos
old:
mailcap:
----------
new:
2.1.41-2.el7
old:
2.3.6 pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt 'minion' pkg.list_downloaded
minion:
----------
httpd:
----------
2.4.6-93.el7.centos:
----------
creation_date_time:
2020-08-18T07:09:32
creation_date_time_t:
1597748972
path:
/var/cache/yum/packages/httpd-2.4.6-93.el7.centos.x86_64.rpm
size:
2843664
2.3.7 pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt 'minion' pkg.list_pkgs
minion:
----------
NetworkManager:
1:1.18.4-3.el7
NetworkManager-libnm:
1:1.18.4-3.el7
NetworkManager-team:
1:1.18.4-3.el7
NetworkManager-tui:
1:1.18.4-3.el7
NetworkManager-wifi:
1:1.18.4-3.el7
PyYAML:
3.11-1.el7
acl:
2.2.51-15.el7
aic94xx-firmware:
30-6.el7
alsa-firmware:
1.0.28-2.el7
...
2.3.8 pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt 'minion' pkg.owner /usr/sbin/apachectl
minion:
httpd
[root@master ~]# salt 'minion' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
minion:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
2.3.9 pkg.remove
卸载指定软件
[root@master ~]# salt 'minion' pkg.remove httpd
minion:
----------
httpd:
----------
new:
old:
2.4.6-93.el7.centos
2.3.10 pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt 'minion' pkg.upgrade name=libsodium
minion:
----------
libsodium:
----------
new:
1.0.18-1.el7
old:
1.0.16-1.el7
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt 'minion' state.show_highstate
minion:
----------
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.nginx
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10002
nginx-repo:
----------
__env__:
base
__sls__:
web.nginx.nginx
cmd:
|_
----------
name:
yum clean all && yum makecache fast
- run
|_
----------
order:
10001
file:
|_
----------
name:
/etc/yum.repos.d/nginx.repo
|_
----------
source:
salt:///web/nginx/nginx.repo
|_
----------
user:
root
|_
----------
group:
root
|_
----------
mode:
644
- managed
|_
----------
order:
10000
nginx-service:
----------
__env__:
base
__sls__:
web.nginx.nginx
service:
|_
----------
name:
nginx
|_
----------
enable:
True
- running
|_
----------
order:
10003
2.4.2 state.highstate
执行高级状态
[root@master ~]# salt 'minion' state.show_highstate
minion:
----------
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.nginx
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10002
......
......
Summary for minion
------------
Succeeded: 4 (changed=4)
Failed: 0
------------
Total states run: 4
Total run time: 182.436 s
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt 'minion' state.show_state_usage
minion:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.nginx.nginx
2.4.4 state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt 'minion' state.show_top
minion:
----------
base:
- web.nginx.nginx
2.4.5 state.top
执行指定的top file,而不是默认的
[root@master ~]# salt 'minion' state.top service.sls
minion:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx has been disabled, and is in the desired state
Started: 22:14:51.644832
Duration: 367.317 ms
Changes:
----------
nginx:
True
Summary for minion
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
Total run time: 367.317 ms
2.4.6 state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt 'minion' state.show_sls web.nginx.nginx
minion:
----------
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.nginx
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10002
nginx-repo:
----------
__env__:
base
__sls__:
web.nginx.nginx
cmd:
|_
----------
name:
yum clean all && yum makecache fast
- run
|_
----------
order:
10001
file:
|_
----------
name:
/etc/yum.repos.d/nginx.repo
|_
----------
source:
salt:///web/nginx/nginx.repo
|_
----------
user:
root
|_
----------
group:
root
|_
----------
mode:
644
- managed
|_
----------
order:
10000
nginx-service:
----------
__env__:
base
__sls__:
web.nginx.nginx
service:
|_
----------
name:
nginx
|_
----------
enable:
True
- running
|_
----------
order:
10003
2.5 SaltStack常用模块之salt-cp
salt-cp
能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt 'minion' cmd.run 'ls /usr/src'
minion:
debug
kernels
[root@master ~]# salt-cp 'minion' /etc/passwd /usr/src
minion:
----------
/usr/src/passwd:
True
[root@master ~]# salt 'minion' cmd.run 'ls /usr/src'
minion:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp 'minion' /etc/shadow /etc/group /usr/src
minion:
----------
/usr/src/group:
True
/usr/src/shadow:
True
[root@master ~]# salt 'minion' cmd.run 'ls /usr/src'
minion:
debug
group
kernels
passwd
shadow
2.6 SaltStack常用模块之file
2.6.1 file.access
检查指定路径是否存在
[root@master ~]# salt 'minion' cmd.run 'ls /usr/src'
minion:
debug
group
kernels
passwd
shadow
[root@master ~]# salt 'minion' file.access /usr/src/passwd f
minion:
True
[root@master ~]# salt 'minion' file.access /usr/src/abc f
minion:
False
检查指定文件的权限信息
[root@master ~]# salt 'minion' cmd.run 'ls -l /usr/src'
minion:
total 12
drwxr-xr-x. 2 root root 6 Apr 11 2018 debug
-rw-r--r-- 1 root root 449 Aug 18 22:19 group
drwxr-xr-x. 2 root root 6 Apr 11 2018 kernels
-rw-r--r-- 1 root root 846 Aug 18 22:18 passwd
-rw-r--r-- 1 root root 585 Aug 18 22:19 shadow
[root@master ~]# salt 'minion' file.access /usr/src/passwd r
minion:
True
[root@master ~]# salt 'minion' file.access /usr/src/passwd x
minion:
False
2.6.2 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt 'minion' cmd.run 'cat abc'
minion:
hello
[root@master ~]# salt 'minion' file.append /root/abc "hello" "zhangsan" "lisi"
minion:
Wrote 3 lines to "/root/abc"
[root@master ~]# salt 'minion' cmd.run 'cat abc'
minion:
hello
hello
zhangsan
lisi
2.6.3 file.basename
获取指定路径的基名
[root@master ~]# salt 'minion' file.basename '/root/zabbix/abc'
minion:
abc
2.6.4 file.dirname
获取指定路径的目录名
[root@master ~]# salt 'minion' file.dirname '/root/zabbix/abc'
minion:
/root/zabbix
2.6.5 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt 'minion' cmd.run 'md5sum /etc/passwd'
minion:
4299da44f4cb6996df2b3411e2222a7f /etc/passwd
[root@master ~]# salt 'minion' file.check_hash /etc/passwd 4299da44f4cb6996df2b3411e2222a7f
minion:
True
2.6.6 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件, 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改, 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件, 不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@master ~]# salt 'minion' cmd.run 'lsattr /root'
minion:
---------------- /root/anaconda-ks.cfg
---------------- /root/abc
//添加属性
[root@master ~]# salt 'minion' cmd.run 'lsattr /root'
minion:
---------------- /root/anaconda-ks.cfg
---------------- /root/abc
[root@master ~]# salt 'minion' file.chattr /root/abc operator=add attributes=ai
minion:
True
[root@master ~]# salt 'minion' cmd.run 'lsattr /root'
minion:
---------------- /root/anaconda-ks.cfg
----ia---------- /root/abc
给指定文件去除属性
[root@master ~]# salt 'minion' file.chattr /root/abc operator=remove attributes=i
minion:
True
[root@master ~]# salt 'minion' cmd.run 'lsattr /root'
minion:
---------------- /root/anaconda-ks.cfg
-----a---------- /root/abc
2.6.7 file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt 'minion' cmd.run 'lsattr /root'
minion:
---------------- /root/anaconda-ks.cfg
-----a---------- /root/abc
[root@master ~]# salt 'minion' file.chown /root/abc tom tom
minion:
None
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 8
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
2.6.8 file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt 'minion' cmd.run 'ls /root'
minion:
abc
anaconda-ks.cfg
[root@master ~]# salt 'minion' file.copy /root/abc /root/def
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls /root'
minion:
abc
anaconda-ks.cfg
def
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt 'minion' file.copy /tmp /root/test recurse=True
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls /root'
minion:
abc
anaconda-ks.cfg
def
test
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt 'minion' cmd.run 'ls -l test'
minion:
total 0
-rw-r--r-- 1 root root 0 Aug 18 22:38 hehe
drwxr-xr-x 2 root root 6 Aug 18 22:36 systemd-private-3b82fc0ef3424ffcb4448847b1ba9502-chronyd.service-ADLFsk
-rw-r--r-- 1 root root 0 Aug 18 22:38 xiix
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt/test'
minion:
total 0
-rw-r--r-- 1 root root 0 Aug 18 22:39 hehe
-rw-r--r-- 1 root root 0 Aug 18 22:39 jjyy
[root@master ~]# salt 'minion' file.copy /root/test /opt/test recurse=True remove_existing=True
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt/test'
minion:
total 0
-rw-r--r-- 1 root root 0 Aug 18 22:38 hehe
drwxr-xr-x 2 root root 6 Aug 18 22:36 systemd-private-3b82fc0ef3424ffcb4448847b1ba9502-chronyd.service-ADLFsk
-rw-r--r-- 1 root root 0 Aug 18 22:38 xiix
2.6.9 file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
total 0
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
[root@master ~]# salt 'minion' file.directory_exists /opt/test
minion:
True
2.6.10 file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt 'minion' cmd.run 'du -sb /root'
minion:
3616 /root
[root@master ~]# salt 'minion' file.diskusage /root
minion:
3162
2.6.11 file.file_exists
判断指定文件是否存在
[root@master ~]# salt 'minion' cmd.run 'ls /root'
minion:
abc
anaconda-ks.cfg
def
test
[root@master ~]# salt 'minion' file.file_exists /root/abc
minion:
True
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 'minion' cmd.run 'ls -l /root'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 26 Aug 18 22:34 def
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
[root@master ~]# salt 'minion' file.get_gid /root/abc
minion:
1000
2.6.14 file.get_group
获取指定文件的组名
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/abc'
minion:
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 /root/abc
[root@master ~]# salt 'minion' file.get_group /root/abc
minion:
tom
2.6.15 file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt 'minion' cmd.run 'sha256sum /root/abc'
minion:
dd96656b3f114164a208dd9333342a8200288a69c07cc2e849dc1f9fcf2db36b /root/abc
[root@master ~]# salt 'minion' file.get_hash /root/abc
minion:
dd96656b3f114164a208dd9333342a8200288a69c07cc2e849dc1f9fcf2db36b
2.6.16 file.get_mode
获取指定文件的权限,以数字方式显示
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/abc'
minion:
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 /root/abc
[root@master ~]# salt 'minion' file.get_mode /root/abc
minion:
0644
2.6.17 file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt 'minion' cmd.run 'ls -Z /root/anaconda-ks.cfg'
minion:
-rw-------. root root system_u:object_r:admin_home_t:s0 /root/anaconda-ks.cfg
[root@master ~]# salt 'minion' file.get_selinux_context /root/anaconda-ks.cfg
minion:
system_u:object_r:admin_home_t:s0
2.6.18 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt 'minion' cmd.run 'sha256sum /root/abc'
minion:
dd96656b3f114164a208dd9333342a8200288a69c07cc2e849dc1f9fcf2db36b /root/abc
[root@master ~]# salt 'minion' file.get_sum /root/abc
minion:
dd96656b3f114164a208dd9333342a8200288a69c07cc2e849dc1f9fcf2db36b
[root@master ~]# salt 'minion' cmd.run 'md5sum /root/abc'
minion:
d060e840a40adc2aa02ffeb0d53f2d51 /root/abc
[root@master ~]# salt 'minion' file.get_sum /root/abc md5
minion:
d060e840a40adc2aa02ffeb0d53f2d51
2.6.19 file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/abc'
minion:
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 /root/abc
[root@master ~]# salt 'minion' file.get_uid /root/abc
minion:
1000
[root@master ~]# salt 'minion' file.get_user /root/abc
minion:
tom
2.6.20 file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt 'minion' file.gid_to_group 1000
minion:
tom
[root@master ~]# salt 'minion' file.gid_to_group 0
minion:
root
2.6.21 file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt 'minion' file.group_to_gid tom
minion:
1000
[root@master ~]# salt 'minion' file.group_to_gid root
minion:
0
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 'minion' file.is_blkdev /dev/sr0
minion:
True
[root@master ~]# salt 'minion' file.is_blkdev /dev/sr1
minion:
False
2.6.25 file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 26 Aug 18 22:34 def
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
[root@master ~]# salt 'minion' file.mkdir /root/hehe
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 26 Aug 18 22:34 def
drwxr-xr-x 2 root root 6 Aug 18 23:02 hehe
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
[root@master ~]# salt 'minion' file.mkdir /root/haha tom tom 400
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 26 Aug 18 22:34 def
dr-------- 2 tom tom 6 Aug 18 23:02 haha
drwxr-xr-x 2 root root 6 Aug 18 23:02 hehe
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
2.6.26 file.move
移动或重命名
//重命名
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 26 Aug 18 22:34 def
dr-------- 2 tom tom 6 Aug 18 23:02 haha
drwxr-xr-x 2 root root 6 Aug 18 23:02 hehe
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
[root@master ~]# salt 'minion' file.move /root/hehe /root/xixi
minion:
----------
comment:
'/root/hehe' moved to '/root/xixi'
result:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 26 Aug 18 22:34 def
dr-------- 2 tom tom 6 Aug 18 23:02 haha
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
drwxr-xr-x 2 root root 6 Aug 18 23:02 xixi
//移动
[root@master ~]# salt 'minion' file.move /root/xixi /opt
minion:
----------
comment:
'/root/xixi' moved to '/opt'
result:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root/'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 26 Aug 18 22:34 def
dr-------- 2 tom tom 6 Aug 18 23:02 haha
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
total 0
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
drwxr-xr-x 2 root root 6 Aug 18 23:02 xixi
2.6.27 file.prepend
把文本插入指定文件的开头
[root@master ~]# salt 'minion' cmd.run 'cat def'
minion:
zhangsan
lisi
[root@master ~]# salt 'minion' file.prepend /root/def "hehe" "xixi"
minion:
Prepended 2 lines to "/root/def"
[root@master ~]# salt 'minion' cmd.run 'cat def'
minion:
hehe
xixi
zhangsan
lisi
2.6.28 file.sed
修改文本文件的内容
[root@master ~]# salt 'minion' cmd.run 'cat def'
minion:
hehe
xixi xixi xixi xixi
zhangsan xixi
lisi
[root@master ~]# salt 'minion' file.sed /root/def "xixi" "hello"
minion:
----------
pid:
12091
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'minion' cmd.run 'cat def'
minion:
hehe
hello hello hello hello
zhangsan hello
lisi
[root@master ~]# salt 'minion' file.sed /root/def "hello" "world" flags=2
minion:
----------
pid:
12100
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'minion' cmd.run 'cat def'
minion:
hehe
hello world hello hello
zhangsan hello
lisi
2.6.29 file.read
读取文件内容
[root@master ~]# salt 'minion' cmd.run 'cat def'
minion:
hehe
hello world hello hello
zhangsan hello
lisi
[root@master ~]# salt 'minion' file.read /root/def
minion:
hehe
hello world hello hello
zhangsan hello
lisi
2.6.30 file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt 'minion' file.readdir /root
minion:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .bash_history
- .pki
- abc
- test
- haha
- .viminfo
- def.bak
- def
2.6.31 file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 16
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 49 Aug 18 23:09 def
-rw-r--r-- 1 tom tom 49 Aug 18 23:08 def.bak
dr-------- 2 tom tom 6 Aug 18 23:02 haha
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
[root@master ~]# salt 'minion' file.remove /root/def.bak
minion:
True
[root@master ~]# salt 'minion' file.remove /root/haha
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 49 Aug 18 23:09 def
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
2.6.32 file.rename
重命名文件或目录
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
-rw-r--r-- 1 tom tom 49 Aug 18 23:09 def
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
[root@master ~]# salt 'minion' file.rename /root/def /root/xixi
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
-rw-r--r-- 1 tom tom 49 Aug 18 23:09 xixi
2.6.33 file.set_mode
给指定文件设置权限
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
-rw-r--r-- 1 tom tom 49 Aug 18 23:09 xixi
[root@master ~]# salt 'minion' file.set_mode /root/xixi 0400
minion:
0400
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
-r-------- 1 tom tom 49 Aug 18 23:09 xixi
2.6.34 file.symlink
给指定的文件创建软链接
[root@master ~]# salt 'minion' cmd.run 'ls -l /root'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
-r-------- 1 tom tom 49 Aug 18 23:09 xixi
[root@master ~]# salt 'minion' file.symlink /root/abc /opt/a
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /root;ls -l /opt'
minion:
total 12
-rw-r--r-- 1 tom tom 26 Aug 18 22:25 abc
-rw-------. 1 root root 1260 Aug 7 10:22 anaconda-ks.cfg
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
-r-------- 1 tom tom 49 Aug 18 23:09 xixi
total 0
lrwxrwxrwx 1 root root 9 Aug 18 23:37 a -> /root/abc
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
drwxr-xr-x 2 root root 6 Aug 18 23:02 xixi
2.6.35 file.touch
创建空文件或更新时间戳
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
total 0
lrwxrwxrwx 1 root root 9 Aug 18 23:37 a -> /root/abc
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
drwxr-xr-x 2 root root 6 Aug 18 23:02 xixi
[root@master ~]# salt 'minion' file.touch /opt/hehe
minion:
True
[root@master ~]# salt 'minion' file.touch /opt/xixi
minion:
True
[root@master ~]# salt 'minion' cmd.run 'ls -l /opt'
minion:
total 0
lrwxrwxrwx 1 root root 9 Aug 18 23:37 a -> /root/abc
-rw-r--r-- 1 root root 0 Aug 18 23:39 hehe
drwxrwxrwt 8 root root 196 Aug 18 22:38 test
drwxr-xr-x 2 root root 6 Aug 18 23:39 xixi
2.6.36 file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@master ~]# salt 'minion' file.uid_to_user 0
minion:
root
[root@master ~]# salt 'minion' file.uid_to_user 1000
minion:
tom
2.6.37 file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@master ~]# salt 'minion' file.user_to_uid tom
minion:
1000
[root@master ~]# salt 'minion' file.user_to_uid root
minion:
0
2.6.38 file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt 'minion' cmd.run 'cat abc'
minion:
hello
hello
zhangsan
lisi
[root@master ~]# salt 'minion' file.write /root/abc "hello word"
minion:
Wrote 1 lines to "/root/abc"
[root@master ~]# salt 'minion' cmd.run 'cat abc'
minion:
hello word