SaltStack常用模块
文章目录
- SaltStack常用模块
- 1. SaltSack模块介绍:
- 2.SaltStack常用模块:
- 2.1 常用模块之network
- 2.1.1 network.active_tcp
- 2.1.2 nwetwork.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.1.18 network.convert_cidr
- 2.2 SaltStack常用模块之service
- 2.3 SaltStack常用模块之pkg
- 2.4 SaltStack常用模块之state
- 2.5 SaltStack常用模块之user
- 2.6 SaltStack之salt-cp
- 2.7 SaltStack常用模块之file
- 2.7.1 file.access
- 2.7.2 file.append
- 2.7.3 file.basename
- 2.7.4 file.dirname
- 2.7.5 file.check_hash
- 2.7.6 file.chattr
- 2.7.7 file.chown
- 2.7.8 file.copy
- 2.7.9 file.directory_exists
- 2.7.10 file.diskusage
- 2.7.11 file.file_exists
- 2.7.12 file.find
- 2.7.13 file.get_gid
- 2.7.14 file.get_group
- 2.7.15 file.get_hash
- 2.7.16 file.get_mode
- 2.7.17 file.get_selinux_context
- 2.7.18 file.get_sum
- 2.7.19 file.get_uid与file.get_user
- 2.7.20 file.gid_to_group
- 2.7.21 file.group_to_gid
- 2.7.22 file.user_to_uid
- 2.7.23 file.uid_to_user
- 2.7.24 file.grep
- 2.7.25 file.is_blkdev
- 2.7.26 file.lsattr
- 2.7.27 file.mkdir
- 2.7.28 file.move
- 2.7.29 file.prepend
- 2.7.30 file.sed
- 2.7.31 file.read
- 2.7.32 file.readdir
- 2.7.33 file.remove
- 2.7.34 rename
- 2.7.35 file.set_mode
- 2.7.36 file.symlink
- 2.7.37 file.touch
- 2.7.38 file.write
1. SaltSack模块介绍:
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
# 查看所有module列表
[root@master ~]# salt 'master' sys.list_modules
master:
- acl
- aliases
- alternatives
- apache
- archive
- artifactory
- baredoc
- beacons
- bigip
- btrfs
- buildout
- chroot
- cloud
- cmd
- composer
- config
- consul
- container_resource
- cp
- cron
- cryptdev
- data
- defaults
- devinfo
.....................
# 查看指定module的所有function(函数)
[root@master ~]# salt 'master' sys.list_functions user
master:
- user.add
- user.chfullname
- user.chgid
- user.chgroups
- user.chhome
- user.chhomephone
- user.chloginclass
- user.chother
- user.chroomnumber
- user.chshell
- user.chuid
- user.chworkphone
- user.delete
- user.get_loginclass
- user.getent
- user.info
- user.list_groups
- user.list_users
- user.primary_group
- user.rename
# 查看指定module的用法(b)
[root@master ~]# salt 'master' sys.doc user |less
user.add:
Add a user to the minion
name
Username LOGIN to add
uid
User ID of the new account
gid
Name or ID of the primary group of the new account
groups
List of supplementary groups of the new account
home
Home directory of the new account
shell
Login shell of the new accoun
...................................
# SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt 'node1' test.echo,cmd.run,service.status hello,date,sshd
node1:
----------
cmd.run:
Wed Nov 3 06:01:25 EDT 2021
service.status:
True
test.echo:
hello
2.SaltStack常用模块:
2.1 常用模块之network
2.1.1 network.active_tcp
返回所有活动的tcp连接(与ss -ant命令差不多)
[root@master ~]# salt 'node1' network.active_tcp
node1:
----------
0:
----------
local_addr:
192.168.220.10
local_port:
22
remote_addr:
192.168.220.1
remote_port:
62157
1:
----------
local_addr:
192.168.220.10
local_port:
49776
remote_addr:
192.168.220.9
remote_port:
4505
2:
----------
local_addr:
192.168.220.10
local_port:
22
remote_addr:
192.168.220.1
remote_port:
62158
2.1.2 nwetwork.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt 'node1' network.calc_net 192.168.220.10 255.255.255.0
node1:
192.168.220.0/24
[root@master ~]# salt 'node1' network.calc_net 192.168.1.10 255.255.240.0
node1:
192.168.0.0/20
[root@master ~]# salt 'node1' network.calc_net 172.15.1.3 255.255.0.0
node1:
172.15.0.0/16
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt 'node1' network.connect www.baidu.com 80
node1:
----------
comment:
Successfully connected to www.baidu.com (182.61.200.7) on tcp port 80
result:
True
[root@master ~]# salt 'node1' network.connect 192.168.220.9 22
node1:
----------
comment:
Successfully connected to 192.168.220.9 (192.168.220.9) on tcp port 22
result:
True
2.1.4 network.default_route
查看默认路由
[root@master ~]# salt 'node1' network.default_route
node1:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.220.2
interface:
ens33
netmask:
0.0.0.0
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt 'node1' network.get_fqdn
node1:
node1
[root@master ~]# salt 'master' network.get_fqdn
master:
master
2.1.6 network.get_hostname
获取主机名
[root@master ~]# salt 'master' network.get_hostname
master:
master
[root@master ~]# salt 'node1' network.get_hostname
node1:
node1
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt 'node1' network.get_route 192.168.220.9
node1:
----------
destination:
192.168.220.9
gateway:
None
interface:
ens33
source:
192.168.220.10
[root@master ~]# salt 'node1' network.get_route 192.168.220.10
node1:
----------
destination:
192.168.220.10
gateway:
None
interface:
lo
source:
192.168.220.10
[root@master ~]# salt 'node1' network.get_route 192.168.162.109
node1:
----------
destination:
192.168.162.109
gateway:
192.168.220.2
interface:
ens33
source:
192.168.220.10
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt 'node1' network.hw_addr ens33
node1:
00:0c:29:05:72:c7
[root@master ~]# salt 'node1' network.hw_addr lo
node1:
00:00:00:00:00:00
[root@master ~]# salt 'master' network.hw_addr lo
master:
00:00:00:00:00:00
[root@master ~]# salt 'master' network.hw_addr ens33
master:
00:0c:29:34:91:07
2.1.9 network.ifacestartswith
从特定的网络中检索接口名称
[root@master ~]# salt 'node1' network.ifacestartswith 192
node1:
- ens33
[root@master ~]# salt 'node1' network.ifacestartswith 192.168
node1:
- ens33
[root@master ~]# salt 'node1' network.ifacestartswith 127
node1:
- lo
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt 'node1' network.in_subnet 192.168.220.0/24
node1:
True
[root@master ~]# salt 'node1' network.in_subnet 172.16.0.0/16
node1:
False
2.1.11 network.interface
返回指定网卡的信息
[root@master ~]# salt 'node1' network.interface ens33
node1:
|_
----------
address:
192.168.220.10
broadcast:
192.168.220.255
label:
ens33
netmask:
255.255.255.0
[root@master ~]# salt 'node1' network.interface lo
node1:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt 'node1' network.interface_ip ens33
node1:
192.168.220.10
[root@master ~]# salt 'node1' network.interface_ip lo
node1:
127.0.0.1
[root@master ~]# salt 'master' network.interface_ip lo
master:
127.0.0.1
[root@master ~]# salt 'master' network.interface_ip ens33
master:
192.168.220.9
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt '*' network.interfaces
node1:
----------
ens33:
----------
hwaddr:
00:0c:29:05:72:c7
inet:
|_
----------
address:
192.168.220.10
broadcast:
192.168.220.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::2166:5bdf:402b:32c9
prefixlen:
64
scope:
link
|_
----------
address:
fe80::cc61:eeb0:86a:e547
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
master:
----------
ens33:
----------
hwaddr:
00:0c:29:34:91:07
inet:
|_
----------
address:
192.168.220.9
broadcast:
192.168.220.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::2166:5bdf:402b:32c9
prefixlen:
64
scope:
link
up:
True
lo:
----------
hwaddr:
00:00:00:00:00:00
inet:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
----------
address:
::1
prefixlen:
128
scope:
host
up:
True
2.1.14 network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1
的地址
[root@master ~]# salt '*' network.ip_addrs
node1:
- 192.168.220.10
master:
- 192.168.220.9
# 给node1 加一个临时IP再使用命令获取
[root@node1 ~]# ip addr add 192.168.220.11/24 dev ens33
[root@node1 ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:05:72:c7 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.10/24 brd 192.168.220.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.220.11/24 scope global secondary ens33
[root@master ~]# salt '*' network.ip_addrs
node1:
- 192.168.220.10
- 192.168.220.11
master:
- 192.168.220.9
2.1.15 network.netstat
返回所有打开的端口和状态
[root@master ~]# salt 'node1' network.netstat
node1:
|_
----------
inode:
26453
local-address:
127.0.0.1:323
program:
chronyd
proto:
udp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
user:
0
|_
----------
inode:
26454
local-address:
[::1]:323
program:
chronyd
proto:
udp
recv-q:
0
remote-address:
[::]:*
send-q:
0
user:
0
|_
----------
inode:
28031
local-address:
0.0.0.0:8080
program:
nginx
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
27842
local-address:
0.0.0.0:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
0
local-address:
192.168.220.10:48016
program:
proto:
tcp
recv-q:
0
remote-address:
192.168.220.9:4506
send-q:
0
state:
TIME-WAIT
user:
0
|_
----------
inode:
28366
local-address:
192.168.220.10:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.220.1:62157
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
37896
local-address:
192.168.220.10:49776
program:
salt-minion
proto:
tcp
recv-q:
0
remote-address:
192.168.220.9:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
31023
local-address:
192.168.220.10:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.220.1:62158
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
27977
local-address:
*:80
program:
httpd
proto:
tcp
recv-q:
0
remote-address:
*:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
27844
local-address:
[::]:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
[::]:*
send-q:
128
state:
LISTEN
user:
0
2.1.16 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=67.4 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=46.7 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=128 time=80.0 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=4 ttl=128 time=63.2 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 46.656/64.318/80.004/11.929 ms
[root@master ~]# salt 'node1' network.ping 192.168.220.9
node1:
PING 192.168.220.9 (192.168.220.9) 56(84) bytes of data.
64 bytes from 192.168.220.9: icmp_seq=1 ttl=64 time=0.334 ms
64 bytes from 192.168.220.9: icmp_seq=2 ttl=64 time=0.170 ms
64 bytes from 192.168.220.9: icmp_seq=3 ttl=64 time=0.236 ms
64 bytes from 192.168.220.9: icmp_seq=4 ttl=64 time=0.233 ms
--- 192.168.220.9 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3054ms
rtt min/avg/max/mdev = 0.170/0.243/0.334/0.059 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt 'node1' network.reverse_ip 192.168.220.9
node1:
9.220.168.192.in-addr.arpa
[root@master ~]# salt 'node1' network.reverse_ip 172.16.22.5
node1:
5.22.16.172.in-addr.arpa
2.1.18 network.convert_cidr
算出能够设置的子网掩码
[root@master ~]# salt 'node1' network.convert_cidr 192.168.220.9/24
node1:
----------
broadcast:
192.168.220.255
netmask:
255.255.255.0
network:
192.168.220.0
[root@master ~]# salt 'node1' network.convert_cidr 172.16.2.10/24
node1:
----------
broadcast:
172.16.2.255
netmask:
255.255.255.0
network:
172.16.2.0
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用(看的是,是否安装)
[root@master ~]# salt '*' service.available sshd
node1:
True
master:
True
[root@master ~]# salt '*' service.available nginx
master:
False
node1:
True
[root@master ~]# salt '*' service.available httpd
master:
True
node1:
True
2.2.2 service.get_all
获取所有正在运行的服务
[root@master ~]# salt 'node1' service.get_all
node1:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- boot-complete.target
- chrony-dnssrv@
- chrony-dnssrv@.timer
- chrony-wait
- chronyd
- dbus-org.freedesktop.timedate1
- dbus.socket
- debug-shell
- default.target
- initrd.target
- network.target
- nftables
- nginx
- nis-domainname
- nss-lookup.target
- nss-user-lookup.target
- paths.target
- plymouth-halt
- plymouth-kexec
- plymouth-poweroff
- plymouth-quit
- poweroff.target
.....................................
- remote-fs.target
- rescue
- rescue.target
- rhsm
- rhsm-facts
- rhsmcertd
- rngd
- rngd-wake-threshold
- rpcbind.target
- rsyslog
- run-vmblock\x2dfuse.mount
- runlevel0.target
- runlevel1.target
- runlevel2.target
- runlevel3.target
- shutdown.target
- sigpwr.target
- sleep.target
- slices.target
- smartcard.target
- sssd-pam
- sssd-pam-priv.socket
- sssd-pam.socket
- sssd-ssh
- sssd-ssh.socket
- systemd-update-utmp-runlevel
- systemd-user-sessions
- systemd-vconsole-setup
- user@
- vgauthd
- vmtoolsd
2.2.3 service.disabled
检查指定服务是否为开机不自启的状态
[root@master ~]# salt 'node1' service.disabled nginx
node1:
True
[root@master ~]# salt 'master' service.disabled httpd
master:
True
[root@master ~]# systemctl status httpd # 查看master上的httpd状态是否为开机不自启
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled(是开机不自启的状态); vendor>
Active: active (running) since Tue 2021-11-02 20:53:50 EDT; 6h ago
Docs: man:httpd.service(8)
Main PID: 896 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11201)
Memory: 45.1M
CGroup: /system.slice/httpd.service
├─896 /usr/sbin/httpd -DFOREGROUND
├─939 /usr/sbin/httpd -DFOREGROUND
2.2.4 service.enabled
检查指定服务是否为开机自启动
[root@master ~]# salt 'node1' service.enabled nginx
node1:
False
[root@master ~]# salt 'master' service.enabled httpd
master:
False
[root@master ~]# salt 'node1' service.enabled sshd
node1:
True
# 查看node1 上的sshd是否为开启自启的状态
[root@node1 ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled(是开机自启的状态); vendor p>
Active: active (running) since Wed 2021-11-03 01:01:55 EDT; 2h 3min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 911 (sshd)
Tasks: 1 (limit: 11201)
Memory: 8.0M
CGroup: /system.slice/sshd.service
└─911 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha
2.2.5 service.disable
设置指定服务开机不自启动
[root@master ~]# salt '*' service.disable httpd
master:
True
node1:
True
# 查看master上的httpd设置成功为开机不自启动(disabled)
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled(成功设置); vendor >
Active: active (running) since Tue 2021-11-02 20:53:50 EDT; 6h ago
Docs: man:httpd.service(8)
Main PID: 896 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11201)
Memory: 45.1M
CGroup: /system.slice/httpd.service
├─896 /usr/sbin/httpd -DFOREGROUND
├─939 /usr/sbin/httpd -DFOREGROUND
2.2.6 service enable
设置指定服务开机自动启动
[root@master ~]# salt '*' service.enable httpd
node1:
True
master:
True
# 查看master上的httpd是否设置成功为enabled(开机自启状态)
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled(成功设置); vendor >
Active: active (running) since Tue 2021-11-02 20:53:50 EDT; 6h ago
Docs: man:httpd.service(8)
Main PID: 896 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11201)
Memory: 45.1M
CGroup: /system.slice/httpd.service
├─896 /usr/sbin/httpd -DFOREGROUND
├─939 /usr/sbin/httpd -DFOREGROUND
2.2.7 service.reload
重新加载指定服务(服务必须是启动的才能重新加载)
[root@master ~]# salt '*' service.reload httpd
node1:
True
master:
True
# 关闭master上的httpd服务再执行重新加载命令
[root@master ~]# systemctl stop httpd
[root@master ~]# salt '*' service.reload httpd
master:
ERROR: httpd.service is not active, cannot reload. # 错误:httpd。服务未激活,无法重新加载。
node1:
True
ERROR: Minions returned with non-zero exit code
2.2.8 service.stop
停止指定服务
[root@master ~]# salt 'master' service.stop httpd
master:
True
# 查看master上的httpd是否停止
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor>
Active: inactive (dead)(死的)
Docs: man:httpd.service(8)
Nov 02 20:53:50 master systemd[1]: Started The Apache HTTP Server.
Nov 02 20:53:51 master httpd[896]: Server configured, listening on: port 80
Nov 03 03:15:54 master systemd[1]: Reloading The Apache HTTP Server.
Nov 03 03:15:55 master httpd[891467]: AH00558: httpd: Could not reliably d>
Nov 03 03:15:55 master systemd[1]: Reloaded The Apache HTTP Server.
Nov 03 03:15:55 master httpd[896]: Server configured, listening on: port 80
2.2.9 service.start
启动指定服务
[root@master ~]# salt 'master' service.start httpd
master:
True
# 查看master上的httpd是否启动
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor>
Active: active (running)(运行) since Wed 2021-11-03 03:22:02 EDT; 13s ago
Docs: man:httpd.service(8)
Main PID: 915048 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 11201)
Memory: 27.5M
CGroup: /system.slice/httpd.service
├─915048 /usr/sbin/httpd -DFOREGROUND
├─915058 /usr/sbin/httpd -DFOREGROUND
2.2.10 service.restart
重启指定服务
[root@master ~]# salt '*' service.restart httpd
node1:
True
master:
True
2.2.11 service.status
查看指定服务的状态
[root@master ~]# salt '*' service.status httpd
master:
True
node1:
True
# 停掉master上的httpd,再查看
[root@master ~]# systemctl stop httpd
[root@master ~]# salt '*' service.status httpd
node1:
True
master:
False
2.2.12 service.missing
服务的反面。如果命名的服务不可用,则返回真实。
[root@master ~]# salt '*' service.missing nginx
node1:
False
master:
True
# node1上有nginx,返回了False,master上没有nginx,返回了True
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包(不会下载依赖包),但是需要在minion端安装yum-utils
,可以使用 cmd.run 进行远程安装
# 查看node1上是否安装yum-utila
[root@node1 ~]# rpm -qa |grep yum-utils
yum-utils-4.0.21-3.el8.noarch
[root@master ~]# salt '*' pkg.download wget
master:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
node1:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
# 查看node1上下载的wget
[root@node1 ~]# ls /var/cache/yum/packages/
wget-1.19.5-10.el8.x86_64.rpm
2.3.2 pkg.file_list
列出指定包或系统中已安装的所有包的文件(与rpm -ql 一样)
# 列出已安装的apache软件包提供的所有文件
[root@master ~]# salt 'node1' pkg.file_list nginx
node1:
----------
errors:
files:
- /etc/logrotate.d/nginx
- /etc/nginx/fastcgi.conf
- /etc/nginx/fastcgi.conf.default
- /etc/nginx/fastcgi_params
- /etc/nginx/fastcgi_params.default
- /etc/nginx/koi-utf
.....................................
2.3.3 pkg.group_info
查看包组的信息
[root@master ~]# salt 'node1' pkg.group_info 'Development Tools'
node1:
----------
conditional:
default:
- asciidoc
- byacc
- ctags
- diffstat
- elfutils-libelf-devel
- git
- intltool
- jna
- ltrace
- patchutils
- perl-Fedora-VSP
- perl-Sys-Syslog
- 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
2.3.4 pkg.group_list
列出系统中所有的包组
[root@master ~]# salt 'node1' pkg.group_list
node1:
----------
available:
- Backup Client
- base-x
- Conflicts AppStream
- Container Management
- Debugging Tools
- Desktop Debugging and Performance Tools
- .NET Core Development
- FTP Server
- GNOME Applications
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Input Methods
- Internet Applications
- Internet Browser
- Java Platform
- Legacy X Window System Compatibility
- Multimedia
- Office Suite and Productivity
- Atomic Host ostree support
- KVM platform specific packages
- Hyper-v platform specific packages
- Printing Client
- Remote Desktop Clients
- RPM Development Tools
- TeX formatting system
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Basic Web Server
- Additional Development
- Anaconda tools
- Base
- Conflicts BaseOS
- Development Tools
- Dial-up Networking Support
- File and Storage Server
- Fonts
- GNOME
- Graphical Administration Tools
- Hardware Monitoring Utilities
- Hardware Support
- Headless Management
- Infiniband Support
- Large Systems Performance
- Legacy UNIX Compatibility
- Mail Server
- Mainframe Access
- Network File System Client
- Network Servers
- Networking Tools
- Common NetworkManager submodules
- Performance Tools
- Platform Development
- Python Web
- Remote Management for Linux
- Scientific Support
- Security Tools
- Server product core
- Smart Card Support
- Windows File Server
- Standard
- System Tools
- Workstation product core
available environments:
- Server with GUI
- Server
- Workstation
- Virtualization Host
- Custom Operating System
available languages:
----------
installed:
- VMware platform specific packages
- Core
installed environments:
- Minimal Install
2.3.5 pkg.install
安装软件
[root@master ~]# salt 'node1' pkg.install make
node1:
----------
make:
----------
new:
1:4.2.1-11.el8
old:
2.3.6 pkg.list_downladed
列出已下载到本地(下载到当前主机,不是受控主机)的软件包
[root@master ~]# salt '*' pkg.list_downloaded
node1:
----------
master:
----------
# 没有packages的目录
[root@master ~]# ls /var/cache/dnf/
appstream-773ef6463612e8e2 extras-filenames.solvx
appstream-filenames.solvx extras.solv
appstream.solv last_makecache
baseos-055ffcb2ec25a27f packages.db
baseos-filenames.solvx salt-latest-repo-c9e9d9520ddd8763
baseos.solv salt-latest-repo-filenames.solvx
commandline-a76fe31ae310b0c7 salt-latest-repo.solv
expired_repos.json tempfiles.json
extras-9705a089504ff150
2.3.7 pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt '*' pkg.list_pkgs
node1:
----------
NetworkManager:
1:1.30.0-0.3.el8
NetworkManager-libnm:
1:1.30.0-0.3.el8
NetworkManager-team:
1:1.30.0-0.3.el8
NetworkManager-tui:
1:1.30.0-0.3.el8
abattis-cantarell-fonts:
0.0.25-4.el8
................
2.3.8 pkg.owner
列出指定文件是由哪个包提供的(必须已经存在的文件)
[root@master ~]# salt '*' pkg.owner /etc/passwd
node1:
setup
master:
setup
[root@master ~]# salt '*' pkg.owner /usr/bin/vim
node1:
vim-enhanced
master:
vim-enhanced
2.3.9 pkg.remove
卸载指定软件
[root@master ~]# salt 'node1' cmd.run 'rpm -qa |grep make' # 查看node1上的make已安装
node1:
make-4.2.1-11.el8.x86_64
[root@master ~]# salt 'node1' pkg.remove make # 卸载make
node1:
----------
make:
----------
new:
old:
1:4.2.1-11.el8
[root@master ~]# salt 'node1' cmd.run 'rpm -qa |grep make' # # 查看node1上的make已被卸载
node1:
ERROR: Minions returned with non-zero exit code
# 若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
# 把这个wget下载到真机再上传到node1 http://mirror.centos.org/centos/7/os/x86_64/Packages/wget-1.14-18.el7_6.1.x86_64.rpm
# 再在node1上安装wget包
[root@node1 ~]# yum -y install wget-1.14-18.el7_6.1.x86_64.rpm
[root@node1 ~]# rpm -qa |grep wget
wget-1.14-18.el7_6.1.x86_64
# 在master上对node1上的wget更新
[root@master ~]# salt 'node1' pkg.upgrade wget
node1:
----------
wget:
----------
new:
1.19.5-10.el8
old:
1.14-18.el7_6.1
[root@master ~]# salt 'node1' cmd.run 'rpm -qa |grep wget'
node1:
wget-1.19.5-10.el8.x86_64
2.3.11 pkg.version
查看版本
[root@master ]# salt '*' pkg.version httpd
node1:
2.4.37-40.module_el8.5.0+852+0aafc63b
master:
2.4.37-40.module_el8.5.0+852+0aafc63b
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@master base]# pwd
/srv/salt/base
[root@master base]# cat web/apache/install.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
# 查看
[root@master ~]# salt '*' state.show_highstate
node1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
apache
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.install
service:
|_
----------
name:
apache
|_
----------
enable:
True
- running
|_
----------
order:
10001
master:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.install
pkg:
|_
----------
name:
apache
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.install
service:
|_
----------
name:
apache
|_
----------
enable:
True
- running
|_
----------
order:
10001
2.4.2 state.highstate
执行高级状态
[root@master base]# salt '*' state.highstate web.apache.install
node1:
----------
ID: apache-install
Function: pkg.installed
Name: apache
Result: None
Comment: The following packages would be installed/updated: apache
Started: 04:49:24.807275
Duration: 773.049 ms
Changes:
----------
installed:
----------
apache:
----------
new:
installed
old:
----------
ID: apache-service
Function: service.running
Name: apache
Result: None
Comment: Service apache not present; if created in this state run, it would have been started
Started: 04:49:25.590649
Duration: 18.878 ms
Changes:
Summary for node1
------------
Succeeded: 2 (unchanged=2, changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 791.927 ms
master:
----------
ID: apache-install
Function: pkg.installed
Name: apache
Result: None
Comment: The following packages would be installed/updated: apache
Started: 04:49:24.896324
Duration: 739.798 ms
Changes:
----------
installed:
----------
apache:
----------
new:
installed
old:
----------
ID: apache-service
Function: service.running
Name: apache
Result: None
Comment: Service apache not present; if created in this state run, it would have been started
Started: 04:49:25.647616
Duration: 30.177 ms
Changes:
Summary for master
------------
Succeeded: 2 (unchanged=2, changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 769.975 ms
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master base]# salt '*' state.show_state_usage
master:
----------
base:
----------
count_all: # 执行状态总数
3
count_unused: # 未被使用的个数
2
count_used: # 使用的个数
1
unused: # 未被使用的是
- top
- web.nginx.install
used: # 使用的是
- web.apache.install
node1:
----------
base:
----------
count_all:
3
count_unused:
2
count_used:
1
unused:
- top
- web.nginx.install
used:
- web.apache.install
2.4.4 state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt '*' state.show_top
node1:
----------
base:
- web.apache.install
master:
----------
base:
- web.apache.install
2.4.5 state.top
执行指定的top file,而不是默认的,state.highstat 执行的是默认的(top.sls)
[root@master ~]# tree /srv/salt/base/
/srv/salt/base/
|-- runinstall.sls(top file文件)
`-- web
|-- apache
| `-- install.sls
`-- nginx
`-- install.sls
# top file文件内容
[root@master ~]# cat /srv/salt/base/runinstall.sls
base:
'node1':
- web.apache.install
# 状态文件
[root@master ~]# cat /srv/salt/base/web/apache/install.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
# 运行指定的top file文件
[root@master ~]# salt 'node1' state.top runinstall.sls
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 04:58:46.494489
Duration: 596.382 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is in the desired state
Started: 04:58:47.092281
Duration: 170.791 ms
Changes:
----------
httpd:
True
Summary for node1
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 767.173 ms
2.4.6 state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt '*' 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
master:
----------
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
2.5 SaltStack常用模块之user
语法:
CLI Example:
salt '*' user.add name <uid> <gid> <groups> <home> <shell>
2.5.1 user.add
创建用户
# 查看node1是否有tom,harry用户
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
id: 'tom': no such user
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt 'node1' cmd.run 'id harry'
node1:
id: 'harry': no such user
ERROR: Minions returned with non-zero exit code
# 创建tom,harry用户在node1上
[root@master ~]# salt 'node1' user.add tom
node1:
True
[root@master ~]# salt 'node1' user.add harry
node1:
True
# 查看
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
uid=1000(tom) gid=1000(tom) groups=1000(tom)
[root@master ~]# salt 'node1' cmd.run 'id harry'
node1:
uid=1112(harry) gid=1112(harry) groups=1112(harry)
2.5.2 user.chfullname
更改用户完整名称(用户信息进行描述)
[root@master ~]# salt 'node1' cmd.run 'cat /etc/passwd | tail -1'
node1:
tom:x:1000:1000::/home/tom:/bin/bash
# 更改查看
[root@master ~]# salt 'node1' user.chfullname tom "tom tur"
node1:
True
[root@master ~]# salt 'node1' cmd.run 'cat /etc/passwd | tail -1'
node1:
tom:x:1000:1000:tom tur:/home/tom:/bin/bash
2.5.3 user.chgid
修改用户gid,修改组gid之前得先有这个组
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
uid=1000(tom) gid=1000(tom) groups=1000(tom)
# 修改查看
[root@master ~]# salt 'node1' user.chgid 'tom' 999
node1:
True
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
uid=1000(tom) gid=999(input) groups=999(input)
2.5.4 user.chuid
修改用户uid
# 查看
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
uid=1000(tom) gid=999(input) groups=999(input)
# 修改查看
[root@master ~]# salt 'node1' user.chuid tom 1111
node1:
True
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
uid=1111(tom) gid=999(input) groups=999(input)
2.5.5 user.chgroups
添加附属组
# 查看
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
uid=1111(tom) gid=999(input) groups=999(input)
# 修改查看
[root@master ~]# salt 'node1' user.chgroups tom root,harry True
node1:
True
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
uid=1111(tom) gid=999(input) groups=999(input),1112(harry),0(root)
2.5.6 user.info
返回用户详细信息
[root@master ~]# salt 'node1' user.info tom
node1:
----------
fullname:
tom tur
gid:
999
groups:
- harry
- input
- root
home:
/home/tom
homephone:
name:
tom
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1111
workphone:
2.5.7 user.getent
返回所有用户信息
[root@master ~]# salt 'node1' user.getent
node1:
|_
----------
fullname:
root
gid:
0
groups:
- root
home:
/root
homephone:
name:
root
other:
..............
2.5.8 user.rename
修改用户名
# 查看
[root@master ~]# salt 'node1' cmd.run 'id harry'
node1:
uid=1112(harry) gid=1112(harry) groups=1112(harry)
# 修改用户名查看
[root@master ~]# salt 'node1' user.rename harry alice
node1:
False
[root@master ~]# salt 'node1' cmd.run 'id alice'
node1:
uid=1112(alice) gid=1112(harry) groups=1112(harry)
2.5.9 user.list_users
返回所有用户的列表
[root@master ~]# salt 'node1' user.list_users
node1:
- adm
- alice
- apache
- bin
- chrony
- daemon
- dbus
- ftp
- games
- halt
- lp
- mail
- nginx
- nobody
- operator
- polkitd
- rngd
- root
- shutdown
- sshd
- sssd
- sync
- systemd-coredump
- systemd-resolve
- tom
- tss
- unbound
2.5.10 user.list_groups
返回指定用户所属的组列表
[root@master ~]# salt 'node1' user.list_groups tom
node1:
- harry
- input
- root
2.5.11 user.chhome
修改用户的家目录
# 查看
[root@master ~]# salt 'node1' cmd.run 'cat /etc/passwd |grep tom'
node1:
tom:x:1111:999:tom tur:/home/tom(家目录位置):/bin/bash
# 新建一个家目录
[root@master ~]# salt 'node1' cmd.run 'mkdir /home/users'
node1:
[root@master ~]# salt 'node1' cmd.run 'ls /home'
node1:
harry
tom
users
# 修改并查看
[root@master ~]# salt 'node1' user.chhome tom /home/users True
node1:
True
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt 'node1' cmd.run 'cat /etc/passwd |grep tom'
node1:
tom:x:1111:999:tom tur:/home/users(新家目录位置):/bin/bash
2.5.12 passwd
为用户设置密码
[root@master ~]# salt 'node1' cmd.run 'id alice'
node1:
uid=1112(alice) gid=1112(harry) groups=1112(harry)
[root@master ~]# salt 'node1' cmd.run 'echo "redhat" |passwd --stdin alice'
node1:
Changing password for user alice.
passwd: all authentication tokens updated successfully.
2.5.13 user.delete
删除用户
# 删除前查看
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
uid=1111(tom) gid=999(input) groups=999(input),1112(harry),0(root)
# 删除
[root@master ~]# salt 'node1' user.delete tom
node1:
True
# 删除后查看
[root@master ~]# salt 'node1' cmd.run 'id tom'
node1:
id: 'tom': no such user
ERROR: Minions returned with non-zero exit code
2.6 SaltStack之salt-cp
salt-cp
能够很方便的把 master 上的文件批量传到 minion上
# 拷贝单个文件到目标主机指定目录下
[root@master ~]# cat /root/test.sh
#!/bin/bish
echo "hello world"
# 查看node1上/opt目录下是否有test.sh文件
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 0
# 将master上的/root/test.sh拷贝到node1上的/opt目录下
[root@master ~]# salt-cp 'node1' /root/test.sh /opt/
node1:
----------
/opt/test.sh:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 4
-rw-r--r-- 1 root root 32 Nov 4 00:51 test.sh
# 执行node1上刚拷贝的脚本文件
[root@master ~]# salt 'node1' cmd.run 'bash /opt/test.sh'
node1:
hello world
# 拷贝多个文件到目标主机的/opt/目录下
[root@master ~]# salt-cp 'node1' /root/anaconda-ks.cfg /etc/passwd /opt/
node1:
----------
/opt/anaconda-ks.cfg:
True
/opt/passwd:
True
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 12
-rw-r--r-- 1 root root 1023 Nov 4 00:54 anaconda-ks.cfg
-rw-r--r-- 1 root root 1218 Nov 4 00:54 passwd
-rw-r--r-- 1 root root 32 Nov 4 00:51 test.sh
2.7 SaltStack常用模块之file
2.7.1 file.access
检查指定路径(文件)是否存在,无法检查目录
# 检查node1上的/opt/目录下的指定文件是否存在,存在返回True,不存在返回False
[root@master ~]# salt 'node1' file.access /opt/test.sh f
node1:
True
[root@master ~]# salt 'node1' file.access /opt/123 f
node1:
False
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
-rw-r--r-- 1 root root 32 Nov 4 00:51 /opt/test.sh
检查指定文件的权限信息(不管在哪个位置上,只要有相应的rwx权限就返回True)
[root@master ~]# salt 'node1' file.access /opt/test.sh r # 是否有读权限
node1:
True
[root@master ~]# salt 'node1' file.access /opt/test.sh w # 是否有写权限
node1:
True
[root@master ~]# salt 'node1' file.access /opt/test.sh x # 是否有执行权限
node1:
False
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
-rw-r--r-- 1 root root 32 Nov 4 00:51 /opt/test.sh
2.7.2 file.append
往文件里追加内容,若文件不存在则报异常
# 查看
[root@master ~]# salt 'node1' cmd.run 'cat /opt/test.sh'
node1:
#!/bin/bish
echo "hello world"
# 追加查看(多个引号)
[root@master ~]# salt 'node1' file.append /opt/test.sh "xixi" "hehe"
node1:
Wrote 2 lines to "/opt/test.sh"
[root@master ~]# salt 'node1' cmd.run 'cat /opt/test.sh'node1:
#!/bin/bish
echo "hello world"
xixi # 追加为多行
hehe
# 追加查看(一个引号)
[root@master ~]# salt 'node1' file.append /opt/test.sh "jjyy lo"
node1:
Wrote 1 lines to "/opt/test.sh"
[root@master ~]# salt 'node1' cmd.run 'cat /opt/test.sh'
node1:
#!/bin/bish
echo "hello world"
xixi
hehe
jjyy lo # 追加为一行
# 追加查看(无引号)
[root@master ~]# salt 'node1' file.append /opt/test.sh look o oooopqqp
node1:
Wrote 3 lines to "/opt/test.sh"
[root@master ~]# salt 'node1' cmd.run 'cat /opt/test.sh'node1:
#!/bin/bish
echo "hello world"
xixi
hehe
jjyy lo
look # 空格处为一行
o
oooopqqp
2.7.3 file.basename
获取指定路径的基名
[root@master ~]# salt 'node1' file.basename /etc/httpd/conf/httpd.conf
node1:
httpd.conf # 取最后每一个
2.7.4 file.dirname
获取指定路径的目录名
[root@master ~]# salt 'node1' file.dirname /etc/httpd/conf/httpd.conf
node1:
/etc/httpd/conf # 取最后一个前面的所有
2.7.5 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt 'node1' cmd.run 'md5sum /opt/test.sh'node1:
3126f013f12a40de8ff032a739eff170 /opt/test.sh
# 把/opt/test.sh的md5值与文件对比,匹配则返回 True 否则返回 False
[root@master ~]# salt 'node1' file.check_hash /opt/test.sh 3126f013f12a40de8ff032a739eff170
node1:
True
2.7.6 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件, 而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改, 不能删除、更改、移动 | 任何的进程只能修改目录之下的文件, 不允许建立和删除文件 |
给指定文件添加属性
# 查看文件本来的属性
[root@master ~]# salt 'node1' cmd.run "lsattr -d /opt/test.sh"
node1:
-------------------- /opt/test.sh
# 给指定文件添加属性
[root@master ~]# salt 'node1' file.chattr /opt/test.sh operator(操作)=add attributes(属性)=ai
node1:
True
[root@master ~]# salt 'node1' cmd.run "lsattr -d /opt/test.sh"node1:
----ia-------------- /opt/test.sh
# 尝试覆盖/opt/test.sh文件的内容
[root@master ~]# salt 'node1' cmd.run 'echo "123" > /opt/test.sh'
node1:
/bin/sh: /opt/test.sh: Operation not permitted(操作不允许)
ERROR: Minions returned with non-zero exit code
# 尝试删除/opt/test.sh文件
[root@master ~]# salt 'node1' cmd.run 'rm -f /opt/test.sh'
node1:
rm: cannot remove '/opt/test.sh': Operation not permitted(操作不允许)
ERROR: Minions returned with non-zero exit code
给目录添加属性
# 查看目录本来的属性
[root@master ~]# salt 'node1' cmd.run "lsattr -d /opt/"
node1:
-------------------- /opt/
# 添加属性
[root@master ~]# salt 'node1' file.chattr /opt/ operator(操作)=add attributes(属性)=ai
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr -d /opt/'node1:
----ia-------------- /opt/
# 尝试删除目录/opt/
[root@master ~]# salt 'node1' cmd.run 'rm -r /opt/'
node1:
rm: cannot remove '/opt/anaconda-ks.cfg': Operation not permitted
rm: cannot remove '/opt/passwd': Operation not permitted
rm: cannot remove '/opt/test.sh': Operation not permitted
ERROR: Minions returned with non-zero exit code
给指定文件去除属性
[root@master ~]# salt 'node1' cmd.run 'lsattr /opt/test.sh'
node1:
----ia-------------- /opt/test.sh
[root@master ~]# salt 'node1' file.chattr /opt/test.sh operator=remove(移除) attributes=ai
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr /opt/test.sh'node1:
-------------------- /opt/test.sh
给指定目录去除属性
[root@master ~]# salt 'node1' file.chattr /opt/ operator=remove(移除) attributes=ai
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr -d /opt/'node1:
-------------------- /opt/
在当前目录下操作
[root@master ~]# lsattr anaconda-ks.cfg
-----a-------------- anaconda-ks.cfg
[root@master ~]# chattr +i anaconda-ks.cfg
[root@master ~]# lsattr anaconda-ks.cfg
----ia-------------- anaconda-ks.cfg
[root@master ~]# chattr -i anaconda-ks.cfg
[root@master ~]# lsattr anaconda-ks.cfg
-----a-------------- anaconda-ks.cfg
2.7.7 file.chown
设置指定文件的属主属组,必须两个都指定
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
-rw-r--r-- 1 root root 66 Nov 4 01:15 /opt/test.sh
# 设置查看
[root@master ~]# salt 'node1' file.chown /opt/test.sh alice(用户) harry(组)
node1:
None
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
-rw-r--r-- 1 alice(用户) harry(组) 66 Nov 4 01:15 /opt/test.sh
# 还原
[root@master ~]# salt 'node1' file.chown /opt/test.sh root root
node1:
None
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/test.sh'
node1:
-rw-r--r-- 1 root root 66 Nov 4 01:15 /opt/test.sh
2.7.8 file.copy
在远程主机上直接复制文件在远程主机上
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 4
-rw-------. 1 root root 1023 Sep 24 04:52 anaconda-ks.cfg
# 将/opt/test.sh 文件复制到/root目录下,也叫test.sh
[root@master ~]# salt 'node1' file.copy /opt/test.sh /root/test.sh
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Sep 24 04:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 66 Nov 4 01:55 test.sh
在远程主机上覆盖并拷贝目录,将会覆盖同名文件或目录
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Sep 24 04:52 anaconda-ks.cfg
-rw-r--r-- 1 root root 66 Nov 4 01:55 test.sh
# 将/usr/src目录复制到/root目录下叫123
[root@master ~]# salt 'node1' file.copy /usr/src/ /root/123 recurse=True
node1:
True
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 8
drwxr-xr-x 4 root root 34 Nov 4 02:09 123
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 66 Nov 4 01:58 test.sh
# 查看/root/123的内容
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/123'
node1:
total 0
drwxr-xr-x 2 root root 6 Nov 4 02:09 debug
drwxr-xr-x 2 root root 6 Nov 4 02:09 kernels
# 将/etc/pam.d目录复制到/root下叫123,查看/root/123发现之前的123目录的内容已被覆盖
[root@master ~]# salt 'node1' file.copy /etc/pam.d/ /root/123 recurse=True
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/123'
node1:
total 96
-rw-r--r-- 1 root root 232 Nov 4 02:12 config-util
-rw-r--r-- 1 root root 328 Nov 4 02:12 crond
drwxr-xr-x 2 root root 6 Nov 4 02:09 debug
-rw-r--r-- 1 root root 701 Nov 4 02:12 fingerprint-auth
...............................
删除目标目录中同名的文件或目录并拷贝新内容至其中
# 查看/root/123目录中的内容
[root@master ~]# salt 'node1' cmd.run 'ls /root/123'
node1:
gfgdfgdf
gg[gfg
gjkfgd
xixi
# 查看/opt/123目录中的内容
[root@master ~]# salt 'node1' cmd.run 'ls /opt/123'
node1:
gg[gfg
gjkfgd
# 将/opt/123目录拷贝到/root/123目录中并查看
[root@master ~]# salt 'node1' file.copy /opt/123 /root/123 recurse=True remove_existing=True
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls /root/123'node1:
gg[gfg
gjkfgd
2.7.9 file.directory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt 'node1' file.directory_exists /root/123
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 8
drwxr-xr-x 3 root root 34 Nov 4 02:19 123
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 66 Nov 4 01:58 test.sh
2.7.10 file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt 'node1' file.diskusage /root/
node1:
16072
[root@master ~]# salt 'node1' cmd.run 'du -sb /root'
node1:
16330 /root
[root@master ~]# salt 'node1' cmd.run 'du -sh /root'
node1:
48K /root
2.7.11 file.file_exists
判断指定文件是否存在
# 判断/opt/test.sh文件是否存在
[root@master ~]# salt 'node1' file.file_exists /opt/test.sh
node1:
False
# 查看确实不存在
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 3 root root 34 Nov 4 02:19 123
-rw-r--r-- 1 root root 1023 Nov 4 00:54 anaconda-ks.cfg
2.7.12 file.find
类似 find 命令并返回符合指定条件的路径列表
选项包括匹配条件:
name = path-glob # 区分大小写
iname = path-glob # 不区分大小写
regex = path-regex # 区分大小写
iregex = path-regex # 不区分大小写
type = file-types # 匹配任何列出的类型
user = users # 匹配任何列出的用户
group = groups # 匹配任何列出的组
size = [+-]number[size-unit] # 默认单位=字节
mtime = interval # 从日期开始修改
grep = regex # 搜索文件内容
and/oractions :
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
# 最大深度=横向路径的最大深度
# 检查文件或目录前的最小横向深度
默认的操作是print=path
path-glob:
* = 匹配零个或多个字符
? = 匹配任意字符
[abc] = 匹配a, b或c
[!abc] or [^abc] = 匹配除a, b, c之外的任何东西
[x-y] = 匹配字符x到y
[!x-y] or [^x-y] = 匹配除了字符x到y以外的任何字符
{a,b,c} = 匹配 a or b or c
path-regex
: 用于匹配路径名的Python正则表达式模式
file-types
:由下列一个或多个字符组成的字符串:
a: 所有文件类型
b: 块设备
c: 字符设备
d: 目录
p: FIFO(命名管道)
f: 普通文件
l: 符号链接
s: 套接字
users
: 由空格和/或逗号分隔的用户名和/或uid列表
groups
: 由空格和/或逗号分隔的组名和/或gids列表
size-unit
:
b: bytes 字节
k: kilobytes kb
m: megabytes mb
g: gigabytes gb
t: terabytes tb
interval:
[<num>w] [<num>d] [<num>h] [<num>m] [<num>s]
where:
w: week 周
d: day 天
h: hour 小时
m: minute 分钟
s: second 秒
Print-opts:由逗号和/或空格分隔的下列一个或多个列表:
group: :组名
md5: 文件内容的md5摘要
mode: 文件权限(以整数形式)
mtime: 最后一次修改时间(as time_t)
name: file basename
path: 文件的绝对路径
size: 以字节为单位的文件大小
type: 文件类型
user: 用户名
示例:
[root@master ~]# salt 'node1' file.find / type=f name=\*.bak size=+10m
[root@master ~]# salt 'node1' file.find /var mtime=+30d(30天以前文件被修改的时间) size=+10m print=path,size,mtime
[root@master ~]# salt 'node1' file.find /var/log name=\*.[0-9] mtime=+30d(30天以前文件被修改的时间) size=+10m delete
# 查找根下面的文件以.bak结尾的
[root@master ~]# salt 'node1' file.find / type=f name=\*.bak
node1:
- /etc/nsswitch.conf.bak
# 打印目录/var下的大于10M的用户,大小,修改时间
[root@master ~]# salt 'node1' file.find /var size=+10m print=user,size,mtime
node1:
|_
- root
- 12904035
- 1635745024
|_
- root
- 13311037
- 1635745021
|_
- root
- 61124608
- 1635928208
|_
- sssd
- 11567160
- 1636002001
# 删除/var/log/目录下的以*.[0-9].log的文件
[root@master ~]# salt 'node1' file.find /var/log name=\*.[0-9].log delete
node1:
- /var/log/fdf.1.log
- /var/log/vmware-network.1.log
- /var/log/vmware-network.2.log
- /var/log/vmware-network.3.log
- /var/log/vmware-network.4.log
- /var/log/vmware-network.5.log
- /var/log/vmware-network.6.log
- /var/log/vmware-network.7.log
- /var/log/vmware-network.8.log
- /var/log/vmware-network.9.log
2.7.13 file.get_gid
获取指定文件的gid
[root@master ~]# salt 'node1' file.chown /root/test.sh alice harry
node1:
None
[root@master ~]# salt 'node1' cmd.run 'id alice'
node1:
uid=1112(alice) gid=1112(harry) groups=1112(harry)
# 获取
[root@master ~]# salt 'node1' file.get_gid /root/test.sh
node1:
1112
2.7.14 file.get_group
获取指定文件的组名
[root@master ~]# salt 'node1' file.get_group /root/test.sh
node1:
harry
2.7.15 file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt 'node1' file.get_hash /root/test.sh
node1:
0acf7a6315c70ed00f6c7db6afb65748471b9fb267088f5eb539037a129ea711
[root@master ~]# salt 'node1' cmd.run 'sha256sum /root/test.sh'
node1:
0acf7a6315c70ed00f6c7db6afb65748471b9fb267088f5eb539037a129ea711 /root/test.sh
2.7.16 file.get_mode
获取指定文件的权限,以数字方式显示
# 将/opt/anaconda-ks.cfg 文件的权限以数字显示
[root@master ~]# salt 'node1' file.get_mode /opt/anaconda-ks.cfg
node1:
0644
# 将/var/log 目录的权限以数字显示
[root@master ~]# salt 'node1' file.get_mode /var/log
node1:
0755
2.7.17 file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
# 获取/varlog目录的上下文
[root@master ~]# salt 'node1' file.get_selinux_context /var/log
node1:
system_u:object_r:var_log_t:s0
# 获取/root/目录的上下文
[root@master ~]# salt 'node1' file.get_selinux_context /root/
node1:
system_u:object_r:admin_home_t:s0
2.7.18 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
# 查看默认的特征码(sha256)
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf
node1:
6ffbc88668a8cd9f5576bc0e9a686b88a2c23eb89194ecceb5dfd11c81120966
# 指定查看特征码MD5
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf md5
node1:
d659bccae84073a2fa24fa8c7cbc9774
# 指定查看特征码sha512
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf sha512
node1: acc464ad461721927b36ee377067ef040bfa5b521385fb7ab87a5492f71de93c640cf614b8201fc4c09c342a2f231c7acaee686e6f64b662ea306a277f183912
# 指定查看特征码sha224
[root@master ~]# salt 'node1' file.get_sum /etc/httpd/conf/httpd.conf sha224
node1:
ac823792c431691a4dd809693afbce98e838c8b222367ab4f8e4978e
2.7.19 file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
# 获取/root/test.sh的uid
[root@master ~]# salt 'node1' file.get_uid /root/test.sh
node1:
1112
# 获取/root/test.sh的用户
[root@master ~]# salt 'node1' file.get_user /root/test.sh
node1:
alice
# 获取/root/test.sh的gid
[root@master ~]# salt 'node1' file.get_gid /root/test.sh
node1:
1112
# 获取/root/test.sh的组
[root@master ~]# salt 'node1' file.get_group /root/test.sh
node1:
harry
2.7.20 file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt 'node1' file.gid_to_group 0
node1:
root
2.7.21 file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt 'node1' file.group_to_gid input
node1:
999
2.7.22 file.user_to_uid
将指定的用户转换为uid显示
[root@master ~]# salt 'node1' file.user_to_uid alice
node1:
1112
2.7.23 file.uid_to_user
将指定的uid转换为用户显示
[root@master ~]# salt 'node1' file.uid_to_user 0
node1:
root
[root@master ~]# salt 'node1' file.uid_to_user 998
node1:
polkitd
2.7.24 file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@master ~]# salt 'node1' file.grep /etc/passwd "^root" # 在/etc/passwd文件中查找以root开头的
node1:
----------
pid:
320915
retcode:
0
stderr:
stdout:
root:x:0:0:root:/root:/bin/bash
# 查找指定文件中的ipaddr 忽略大小写
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
node1:
----------
pid:
323676
retcode:
0
stderr:
stdout:
IPADDR=192.168.220.10
# 查找指定文件中的ipaddr 忽略大小写更详细显示
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -il
node1:
----------
pid:
324061
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-ens33
# 查找指定文件中的ipaddr 忽略大小写并显示后两行
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -A2
node1:
----------
pid:
326627
retcode:
0
stderr:
stdout:
IPADDR=192.168.220.10
GATEWAY=192.168.220.2
NETMASK=255.255.255.0
# 查找指定文件中的ipaddr 忽略大小写并显示前两行
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
node1:
----------
pid:
327128
retcode:
0
stderr:
stdout:
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.220.10
# 查找指定文件中的ipaddr 忽略大小写并显示前后两行
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -C2
node1:
----------
pid:
327683
retcode:
0
stderr:
stdout:
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.220.10
GATEWAY=192.168.220.2
NETMASK=255.255.255.0
2.7.25 file.is_blkdev
判断指定的文件是否是块设备文件
# 查看/dev/sr0是否为块设备文件
[root@master ~]# salt 'node1' file.is_blkdev /dev/sr0
node1:
True
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /dev/sr0'
node1:
brw-rw---- 1 root cdrom 11, 0 Nov 3 21:27 /dev/sr0
2.7.26 file.lsattr
检查并显示出指定文件的属性信息
# 查看属性
[root@master ~]# salt 'node1' cmd.run 'lsattr /etc/passwd'
node1:
-------------------- /etc/passwd
# 添加属性a
[root@master ~]# salt 'node1' cmd.run 'chattr +a /etc/passwd'
node1:
# 查看添加结果
[root@master ~]# salt 'node1' cmd.run 'lsattr /etc/passwd'
node1:
-----a-------------- /etc/passwd
# 删除添加的属性a
[root@master ~]# salt 'node1' cmd.run 'chattr -a /etc/passwd'
node1:
[root@master ~]# salt 'node1' cmd.run 'lsattr /etc/passwd'
node1:
-------------------- /etc/passwd
2.7.27 file.mkdir
创建目录并设置属主、属组及权限
# 创建目录/opt/hehe 属主为root 属组为alice 权限为755
[root@master ~]# salt 'node1' file.mkdir /opt/hehe root alice 755
node1:
True
# 查看创建的/opt/hehe
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt/'
node1:
total 4
drwxr-xr-x 3 root root 34 Nov 4 02:19 123
-rw-r--r-- 1 root root 1023 Nov 4 00:54 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Nov 4 03:48 hehe
2.7.28 file.move
移动或重命名
# 不移动位置改名
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
123
anaconda-ks.cfg
test.sh
# 将/root/123 移动到/root/1234
[root@master ~]# salt 'node1' file.move /root/123 /root/1234
node1:
----------
comment:
'/root/123' moved to '/root/1234'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
1234
anaconda-ks.cfg
test.sh
# 移动到其他位置改名
[root@master ~]# salt 'node1' cmd.run 'ls /opt'
node1:
anaconda-ks.cfg
hehe
# 将/root/1234 移动到/opt/下叫jjyy
[root@master ~]# salt 'node1' file.move /root/1234 /opt/jjyy
node1:
----------
comment:
'/root/1234' moved to '/opt/jjyy'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls /opt'
node1:
anaconda-ks.cfg
hehe
jjyy
2.7.29 file.prepend
把文本插入指定文件的开头,执行一遍加一次
# 查看/root/test.sh文本内容
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
echo "hello world"
xixi
hehe
# 在/root/tests.h/前面加上内容查看
[root@master ~]# salt 'node1' file.prepend /root/test.sh xixi hehe
node1:
Prepended 2 lines to "/root/test.sh"
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
xixi
hehe
echo "hello world"
xixi
hehe
2.7.30 file.sed
修改文本文件的内容,全局修改
# 查看,将xixi改为jjyy
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
xixi
hehe
echo "hello world"
xixi
hehe
# 执行并查看
[root@master ~]# salt 'node1' file.sed /root/test.sh xixi jjyy
node1:
----------
pid:
31777
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
jjyy
hehe
echo "hello world"
jjyy
hehe
# 指定修改
[root@master ~]# salt 'node1' file.sed /root/test.sh jjyy xixi flags=1
node1:
----------
pid:
42402
retcode:
0
stderr:
stdout:
# 查看
[root@master ~]# salt 'node1' cmd.run 'cat /root/test.sh'
node1:
xixi
hehe
echo "hello world"
xixi
hehe
2.7.31 file.read
读取文件内容,与cat命令一样
[root@master ~]# salt 'node1' file.read '/root/test.sh'
node1:
xixi
hehe
echo "hello world"
xixi
hehe
2.7.32 file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件(与ls -a 一样)
[root@master ~]# salt 'node1' file.readdir '/root/'
node1:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .config
- .bash_history
- test.sh.bak
- .viminfo
- test.sh
2.7.33 file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
# 创建递归目录查看
[root@master ~]# salt 'node1' cmd.run 'mkdir -p a/b/c/d/e/f'
node1:
[root@master ~]# salt 'node1' cmd.run 'tree a'
node1:
a
`-- b
`-- c
`-- d
`-- e
`-- f
5 directories, 0 files
# 执行删除
[root@master ~]# salt 'node1' file.remove /root/a
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
anaconda-ks.cfg
test.sh
# 创建文件后删除
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 5 04:15 hello
-rw-r--r-- 1 alice harry 39 Nov 5 04:07 test.sh
[root@master ~]# salt 'node1' file.remove /root/hello
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rw-r--r-- 1 alice harry 39 Nov 5 04:07 test.sh
2.7.34 rename
重命名文件或目录
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
anaconda-ks.cfg
test.sh
# 重命名查看
[root@master ~]# salt 'node1' file.rename /root/test.sh /root/hehe.sh
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
anaconda-ks.cfg
hehe.sh
2.7.35 file.set_mode
给指定文件设置权限
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rw-r--r-- 1 alice harry 39 Nov 5 04:07 hehe.sh
# 设置权限
[root@master ~]# salt 'node1' file.set_mode /root/hehe.sh 755
node1:
0755
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:07 hehe.sh
2.7.36 file.symlink
给指定的文件创建软链接
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:07 hehe.sh
# 给 hehe.sh创建软连接查看
[root@master ~]# salt 'node1' file.symlink /root/hehe.sh /root/hello
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:07 hehe.sh
lrwxrwxrwx 1 root root 13 Nov 5 04:24 hello -> /root/hehe.sh
2.7.37 file.touch
创建空文件或更新时间戳
# 查看并创建一个文件在/root下
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:07 hehe.sh
lrwxrwxrwx 1 root root 13 Nov 5 04:24 hello -> /root/hehe.sh
# 创建查看
[root@master ~]# salt 'node1' file.touch /root/word
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:07 hehe.sh
lrwxrwxrwx 1 root root 13 Nov 5 04:24 hello -> /root/hehe.sh
-rw-r--r-- 1 root root 0 Nov 5 04:27 word
# 对一个已经存在的文件更新时间戳
# 查看时间
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:07(时间) hehe.sh
lrwxrwxrwx 1 root root 13 Nov 5 04:24 hello -> /root/hehe.sh
-rw-r--r-- 1 root root 0 Nov 5 04:27 word
# 更新时间戳/root/hehe.sh
[root@master ~]# salt 'node1' file.touch /root/hehe.sh
node1:
True
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:29(时间更新) hehe.sh
lrwxrwxrwx 1 root root 13 Nov 5 04:24 hello -> /root/hehe.sh
-rw-r--r-- 1 root root 0 Nov 5 04:27 word
2.7.38 file.write
往一个指定的文件里覆盖写入指定内容,如果文件不存在的话创建文件并写入
# 查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:29 hehe.sh
lrwxrwxrwx 1 root root 13 Nov 5 04:24 hello -> /root/hehe.sh
-rw-r--r-- 1 root root 0 Nov 5 04:27 word
# 创建并写入
[root@master ~]# salt 'node1' file.write /root/aaa "hehe" "hhyy" "fdf fdf"
node1:
Wrote 3 lines to "/root/aaa"
# 查看文件是否创建成功
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 root root 18 Nov 5 04:31 aaa
-rw-------. 1 root root 1023 Nov 4 01:58 anaconda-ks.cfg
-rwxr-xr-x 1 alice harry 39 Nov 5 04:29 hehe.sh
lrwxrwxrwx 1 root root 13 Nov 5 04:24 hello -> /root/hehe.sh
-rw-r--r-- 1 root root 0 Nov 5 04:27 word
# 查看创建文件写入的内容
[root@master ~]# salt 'node1' file.read '/root/aaa'
node1:
hehe
hhyy
fdf fdf
# 覆盖写入刚创建的/root/aaa文件
[root@master ~]# salt 'node1' file.write /root/aaa "bagayalu"
node1:
Wrote 1 lines to "/root/aaa"
# 查看
[root@master ~]# salt 'node1' file.read '/root/aaa'
node1:
bagayalu