StaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
查看所有模块
[root@master ~]# salt 'node1' sys.list_modules
node1:
- acl
- aliases
- alternatives
- archive
- artifactory
- at
- baredoc
- beacons
- bigip
- btrfs
- buildout
- chroot
- cloud
- cmd
- composer
- config
- consul
- container_resource
- cp
- cron
- cryptdev
- data
- defaults
- devinfo
- devmap
- dig
- disk
- django
- dnsmasq
- dnsutil
- drbd
- environ
- ethtool
- event
- extfs
- file
- firewalld
- freezer
- gem
- genesis
- git
- glassfish
- gnome
- google_chat
- grafana4
- grains
- group
- hashutil
- helm
- highstate_doc
- hosts
- http
- hue
- incron
- ini
- inspector
- introspect
- iosconfig
- ip
- ipset
- iptables
- jboss7
- jboss7_cli
- jinja
- k8s
- kernelpkg
- key
- keyboard
- kmod
- kubeadm
- locale
- locate
- log
- logrotate
- lowpkg
- lvm
- mandrill
- match
- mattermost
- mine
- minion
- modjk
- mount
- msteams
- nagios_rpc
- namecheap_domains
- namecheap_domains_dns
- namecheap_domains_ns
- namecheap_ssl
- namecheap_users
- network
- nexus
- nftables
- nova
- nspawn
- nxos
- nxos_api
- nxos_upgrade
- openscap
- openstack_config
- opsgenie
- out
- pagerduty
- pagerduty_util
- pam
- parallels
- partition
- peeringdb
- pillar
- pip
- pkg
- pkg_resource
- ps
- publish
- pushover
- pyenv
- quota
- raid
- random
- random_org
- rbenv
- rest_sample_utils
- restartcheck
- ret
- rsync
- rvm
- s3
- s6
- salt_proxy
- salt_version
- saltcheck
- saltutil
- schedule
- scsi
- sdb
- seed
- selinux
- serverdensity_device
- service
- shadow
- slack
- slsutil
- smbios
- smtp
- solrcloud
- sqlite3
- ssh
- state
- status
- statuspage
- supervisord
- sys
- sysctl
- sysfs
- syslog_ng
- system
- telegram
- telemetry
- temp
- test
- timezone
- tls
- travisci
- tuned
- udev
- uptime
- user
- vault
- vbox_guest
- virtualenv
- vsphere
- x509
- xfs
- xml
- zabbix
- zenoss
查看指定模块的函数
不要问为什么不查看所有的,问就是因为太多了,怕虚拟机崩溃
[root@master ~]# salt 'node1' sys.list_functions cmd
node1:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.powershell
- cmd.powershell_all
- cmd.retcode
- cmd.run
- cmd.run_all
- cmd.run_bg
- cmd.run_chroot
- cmd.run_stderr
- cmd.run_stdout
- cmd.script
- cmd.script_retcode
- cmd.shell
- cmd.shell_info
- cmd.shells
- cmd.tty
- cmd.which
- cmd.which_bin
查看指定模块的用法
[root@master ~]# salt 'node1' 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.
All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.
CLI Example:
salt '*' cmd.exec_code ruby 'puts "cheese"'
salt '*' cmd.exec_code ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
#下面部分省略
执行多个模块
SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt 'node1' test.echo,cmd.run,service.status hello,hostname,salt-minion
node1:
----------
cmd.run:
node1
service.status:
True
test.echo:
hello
SaltStack常用模块
network模块
network.active_tcp
返回所有活动的tcp连接
[root@master ~]# salt 'node1' network.active_tcp
node1:
----------
0:
----------
local_addr:
192.168.159.14
local_port:
59818
remote_addr:
192.168.159.13
remote_port:
4505
1:
----------
local_addr:
192.168.159.14
local_port:
22
remote_addr:
192.168.159.1
remote_port:
49367
network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt 'node1' network.calc_net 192.168.159.14 255.255.255.0
node1:
192.168.159.0/24
network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt 'node1' network.connect baidu.com 80
node1:
----------
comment:
Successfully connected to baidu.com (220.181.38.251) on tcp port 80
result:
True
network.default_route
查看默认路由
[root@master ~]# salt 'node1' network.default_route
node1:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.159.2
interface:
ens160
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:
network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt 'node1' network.get_fqdn
node1:
node1
network.get_hostname
获取主机名
[root@master ~]# salt 'node1' network.get_hostname
node1:
node1
network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt 'node1' network.get_route 192.168.159.14
node1:
----------
destination:
192.168.159.14
gateway:
None
interface:
lo
source:
192.168.159.14
network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt 'node1' network.hw_addr ens160
node1:
00:0c:29:83:08:01
network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt 'node1' network.ifacestartswith 192.168
node1:
- ens160
network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt 'node1' network.in_subnet 192.168.159.0/24
node1:
True
[root@master ~]# salt 'node1' network.in_subnet 192.168.159.0/20
node1:
False
network.interface
返回指定网卡的信息
[root@master ~]# salt 'node1' network.interface ens160
node1:
|_
----------
address:
192.168.159.14
broadcast:
192.168.159.255
label:
ens160
netmask:
255.255.255.0
network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt 'node1' network.interface_ip ens160
node1:
192.168.159.14
network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt 'node1' network.interfaces
node1:
----------
ens160:
----------
hwaddr:
00:0c:29:83:08:01
inet:
|_
----------
address:
192.168.159.14
broadcast:
192.168.159.255
label:
ens160
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::2300:4f66:9d65:8a7
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
network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址
[root@master ~]# salt 'node1' network.ip_addrs
node1:
- 192.168.159.14
network.netstat
返回所有打开的端口和状态
[root@master ~]# salt 'node1' network.netstat
node1:
|_
----------
inode:
29387
local-address:
0.0.0.0:22
program:
1054/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
30647
local-address:
192.168.159.14:59818
program:
1531/python3.6
proto:
tcp
recv-q:
0
remote-address:
192.168.159.13:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
0
local-address:
192.168.159.14:34014
program:
-
proto:
tcp
recv-q:
0
remote-address:
192.168.159.13:4506
send-q:
0
state:
TIME_WAIT
user:
0
|_
----------
inode:
33097
local-address:
192.168.159.14:22
program:
1647/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.159.1:49367
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
29398
local-address:
:::22
program:
1054/sshd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt 'node1' network.ping baidu.com
node1:
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=128 time=41.9 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=41.1 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=128 time=43.8 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=4 ttl=128 time=46.4 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 10ms
rtt min/avg/max/mdev = 41.146/43.311/46.423/2.041 ms
network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt 'node1' network.reverse_ip 192.168.159.15
node1:
15.159.168.192.in-addr.arpa
service模块
service.available
判断指定的服务是否可用
[root@master ~]# salt 'node1' service.available sshd
node1:
True
[root@master ~]# salt 'node1' service.available vsftpd
node1:
False
service.get_all
获取所有正在运行的服务
[root@master ~]# salt 'node1' service.get_all
node1:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- arp-ethers
- atd
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- chrony-dnssrv@
- chrony-dnssrv@.timer
- chrony-wait
- chronyd
- clean-mount-point@
- cockpit
- cockpit-motd
- cockpit-wsinstance-http
- cockpit-wsinstance-http-redirect
- cockpit-wsinstance-http-redirect.socket
- cockpit-wsinstance-http.socket
- cockpit-wsinstance-https-factory.socket
- cockpit-wsinstance-https-factory@
- cockpit-wsinstance-https@
- cockpit-wsinstance-https@.socket
- cockpit.socket
- console-getty
- container-getty@
- cpupower
- crond
- cryptsetup-pre.target
- cryptsetup.target
- ctrl-alt-del.target
- dbus
- dbus-org.freedesktop.hostname1
- dbus-org.freedesktop.locale1
# 省略后面内容
service.disabled
检查指定服务是否开机不自动启动
[root@master ~]# salt 'node1' service.disabled httpd
node1:
True
service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt 'node1' service.enabled firewalld
node1:
True
service.disable
设置指定服务开机不自动启动
[root@master ~]# salt 'node1' service.disable firewalld
node1:
True
[root@master ~]# salt 'node1' service.enabled firewalld
node1:
False
service.enable
设置指定服务开机自动启动
[root@master ~]# salt 'node1' service.enable firewalld
node1:
True
[root@master ~]# salt 'node1' service.disabled firewalld
node1:
False
service.reload
重新加载指定服务
[root@master ~]# salt 'node1' service.reload httpd
node1:
True
service.stop
停止指定服务
[root@master ~]# salt 'node1' service.stop httpd
node1:
True
service.start
启动指定服务
[root@master ~]# salt 'node1' service.start httpd
node1:
True
service.restart
重启指定服务
[root@master ~]# salt 'node1' service.restart httpd
node1:
True
service.status
查看指定服务的状态
[root@master ~]# salt 'node1' service.status httpd
node1:
True
pkg(package)模块
pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
[root@master ~]# salt 'node1' cmd.run "yum -y install yum-utils"
node1:
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 1:43:42 ago on Wed Nov 3 17:41:16 2021.
Package yum-utils-4.0.12-3.el8.noarch is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@master ~]# salt 'node1' pkg.download wget
node1:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-8.el8_1.1.x86_64.rpm
pkg.file_list
列出指定包或系统中已安装的所有包的文件
[root@master ~]# salt 'node1' pkg.file_list httpd
node1:
----------
errors:
files:
- /etc/httpd/conf
- /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-optional.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.modules.d/README
- /etc/httpd/conf/httpd.conf
- /etc/httpd/conf/magic
- /etc/httpd/logs
- /etc/httpd/modules
- /etc/httpd/run
- /etc/httpd/state
- /etc/logrotate.d/httpd
- /etc/sysconfig/htcacheclean
- /run/httpd
- /run/httpd/htcacheclean
- /usr/lib/.build-id
- /usr/lib/.build-id/01
- /usr/lib/.build-id/01/319ddc7e9693d5327dbff2f4825f017bb3b2d9
- /usr/lib/.build-id/01/a2952dc4a9a05aceec7230348dded1052df4f8
- /usr/lib/.build-id/02
- /usr/lib/.build-id/02/524c9b52b3264c7fe755d6de3ae0941f25cb44
- /usr/lib/.build-id/06
- /usr/lib/.build-id/06/439bcc914482a0d452b315e67c10082e6f38ae
- /usr/lib/.build-id/08
- /usr/lib/.build-id/08/f763283743b271e570fee309b5b92e978c35b7
- /usr/lib/.build-id/09
- /usr/lib/.build-id/09/3bc18a1e7ac52bf5cfefdccda80262c367f9ac
- /usr/lib/.build-id/0b
- /usr/lib/.build-id/0b/64f176475257c90ddda3c5e938a039bfb2419e
- /usr/lib/.build-id/0c
- /usr/lib/.build-id/0c/9de21b9c3790e14af023c202eff05c4612ba13
- /usr/lib/.build-id/0e
# 省略余下部分
当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt 'node1' pkg.file_list
node1:
----------
errors:
files:
VALUE_TRIMMED
pkg.group_info
查看包组的信息
[root@master ~]# salt 'node1' pkg.group_info 'Development Tools'
node1:
----------
conditional:
default:
- asciidoc
- byacc
- ctags
- diffstat
- git
- intltool
- jna
- ltrace
- patchutils
- perl-Fedora-VSP
- perl-generators
- pesign
- source-highlight
- systemtap
- valgrind
- valgrind-devel
description:
A basic development environment.
group:
Development Tools
id:
None
mandatory:
- autoconf
- automake
- binutils
- bison
- flex
- gcc
- gcc-c++
- gdb
- glibc-devel
- libtool
- make
- pkgconf
- pkgconf-m4
- pkgconf-pkg-config
- redhat-rpm-config
- rpm-build
- rpm-sign
- strace
optional:
- cmake
- expect
- rpmdevtools
- rpmlint
type:
package group
pkg.group_list
列出系统中所有的包组
[root@master ~]# salt 'node1' pkg.group_list
node1:
----------
available:
- Conflicts BaseOS
- Dial-up Networking Support
- Hardware Monitoring Utilities
- Hardware Support
- Large Systems Performance
- Python Web
- Server product core
- Windows File Server
- Additional Development
- Anaconda tools
- Backup Client
- Base
- base-x
- Conflicts AppStream
- Debugging Tools
- Desktop Debugging and Performance Tools
- File and Storage Server
- Fonts
- FTP Server
- GNOME Applications
- GNOME
- Graphics Creation Tools
- Guest Desktop Agents
- Infiniband Support
- Input Methods
- Internet Applications
- Internet Browser
- Java Platform
- Legacy X Window System Compatibility
- Mail Server
- Mainframe Access
- Multimedia
- Network File System Client
- Networking Tools
- Common NetworkManager submodules
- Office Suite and Productivity
- Atomic Host ostree support
- Performance Tools
- Platform Development
- KVM platform specific packages
- Hyper-v platform specific packages
- Printing Client
- Remote Desktop Clients
- Remote Management for Linux
- TeX formatting system
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Basic Web Server
- Workstation product core
available environments:
- Server with GUI
- Server
- Workstation
- Custom Operating System
- Virtualization Host
available languages:
----------
installed:
- Legacy UNIX Compatibility
- Container Management
- Core
- Development Tools
- .NET Core Development
- Graphical Administration Tools
- Guest Agents
- Headless Management
- Network Servers
- VMware platform specific packages
- RPM Development Tools
- Scientific Support
- Security Tools
- Smart Card Support
- Standard
- System Tools
installed environments:
- Minimal Install
pkg.install
安装软件
[root@master ~]# salt 'node1' pkg.install wget
node1:
----------
wget:
----------
new:
1.19.5-8.el8_1.1
old:
pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt 'node1' pkg.list_downloaded
node1:
----------
//手动执行download命令下载的包查看不到
//因为已经被删除
pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt 'node1' pkg.list_pkgs
node1:
----------
GConf2:
3.2.6-22.el8
NetworkManager:
1:1.22.8-4.el8
NetworkManager-libnm:
1:1.22.8-4.el8
NetworkManager-libreswan:
1.2.10-4.el8
NetworkManager-team:
1:1.22.8-4.el8
NetworkManager-tui:
1:1.22.8-4.el8
PackageKit:
1.1.12-4.el8
PackageKit-glib:
1.1.12-4.el8
abattis-cantarell-fonts:
0.0.25-4.el8
# 省略余下部分
pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt 'node1' pkg.owner /usr/sbin/apachectl
node1:
httpd
[root@master ~]# salt 'node1' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
node1:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
pkg.remove
卸载指定软件
[root@master ~]# salt 'node1' cmd.run 'rpm -qa| grep wget'
node1:
wget-1.19.5-8.el8_1.1.x86_64
[root@master ~]# salt 'node1' pkg.remove wget
node1:
----------
wget:
----------
new:
old:
1.19.5-8.el8_1.1
pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt 'node1' pkg.upgrade wget
node1:
----------
wget:
----------
new:
1.19.5-8.el8_1.1
old:
1.14-18.el7_6.1
state模块
state.show_highstate
[root@master base]# salt 'node1' state.show_highstate
node1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.install
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
state.highstate
执行高级状态
[root@master base]# salt 'node1' state.highstate
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 21:33:17.182364
Duration: 1016.702 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 21:33:18.205298
Duration: 398.451 ms
Changes:
----------
httpd:
True
Summary for node1
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 1.415 s
state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master base]# salt 'node1' state.show_state_usage
node1:
----------
base:
----------
count_all:
2
count_unused:
1
count_used:
1
unused:
- top
used:
- web.apache.install
dev:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
prod:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
state.show_top
返回minion将用于highstate的顶级数据
[root@master base]# salt 'node1' state.show_top
node1:
----------
base:
- web.apache.install
state.top
执行指定的top file,而不是默认的
[root@master base]# salt 'node1' state.top top.sls
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 21:36:52.239216
Duration: 1049.4 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 21:36:53.292081
Duration: 49.818 ms
Changes:
Summary for node1
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.099 s
state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master base]# salt 'node1' state.show_sls web.apache.install
node1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.install
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
user模块
//参数
- name //要添加的用户名 LOGIN
- uid //新账户的用户ID
- gid //新帐户的主要组的名称或 ID
- groups //新账户的补充组列表
- home //新帐户的主目录
- shell //新账户的登录shell
- unique //如果不是 True,则用户帐户可以具有非唯一 UID
- system //创建系统账户
- fullname //全名的 GECOS 字段
- roomnumber //房间号的 GECOS 字段
- workphone //工作电话的 GECOS 字段
- homephone //家庭电话的 GECOS 字段
- other //其他信息的 GECOS 字段
- createhome //创建用户的主目录
- loginclass //新帐户的登录类 (OpenBSD)
- nologinit //不要将用户添加到 lastlog 和 faillog 数据库
- root //chroot 进入的目录
- usergroup //创建用户并将其添加到同名的新主要组
user.add
[root@master ~]# salt 'node1' user.add zcd
node1:
True
user.chuid
更改用户UID
[root@master ~]# salt 'node1' user.chuid dzc 6000
node1:
True
user.chgid
更改用户gid
[root@master ~]# salt 'node1' user.chgid dzc 1001
node1:
True
user.chgroups
更改此用户所属的组
[root@master ~]# salt 'node1' user.chgroups dzc root
node1:
True
user.chhome
更改用户的主目录,如果旧的主目录存在,则为持久传递 True 以将文件移动到新的主目录。
[root@master ~]# salt 'node1' user.chhome dzc /home/root True
node1:
True
chshell
更改用户的默认shell
[root@master ~]# salt 'node1' user.chshell dzc /sbin/nologin
node1:
True
user.info
返回用户信息
[root@master ~]# salt 'node1' user.info dzc
node1:
----------
fullname:
zi chen
gid:
1001
groups:
- dzc
- root
home:
/home/root
homephone:
1728390496
name:
dzc
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
6000
workphone:
list_users
返回所有用户的列表
[root@master ~]# salt 'node1' user.list_users
node1:
- adm
- admin
- apache
- bin
- chrony
- clevis
- cockpit-ws
- cockpit-wsinstance
- daemon
- dbus
- dzc
- ftp
- games
- halt
- libstoragemgmt
- lp
- mail
- nginx
- nobody
- operator
- pesign
- polkitd
- rngd
- root
- setroubleshoot
- shutdown
- sshd
- sssd
- sync
- systemd-coredump
- systemd-resolve
- tcpdump
- tss
- unbound
- zcd
get_loginclas
获取用户的登录类
[root@master ~]# salt 'node1' user.get_loginclass dzc
node1:
False
user.getent
返回所有用户的所有信息列表
[root@master ~]# salt 'node1' user.getent
node1:
|_
----------
fullname:
root
gid:
0
groups:
- root
home:
/root
homephone:
name:
root
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
0
workphone:
|_
----------
fullname:
bin
gid:
1
groups:
- bin
home:
/bin
homephone:
name:
bin
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
1
workphone:
|_
# 省略余下部分
user.list_groups
返回指定用户所属的组列表
[root@master ~]# salt 'node1' user.list_groups dzc
node1:
- dzc
- root
user.primary_group
返回指定用户的主要组
[root@master ~]# salt 'node1' user.primary_group dzc
node1:
dzc
user.renam
更改命名用户的用户名
[root@master ~]# salt 'node1' user.rename dzc dengzc
node1:
True
user.delete
从minion中删除一个用户
[root@master ~]# salt 'node1' user.delete dengzc remove=True force=True
node1:
True
cp模块
先查看/srv/salt下的整个文件系统
[root@master ~]# tree /srv/
/srv/
├── pillar
│ ├── base
│ └── prod
└── salt
└── base
├── test
│ ├── testfile
│ ├── testfile1
│ └── testfile2
│ ├── testfile3
│ └── testfile4
├── top.sls
└── web
└── apache
└── install.sls
cp.cache_dest
如果使用 缓存,则返回文件的预期缓存路径 cp.cache_file
[root@master ~]# salt 'node1' cp.cache_dest salt://test/testfile1
node1:
/var/cache/salt/minion/files/base/test/testfile1
这仅返回 expected 路径,它不会告诉您 URL 是否真的被缓存。要检查 URL 是否已缓存,请 cp.is_cached改用。
cp.cache_dir
从 master 下载并缓存目录下的所有内容
[root@master ~]# salt 'node1' cp.cache_dir salt://test/testfile2/
node1:
- /var/cache/salt/minion/files/base/test/testfile2/testfile3
- /var/cache/salt/minion/files/base/test/testfile2/testfile4
[root@node1 ~]# ls /var/cache/salt/minion/files/base/test/testfile2/
testfile3 testfile4
cp.cache_file
用于在 Minion 上缓存单个文件
返回 Minion 上新缓存文件的位置
[root@master ~]# salt 'node1' cp.cache_file salt://test/testfile1
node1:
/var/cache/salt/minion/files/base/test/testfile1
[root@node1 test]# pwd
/var/cache/salt/minion/files/base/test
[root@node1 test]# ls
testfile1 testfile2
cp.cache_files
用于从Master收集许多文件,收集的文件将保存在反映从Master检索到的路径的minion cachedir中
//先清空node1上的testfile,防止文件重名
[root@node1 ~]# cd /var/cache/salt/minion/files/base/test/
[root@node1 test]# ls
testfile1 testfile2
[root@node1 test]# rm -rf testfile1 testfile2
[root@node1 test]# ls
[root@node1 test]#
[root@master ~]# salt 'node1' cp.cache_files salt://test/testfile,salt://test/testfile2/
node1:
- /var/cache/salt/minion/files/base/test/testfile
- False //不能使用文件夹,不然为False状态
[root@master ~]# salt 'node1' cp.cache_files salt://test/testfile,salt://test/testfile2/testfile3
node1:
- /var/cache/salt/minion/files/base/test/testfile
- /var/cache/salt/minion/files/base/test/testfile2/testfile3
//使用文件则成功
[root@node1 test]# pwd
/var/cache/salt/minion/files/base/test
[root@node1 test]# ls
testfile testfile2
[root@node1 test]# ls testfile2
testfile3
cp.cache_local_file
在 localfiles 缓存中缓存 minion 上的本地文件
[root@master ~]# salt 'node1' cp.cache_local_file /etc/hosts
node1:
/var/cache/salt/minion/localfiles/etc/hosts
[root@node1 etc]# pwd
/var/cache/salt/minion/localfiles/etc
[root@node1 etc]# ls
hosts
cp.cache_master
检索 master 上的所有文件并在本地缓存它们
[root@master ~]# salt 'node1' cp.cache_master
node1:
- /var/cache/salt/minion/files/base/test/testfile
- /var/cache/salt/minion/files/base/test/testfile1
- /var/cache/salt/minion/files/base/test/testfile2/testfile3
- /var/cache/salt/minion/files/base/test/testfile2/testfile4
- /var/cache/salt/minion/files/base/top.sls
- /var/cache/salt/minion/files/base/web/apache/install.sls
cp.envs
列出文件服务器的可用环境
[root@master ~]# salt 'node1' cp.envs
node1:
- prod
- dev
- base
cp.get_dir
用于从 salt master 递归复制目录
[root@master ~]# salt 'node1' cp.get_dir salt://web/apache/ /minion/dest
node1:
- /minion/dest/apache/install.sls
[root@node1 ~]# ls /minion/dest/apache/install.sls
/minion/dest/apache/install.sls
cp.get_file
用于从 salt master 获取单个文件
这个命令只会获取那一个文件,而不是像cp.get_dir一样包括目录一起获取
[root@master ~]# salt 'node1' cp.get_file salt://test/testfile2/testfile4 /minion/dest
node1:
/minion/dest/testfile4
[root@node1 ~]# ls /minion/dest/testfile4
/minion/dest/testfile4
可以在源文件名和目标文件名上启用模板渲染
salt '*' cp.get_file "salt://{{grains.os}}/vimrc" /etc/vimrc template=jinja
此示例将指示所有 Salt Minion 从与其操作系统粒度相同名称的目录下载 vimrc 并将其复制到 /etc/vimrc
cp.get_file_str
从 URL 下载文件到 Minion 缓存目录并返回该文件的内容
//文件内容为空,则打印空
[root@master ~]# salt 'node1' cp.get_file_str salt://test/testfile
node1:
//添加内容
[root@master ~]# cat /srv/salt/base/test/testfile
Welcome to WuHan!!!
[root@master ~]# salt 'node1' cp.get_file_str salt://test/testfile
node1:
Welcome to WuHan!!!
cp.get_template
在设置之前将文件渲染为模板。警告,对于非破坏旧 API,顺序与 fileclient.cp 中的顺序不同。
[root@master ~]# salt 'node1' cp.get_template salt://test/testfile2/testfile3 /minion/dest
node1:
/minion/dest
[root@node1 ~]# ls /minion/dest/
apache __salt.tmp.8s0k37h6 testfile4
cp.get_url
用于从 URL 获取单个文件。
从中下载文件的 URL。支持的URL方案是:salt://, http://,https://,ftp://,s3://,swift://和 file://(本地文件系统)。如果未指定方案,则这等效于使用file://. 如果file://给出了URL,则该函数仅返回本地文件系统上该文件的绝对路径。False如果 Salt 无法从salt://URL获取文件,则该函数返回。
[root@master ~]# salt 'node1' cp.get_url salt://test/testfile /tmp/this_file_is_mine
node1:
/tmp/this_file_is_mine
[root@master ~]# salt 'node1' cp.get_url http://www.slashdot.org /tmp/index.html
node1:
/tmp/index.html
[root@node1 ~]# ls /tmp/
hsperfdata_root __salt.tmp.3_jfmjrk __salt.tmp.9nlvcr6s __salt.tmp.g27trdt8 vmware-root_1003-4290756405 vmware-root_1029-4290101030
index.html __salt.tmp.42c6a1k8 __salt.tmp.agkpxmyz __salt.tmp.p33ol30r vmware-root_1006-2966103406 vmware-root_1031-4281646756
ks-script-wpl2r9pp __salt.tmp.88y5r_o5 __salt.tmp.e636tpep systemd-private-37e54037f02e4dcba1730fd33126e9f7-httpd.service-u0wRml vmware-root_1020-2990678780
__salt.tmp.0560mh8o __salt.tmp.99o50n6v __salt.tmp.eo4cbt2c this_file_is_mine vmware-root_1021-4256545058
要简单地返回文件内容,请将目标设置为 None. 这适用于salt://,http://,httpshttps:// 和file://网址。该文件被拿来http://和 https://不被缓存。
cp.hash_file
返回文件的哈希值,以获取盐主文件服务器上文件的哈希值,在路径前加上 salt://
[root@master ~]# salt 'node1' cp.hash_file salt://test/testfile2/testfile3
node1:
----------
hash_type:
sha256
hsum:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
cp.is_cached
如果文件在 minion 本地缓存,则返回文件的完整路径,否则返回空字符串
[root@master ~]# salt 'node1' cp.is_cached salt://test/testfile
node1:
/var/cache/salt/minion/files/base/test/testfile
cp.list_master
列出存储在 master 上的所有文件
[root@master ~]# salt 'node1' cp.list_master
node1:
- test/testfile
- test/testfile1
- test/testfile2/testfile3
- test/testfile2/testfile4
- top.sls
- web/apache/install.sls
cp.list_master_dirs
列出存储在 master 上的所有目录
[root@master ~]# salt 'node1' cp.list_master_dirs
node1:
- test
- test/testfile2
- web
- web/apache
cp.list_master_symlinks
列出存储在 master 上的所有符号链接
[root@master ~]# salt 'node1' cp.list_master_symlinks
node1:
---------- //没有链接
cp.list_minion
列出minion上缓存的所有文件
[root@master ~]# salt 'node1' cp.list_minion
node1:
- /var/cache/salt/minion/files/base/test/testfile
- /var/cache/salt/minion/files/base/test/testfile1
- /var/cache/salt/minion/files/base/test/testfile2/testfile3
- /var/cache/salt/minion/files/base/test/testfile2/testfile4
- /var/cache/salt/minion/files/base/top.sls
- /var/cache/salt/minion/files/base/web/apache/install.sls
- /var/cache/salt/minion/localfiles/etc/hosts
cp.list_states
列出环境中所有可用的状态模块
[root@master ~]# salt 'node1' cp.list_states
node1:
- top
- web.apache.install
cp.push
警告推送到 master 的文件将具有全局读取权限
从minion向master推送一个文件,文件会保存到salt master在master的minion文件cachedir中(默认为/var/cache/salt/master/minions/minion-id/files)
由于此功能允许 Minion 将文件推送到主服务器,因此出于安全目的,默认情况下它是禁用的。要启用,file_recv请True在主配置文件中设置 为,然后重新启动主。
[root@master ~]# vim /etc/salt/master
file_recv: True //将此行取消注释并设置为True
[root@master ~]# systemctl restart salt-master.service
[root@master ~]# salt 'node1' cp.push /etc/fstab
node1:
True
[root@master ~]# salt 'node1' cp.push /etc/system-release keep_symlinks=True
node1:
True
[root@master ~]# salt 'node1' cp.push /etc/fstab upload_path='/new/path/fstab'
node1:
True
[root@master ~]# salt 'node1' cp.push /tmp/filename remove_source=True
node1:
False
cp.push_dir
与push一样,但多了一个可以使用 globbing 匹配特定文件的 glob。
[root@master ~]# salt 'node1' cp.push_dir /etc/modprobe.d/ glob='*.conf'
node1:
True
recv
与 salt-cp 一起使用,传递文件字典和目的地。
这个函数通过 salt-cp 从 master 接收小的快速复制文件。它不能通过 CLI 工作。
recv_chunked
此函数接收复制到 minion 的文件salt-cp,不打算直接在 CLI 上使用。
cp.stat_file
返回文件的权限,以获取 salt 主文件服务器上文件的权限,在路径前加上 salt://
[root@master ~]# salt 'node1' cp.stat_file salt://test/testfile
node1:
0644
salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
拷贝单个文件到目标主机的/usr/src目录下
//查看目标文件夹
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
debug
kernels
//使用salt-cp命令
[root@master ~]# salt-cp 'node1' /etc/passwd /usr/src
node1:
----------
/usr/src/passwd:
True
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
debug
kernels
passwd
拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp 'node1' /srv/salt/base/test/testfile /srv/salt/base/test/testfile1 /usr/src
node1:
----------
/usr/src/testfile:
True
/usr/src/testfile1:
True
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
debug
kernels
passwd
testfile
testfile1
file模块
file.access
检查指定路径是否存在
[root@master ~]# salt 'node1' cmd.run 'ls /usr/src'
node1:
debug
kernels
passwd
testfile
testfile1
[root@master ~]# salt 'node1' file.access /usr/src/passwd f
node1:
True
[root@master ~]# salt 'node1' file.access /usr/src/123 f
node1:
False
检查指定文件的权限信息
[root@master ~]# salt 'node1' cmd.run 'ls -l /usr/src/'
node1:
total 8
drwxr-xr-x. 2 root root 6 Aug 12 2018 debug
drwxr-xr-x. 3 root root 35 Nov 1 16:47 kernels
-rw-r--r-- 1 root root 1724 Nov 4 18:25 passwd
-rw-r--r-- 1 root root 20 Nov 4 18:26 testfile
-rw-r--r-- 1 root root 0 Nov 4 18:26 testfile1
[root@master ~]# salt 'node1' file.access /usr/src/passwd r
node1:
True
[root@master ~]# salt 'node1' file.access /usr/src/passwd x
node1:
False
[root@master ~]# salt 'node1' file.access /usr/src/passwd w
node1:
True
file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt 'node1' cmd.run 'ls -l /usr/src/testfile1'
node1:
-rw-r--r-- 1 root root 8 Nov 4 18:32 /usr/src/testfile1
[root@master ~]# salt 'node1' file.append /usr/src/testfile1 "welcome"
node1:
Wrote 1 lines to "/usr/src/testfile1"
[root@master ~]# salt 'node1' cmd.run 'cat /usr/src/testfile1'
node1:
welcome
file.basename
获取指定路径的基名
[root@master ~]# salt 'node1' file.basename '/usr/src/testfile'
node1:
testfile
file.dirname
获取指定路径的目录名
[root@master ~]# salt 'node1' file.dirname '/usr/src/testfile'
node1:
/usr/src
file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt 'node1' cmd.run 'md5sum /etc/passwd'
node1:
bcffdf970725ecbafbe406d475cfaa70 /etc/passwd
[root@master ~]# salt 'node1' file.check_hash /etc/passwd bcffdf970725ecbafbe406d475cfaa70
node1:
True
file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件, 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改, 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件, 不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/wget-1.14-18.el7_6.1.x86_64.rpm
-------------------- /root/123
//添加属性
[root@master ~]# salt 'node1' file.chattr /root/123 operator=add attributes=ai
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/wget-1.14-18.el7_6.1.x86_64.rpm
----ia-------------- /root/123
给指定文件去除属性
[root@master ~]# salt 'node1' file.chattr /root/123 operator=remove attributes=i
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/wget-1.14-18.el7_6.1.x86_64.rpm
-----a-------------- /root/123
file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 552
-rw-r--r-- 1 dzc dzc 0 Nov 4 18:40 123
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt 'node1' file.chown /root/123 root root
node1:
None
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 552
-rw-r--r-- 1 root root 0 Nov 4 18:40 123
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 552
-rw-r--r-- 1 root root 0 Nov 4 18:40 123
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt 'node1' file.copy /root/123 /root/321
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 552
-rw-r--r-- 1 root root 0 Nov 4 18:40 123
-rw-r--r-- 1 root root 0 Nov 4 18:59 321
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt 'node1' file.copy /tmp/ /root/abc recurse=True
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 556
-rw-r--r-- 1 root root 0 Nov 4 18:40 123
-rw-r--r-- 1 root root 0 Nov 4 18:59 321
drwxrwxrwt 17 root root 4096 Nov 4 19:00 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abc'
node1:
total 192
-rw-r--r-- 1 root root 33 Nov 4 19:00 __salt.tmp.0560mh8o
-rw-r--r-- 1 root root 26 Nov 4 19:00 __salt.tmp.3_jfmjrk
-rw-r--r-- 1 root root 28 Nov 4 19:00 __salt.tmp.42c6a1k8
-rw-r--r-- 1 root root 24 Nov 4 19:00 __salt.tmp.88y5r_o5
-rw-r--r-- 1 root root 29 Nov 4 19:00 __salt.tmp.99o50n6v
-rw-r--r-- 1 root root 26 Nov 4 19:00 __salt.tmp.9nlvcr6s
-rw-r--r-- 1 root root 30 Nov 4 19:00 __salt.tmp.agkpxmyz
-rw-r--r-- 1 root root 25 Nov 4 19:00 __salt.tmp.e636tpep
-rw-r--r-- 1 root root 26 Nov 4 19:00 __salt.tmp.eo4cbt2c
-rw-r--r-- 1 root root 30 Nov 4 19:00 __salt.tmp.g27trdt8
-rw-r--r-- 1 root root 32 Nov 4 19:00 __salt.tmp.p33ol30r
# 省略
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/abc'
node1:
total 0
-rw-r--r-- 1 root root 0 Nov 4 19:08 1
-rw-r--r-- 1 root root 0 Nov 4 19:08 2
-rw-r--r-- 1 root root 0 Nov 4 19:08 3
[root@master ~]# salt 'node1' file.copy /opt/abc/ /root/abc/ recurse=True remove_existing=True
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abc'
node1:
total 0
-rw-r--r-- 1 root root 0 Nov 4 19:08 1
-rw-r--r-- 1 root root 0 Nov 4 19:08 2
-rw-r--r-- 1 root root 0 Nov 4 19:08 3
file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 0
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
[root@master ~]# salt 'node1' file.directory_exists /opt/abc
node1:
True
file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt 'node1' cmd.run 'du -sb /opt'
node1:
50 /opt
[root@master ~]# salt 'node1' file.diskusage /opt
node1:
0
file.file_exists
判断指定文件是否存在
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 552
-rw-r--r-- 1 root root 0 Nov 4 18:40 123
-rw-r--r-- 1 root root 0 Nov 4 18:59 321
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt 'node1' file.file_exists /root/a
node1:
False
[root@master ~]# salt 'node1' file.file_exists /root/123
node1:
True
[root@master ~]# salt 'node1' file.file_exists /root/abc
node1:
False //因为abc是目录而非文件
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
file.get_gid
获取指定文件的gid
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/123'
node1:
-rw-r--r-- 1 root root 0 Nov 4 18:40 /root/123
[root@master ~]# salt 'node1' file.get_gid /root/123
node1:
0
file.get_group
获取指定文件的组名
[root@master ~]# salt 'node1' file.get_group /root/123
node1:
root
file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt 'node1' cmd.run 'sha256sum /root/123'
node1:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 /root/123
[root@master ~]# salt 'node1' file.get_hash /root/123
node1:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
file.get_mode
获取指定文件的权限,以数字方式显示
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/123'
node1:
-rw-r--r-- 1 root root 0 Nov 4 18:40 /root/123
[root@master ~]# salt 'node1' file.get_mode /root/123
node1:
0644
file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt 'node1' cmd.run 'ls -Z /root/123'
node1:
? /root/123
[root@master ~]# salt 'node1' file.get_selinux_context /root/123
node1:
No selinux context information is available for /root/123
file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt 'node1' cmd.run 'sha256sum /root/123'
node1:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 /root/123
[root@master ~]# salt 'node1' file.get_sum /root/123
node1:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
[root@master ~]# salt 'node1' cmd.run 'md5sum /root/123'
node1:
d41d8cd98f00b204e9800998ecf8427e /root/123
[root@master ~]# salt 'node1' file.get_sum /root/123 md5
node1:
d41d8cd98f00b204e9800998ecf8427e
file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/123'
node1:
-rw-r--r-- 1 root root 0 Nov 4 18:40 /root/123
[root@master ~]# salt 'node1' file.get_uid /root/123
node1:
0
[root@master ~]# salt 'node1' file.get_user /root/123
node1:
root
file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt 'node1' file.gid_to_group 1000
node1:
admin
[root@master ~]# salt 'node1' file.gid_to_group 0
node1:
root
file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt 'node1' file.group_to_gid root
node1:
0
[root@master ~]# salt 'node1' file.group_to_gid dzc
node1:
1003
file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i -B2
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
示例:
[root@master ~]# salt 'node1' file.grep /etc/passwd nobody
node1:
----------
pid:
8565
retcode:
0
stderr:
stdout:
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i
node1:
----------
pid:
10706
retcode:
0
stderr:
stdout:
IPADDR=192.168.159.14
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -B2
node1:
----------
pid:
12153
retcode:
0
stderr:
stdout:
DEVICE="ens160"
ONBOOT="yes"
IPADDR=192.168.159.14
[root@master ~]# salt 'node1' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
node1:
----------
pid:
15243
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-ens160
file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt 'node1' cmd.run 'ls -l /dev/sr0'
node1:
brw-rw---- 1 root cdrom 11, 0 Nov 4 21:15 /dev/sr0
[root@master ~]# salt 'node1' file.is_blkdev /dev/sr0
node1:
True
file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt 'node1' cmd.run 'lsattr /root/abc'
node1:
-------------------- /root/abc/1
-------------------- /root/abc/2
-------------------- /root/abc/3
[root@master ~]# salt 'node1' cmd.run 'chattr +i /root/abc'
node1:
[root@master ~]# salt 'node1' cmd.run 'lsattr /root/abc'
node1:
----i--------------- /root/abc/1
----i--------------- /root/abc/2
----i--------------- /root/abc/3
[root@master ~]# salt 'node1' file.lsattr /root/abc
node1:
----------
/root/abc/1:
/root/abc/2:
/root/abc/3:
file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 552
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt 'node1' file.mkdir /root/1234
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 552
drwxr-xr-x 2 root root 6 Nov 4 21:19 1234
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt 'node1' file.mkdir /root/aa dzc dzc 600
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 552
drwxr-xr-x 2 root root 6 Nov 4 21:19 1234
drw------- 2 dzc dzc 6 Nov 4 21:20 aa
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
file.move
移动或重命名
//重命名
[root@master ~]# salt 'node1' file.move /root/123 /root/321
node1:
----------
comment:
'/root/123' moved to '/root/321'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 552
-rw-r--r-- 1 root root 0 Nov 4 18:40 321
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
//移动
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 0
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
[root@master ~]# salt 'node1' file.move /root/321 /opt/
node1:
----------
comment:
'/root/321' moved to '/opt/'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 0
-rw-r--r-- 1 root root 0 Nov 4 18:40 321
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
file.rename
重命名文件或目录
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 552
drwxr-xr-x 2 root root 6 Nov 4 21:19 1234
drw------- 2 dzc dzc 6 Nov 4 21:20 aa
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt 'node1' file.rename /root/aa /root/bb
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 552
drwxr-xr-x 2 root root 6 Nov 4 21:19 1234
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
drw------- 2 dzc dzc 6 Nov 4 21:20 bb
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
file.set_mode
给指定文件设置权限
[root@master ~]# salt 'node1' file.set_mode /root/bb 0400
node1:
0400
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 552
drwxr-xr-x 2 root root 6 Nov 4 21:19 1234
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
dr-------- 2 dzc dzc 6 Nov 4 21:20 bb
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
file.symlink
给指定的文件创建软链接
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 552
drwxr-xr-x 2 root root 6 Nov 4 21:19 1234
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
dr-------- 2 dzc dzc 6 Nov 4 21:20 bb
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt 'node1' file.symlink /root/bb /opt/123
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root;ls -l /opt/'
node1:
total 552
drwxr-xr-x 2 root root 6 Nov 4 21:19 1234
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-------. 1 root root 1577 Nov 1 16:52 anaconda-ks.cfg
dr-------- 2 dzc dzc 6 Nov 4 21:20 bb
-rw-r--r-- 1 root root 560272 May 16 2019 wget-1.14-18.el7_6.1.x86_64.rpm
total 0
lrwxrwxrwx 1 root root 8 Nov 7 22:28 123 -> /root/bb
-rw-r--r-- 1 root root 0 Nov 4 18:40 321
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
file.touch
创建空文件或更新时间戳
[root@master ~]# salt 'node1' file.touch /opt/cc
node1:
True
[root@master ~]# salt 'node1' file.touch /opt/aa
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 0
lrwxrwxrwx 1 root root 8 Nov 7 22:28 123 -> /root/bb
-rw-r--r-- 1 root root 0 Nov 4 18:40 321
-rw-r--r-- 1 root root 0 Nov 7 22:29 aa
drwxr-xr-x 2 root root 33 Nov 4 19:08 abc
-rw-r--r-- 1 root root 0 Nov 7 22:29 cc
file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@master ~]# salt 'node1' file.uid_to_user 0
node1:
root
[root@master ~]# salt 'node1' file.uid_to_user 1000
node1:
admin
file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@master ~]# salt 'node1' file.user_to_uid admin
node1:
1000
[root@master ~]# salt 'node1' file.user_to_uid root
node1:
0
[root@master ~]# salt 'node1' file.user_to_uid dzc
node1:
1003
file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt 'node1' cmd.run 'cat /root/123'
node1:
wo shi da wawa
[root@master ~]# salt 'node1' file.write /root/123 "ni shi wo de wa wa "
node1:
Wrote 1 lines to "/root/123"
[root@master ~]# salt 'node1' cmd.run 'cat /root/123'
node1:
ni shi wo de wa wa
评论区