文章目录
- 1 SaltStack模块介绍
- 2 SaltStack常用模块
- 2.1 SaltStack常用模块之network
- 2.1.1 network.active_tcp
- 2.1.2 network.calc_net
- 2.1.3 network.connect
- 2.1.4 network.default_route
- 2.1.5 network.get_fqdn
- 2.1.6 network.get_hostname
- 2.1.7 network.get_route
- 2.1.8 network.hw_addr
- 2.1.9 network.ifacestartswith
- 2.1.10 network.in_subnet
- 2.1.11 network.interface
- 2.1.12 network.interface_ip
- 2.1.13 network.interfaces
- 2.1.14 network.ip_addrs
- 2.1.15 network.netstat
- 2.1.16 network.ping
- 2.1.17 network.reverse_ip
- 2.2 SaltStack常用模块之service
- 2.3 SaltStack常用模块之pkg
- 2.4 SaltStack常用模块之state
- 2.5 SaltStack常用模块之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.ditectory_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.grep
- 2.7.23 file.is_blkdev
- 2.7.24 file.lsattr
- 2.7.25 file.mkdir
- 2.7.26 file.move
- 2.7.27 file.prepend
- 2.7.28 file.sed
- 2.7.29 file.read
- 2.7.30 file.readdir
- 2.7.31 file.remove
- 2.7.32 file.rename
- 2.7.33 file.set_mode
- 2.7.34 file.symlink
- 2.7.35 file.touch
- 2.7.36 file.uid_to_user
- 2.7.37 file.user_to_uid
- 2.7.38 file.write
1 SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表
- 查看所有module列表
[root@master ~]# salt '192.168.25.147' sys.list_modules
192.168.25.147:
- acl
- aliases
- alternatives
- archive
- artifactory
- baredoc
- beacons
- bigip
- btrfs
- buildout
- chroot
- cloud
- cmd
- composer
- config
- consul
- container_resource
- cp
- cron
- cryptdev
- data
- defaults
- devinfo
- devmap
- disk
- django
- dnsmasq
- dnsutil
- drbd
- environ
- ethtool
- event
- extfs
- file
- firewalld
- freezer
- gem
- genesis
- 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
- nginx
- 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
- random
- random_org
- rbenv
- rest_sample_utils
- restartcheck
- ret
- rvm
- s3
- s6
- salt_proxy
- salt_version
- saltcheck
- saltutil
- schedule
- scsi
- sdb
- seed
- 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
- tuned
- udev
- uptime
- user
- vault
- vbox_guest
- virtualenv
- vsphere
- x509
- xfs
- xml
- zabbix
- zenoss
- 查看指定module的所有function
[root@master ~]# salt '192.168.25.147' sys.list_functions cmd
192.168.25.147:
- 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
- 查看指定module的用法
[root@master ~]# salt '192.168.25.147' sys.doc cmd
'cmd.exec_code:'
Pass in two strings, the first naming the executable language, aka -
python2, python3, ruby, perl, lua, etc. the second string containing
the code you wish to execute. The stdout will be returned.
CLI Example:
salt '*' cmd.exec_code ruby 'puts "cheese"'
'cmd.exec_code_all:'
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. All cmd artifacts (stdout, stderr, retcode, pid)
will be returned.
CLI Example:
salt '*' cmd.exec_code_all ruby 'puts "cheese"'
.....
//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt '192.168.25.147' test.echo,cmd.run,service.status jjyy,date,nginx
192.168.25.147:
----------
cmd.run:
Wed Nov 3 01:03:20 EDT 2021
service.status:
True
test.echo:
jjyy
2 SaltStack常用模块
2.1 SaltStack常用模块之network
2.1.1 network.active_tcp
返回所有活动的tcp连接
[root@master ~]# salt '192.168.25.147' network.active_tcp
192.168.25.147:
----------
0:
----------
local_addr:
192.168.25.147
local_port:
51812
remote_addr:
192.168.25.146
remote_port:
4505
1:
----------
local_addr:
192.168.25.147
local_port:
22
remote_addr:
192.168.25.1
remote_port:
62815
[root@minion ~]# ss -ant
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
ESTAB 0 0 192.168.25.147:51812 192.168.25.146:4505
TIME-WAIT 0 0 192.168.25.147:44078 192.168.25.146:4506
ESTAB 0 52 192.168.25.147:22 192.168.25.1:62815
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::]:80 [::]:*
2.1.2 network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt '192.168.25.147' network.calc_net 192.168.25.147 255.255.255.0
192.168.25.147:
192.168.25.0/24
[root@master ~]# salt '192.168.25.147' network.calc_net 192.168.25.161 255.255.255.0
192.168.25.147:
192.168.25.0/24
2.1.3 network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt '192.168.25.147' network.connect baidu.com 80
192.168.25.147:
----------
comment:
Successfully connected to baidu.com (220.181.38.251) on tcp port 80
result:
True
[root@master ~]# salt '192.168.25.147' network.connect 192.168.25.146 22
192.168.25.147:
----------
comment:
Successfully connected to 192.168.25.146 (192.168.25.146) on tcp port 22
result:
True
2.1.4 network.default_route
查看默认路由
[root@master ~]# salt '192.168.25.147' network.default_route
192.168.25.147:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.25.2
interface:
ens33
netmask:
0.0.0.0
[root@master ~]# salt '192.168.25.148' network.default_route
192.168.25.148:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.25.2
interface:
ens33
netmask:
0.0.0.0
2.1.5 network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt '192.168.25.147' network.get_fqdn
192.168.25.147:
minion
[root@master ~]# salt '192.168.25.148' network.get_fqdn
192.168.25.148:
localhost.localdomain
[root@master ~]# salt '192.168.25.148' network.get_fqdn
192.168.25.148:
minion2
2.1.6 network.get_hostname
获取主机名
[root@master ~]# salt '*' network.get_hostname
192.168.25.147:
minion
192.168.25.148:
minion2
master:
master
2.1.7 network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt '192.168.25.147' network.get_route 192.168.25.161
192.168.25.147:
----------
destination:
192.168.25.161
gateway:
None
interface:
ens33
source:
192.168.25.147
[root@master ~]# salt '192.168.25.147' network.get_route 0.0.0.0
192.168.25.147:
----------
destination:
0.0.0.0
gateway:
None
interface:
lo
source:
127.0.0.1
2.1.8 network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt '192.168.25.147' network.hw_addr ens33
192.168.25.147:
00:0c:29:a8:82:48
[root@master ~]# salt '192.168.25.147' network.hw_addr lo
192.168.25.147:
00:00:00:00:00:00
2.1.9 network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt '192.168.25.147' network.ifacestartswith 192.168
192.168.25.147:
- ens33
[root@master ~]# salt '192.168.25.147' network.ifacestartswith 192
192.168.25.147:
- ens33
2.1.10 network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt '192.168.25.147' network.in_subnet 192.168.0.0/24
192.168.25.147:
False
[root@master ~]# salt '192.168.25.147' network.in_subnet 192.168.0.0/16
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' network.in_subnet 192.168.0.0/32
192.168.25.147:
False
2.1.11 network.interface
返回指定网卡的信息
[root@master ~]# salt '192.168.25.147' network.interface ens33
192.168.25.147:
|_
----------
address:
192.168.25.147
broadcast:
192.168.25.255
label:
ens33
netmask:
255.255.255.0
[root@master ~]# salt '192.168.25.147' network.interface lo
192.168.25.147:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
2.1.12 network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt '192.168.25.147' network.interface_ip ens33
192.168.25.147:
192.168.25.147
[root@master ~]# salt '192.168.25.147' network.interface_ip lo
192.168.25.147:
127.0.0.1
2.1.13 network.interfaces
返回当前系统中所有的网卡信息
[root@master ~]# salt '192.168.25.147' network.interfaces
192.168.25.147:
----------
ens33:
----------
hwaddr:
00:0c:29:a8:82:48
inet:
|_
----------
address:
192.168.25.147
broadcast:
192.168.25.255
label:
ens33
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::5ce7:c114:d39e:59b8
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 '192.168.25.147' network.ip_addrs
192.168.25.147:
- 192.168.25.147
[root@master ~]# salt '192.168.25.148' network.ip_addrs
192.168.25.148:
- 192.168.25.148
[root@minion2 ~]# ip addr add 192.168.25.149/24 dev ens33
[root@master ~]# salt '192.168.25.148' network.ip_addrs
192.168.25.148:
- 192.168.25.148
- 192.168.25.149
2.1.15 network.netstat
返回所有打开的端口和状态
[root@master ~]# salt '192.168.25.147' network.netstat
192.168.25.147:
|_
----------
inode:
29959
local-address:
192.168.25.147%ens33:68
program:
NetworkManager
proto:
udp
recv-q:
0
remote-address:
192.168.25.254:67
send-q:
0
user:
0
|_
----------
inode:
27842
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:
27843
local-address:
[::1]:323
program:
chronyd
proto:
udp
recv-q:
0
remote-address:
[::]:*
send-q:
0
user:
0
|_
----------
inode:
27477
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:
28962
local-address:
0.0.0.0:80
program:
nginx
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
31519
local-address:
192.168.25.147:51812
program:
salt-minion
proto:
tcp
recv-q:
0
remote-address:
192.168.25.146:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
31949
local-address:
192.168.25.147:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.25.1:62815
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
27479
local-address:
[::]:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
[::]:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
28963
local-address:
[::]:80
program:
nginx
proto:
tcp
recv-q:
0
remote-address:
[::]:*
send-q:
128
state:
LISTEN
user:
0
2.1.16 network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt '192.168.25.147' network.ping baidu.com
192.168.25.147:
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=44.9 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=2 ttl=128 time=44.1 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=3 ttl=128 time=54.5 ms
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=4 ttl=128 time=42.9 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 42.871/46.586/54.514/4.632 ms
2.1.17 network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt '192.168.25.147' network.reverse_ip 192.168.25.147
192.168.25.147:
147.25.168.192.in-addr.arpa
2.2 SaltStack常用模块之service
2.2.1 service.available
判断指定的服务是否可用
[root@master ~]# salt '192.168.25.147' service.available nginx //查看服务是否安装
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' service.available sshd
192.168.25.147:
True
2.2.2 service.get_all
获取所有正在运行的服务
[root@master ~]# salt '192.168.25.147' service.get_all
192.168.25.147:
- 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
- console-getty
- container-getty@
- cpupower
- crond
- cryptsetup-pre.target
- cryptsetup.target
- ctrl-alt-del.target
- dbus
- dbus-org.freedesktop.hostname1
- dbus-org.freedesktop.locale1
- dbus-org.freedesktop.login1
- dbus-org.freedesktop.nm-dispatcher
- dbus-org.freedesktop.portable1
- dbus-org.freedesktop.timedate1
- dbus.socket
- debug-shell
- default.target
- dev-hugepages.mount
- dev-mqueue.mount
- dm-event
- dm-event.socket
- dnf-makecache
- dnf-makecache.timer
- dracut-cmdline
- dracut-initqueue
- dracut-mount
- dracut-pre-mount
- dracut-pre-pivot
- dracut-pre-trigger
- dracut-pre-udev
- dracut-shutdown
- ebtables
- emergency
- emergency.target
- exit.target
- final.target
- firewalld
- fstrim
- fstrim.timer
- getty-pre.target
- getty.target
- getty@
- graphical.target
- grub-boot-indeterminate
- halt-local
- halt.target
- hibernate.target
- hybrid-sleep.target
- import-state
- initrd-cleanup
- initrd-fs.target
- initrd-parse-etc
- initrd-root-device.target
- initrd-root-fs.target
- initrd-switch-root
- initrd-switch-root.target
- initrd-udevadm-cleanup-db
- initrd.target
- iprdump
- iprinit
- iprupdate
- iprutils.target
- irqbalance
- kdump
- kexec.target
- kmod-static-nodes
- ldconfig
- loadmodules
- local-fs-pre.target
- local-fs.target
- lvm2-lvmpolld
- lvm2-lvmpolld.socket
- lvm2-monitor
- lvm2-pvscan@
- man-db-cache-update
- messagebus
- microcode
- multi-user.target
- network-online.target
- network-pre.target
- network.target
- nftables
- nginx
- nis-domainname
- nss-lookup.target
- nss-user-lookup.target
- paths.target
- plymouth-halt
- plymouth-kexec
- plymouth-poweroff
- plymouth-quit
- plymouth-quit-wait
- plymouth-read-write
- plymouth-reboot
- plymouth-start
- plymouth-switch-root
- plymouth-switch-root-initramfs
- polkit
- poweroff.target
- printer.target
- proc-sys-fs-binfmt_misc.automount
- proc-sys-fs-binfmt_misc.mount
- quotaon
- rc-local
- rdisc
- reboot.target
- remote-cryptsetup.target
- remote-fs-pre.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
- runlevel4.target
- runlevel5.target
- runlevel6.target
- salt-minion
- salt-proxy@
- selinux-autorelabel
- selinux-autorelabel-mark
- selinux-autorelabel.target
- serial-getty@
- shutdown.target
- sigpwr.target
- sleep.target
- slices.target
- smartcard.target
- sockets.target
- sound.target
- sshd
- sshd-keygen.target
- sshd-keygen@
- sshd.socket
- sshd@
- sssd
- sssd-autofs
- sssd-autofs.socket
- sssd-kcm
- sssd-kcm.socket
- sssd-nss
- sssd-nss.socket
- sssd-pac
- sssd-pac.socket
- sssd-pam
- sssd-pam-priv.socket
- sssd-pam.socket
- sssd-ssh
- sssd-ssh.socket
- sssd-sudo
- sssd-sudo.socket
- suspend-then-hibernate.target
- suspend.target
- swap.target
- sys-fs-fuse-connections.mount
- sys-kernel-config.mount
- sys-kernel-debug.mount
- sysinit.target
- syslog
- syslog.socket
- system-update-cleanup
- system-update-pre.target
- system-update.target
- systemd-ask-password-console
- systemd-ask-password-console.path
- systemd-ask-password-plymouth
- systemd-ask-password-plymouth.path
- systemd-ask-password-wall
- systemd-ask-password-wall.path
- systemd-backlight@
- systemd-binfmt
- systemd-coredump.socket
- systemd-coredump@
- systemd-exit
- systemd-firstboot
- systemd-fsck-root
- systemd-fsck@
- systemd-halt
- systemd-hibernate
- systemd-hibernate-resume@
- systemd-hostnamed
- systemd-hwdb-update
- systemd-hybrid-sleep
- systemd-initctl
- systemd-initctl.socket
- systemd-journal-catalog-update
- systemd-journal-flush
- systemd-journald
- systemd-journald-audit.socket
- systemd-journald-dev-log.socket
- systemd-journald.socket
- systemd-kexec
- systemd-localed
- systemd-logind
- systemd-machine-id-commit
- systemd-modules-load
- systemd-portabled
- systemd-poweroff
- systemd-quotacheck
- systemd-random-seed
- systemd-reboot
- systemd-remount-fs
- systemd-resolved
- systemd-rfkill
- systemd-rfkill.socket
- systemd-suspend
- systemd-suspend-then-hibernate
- systemd-sysctl
- systemd-sysusers
- systemd-timedated
- systemd-tmpfiles-clean
- systemd-tmpfiles-clean.timer
- systemd-tmpfiles-setup
- systemd-tmpfiles-setup-dev
- systemd-udev-settle
- systemd-udev-trigger
- systemd-udevd
- systemd-udevd-control.socket
- systemd-udevd-kernel.socket
- systemd-update-done
- systemd-update-utmp
- systemd-update-utmp-runlevel
- systemd-user-sessions
- systemd-vconsole-setup
- systemd-volatile-root
- tcsd
- teamd@
- time-sync.target
- timedatex
- timers.target
- tmp.mount
- tuned
- umount.target
- unbound-anchor
- unbound-anchor.timer
- user-runtime-dir@
- user@
- vgauthd
- vmtoolsd
2.2.3 service.disabled
检查指定服务是否开机不自动启动
[root@master ~]# salt '192.168.25.147' service.disabled nginx
192.168.25.147:
False
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Wed 2021-11-03 06:54:56 EDT; 4h 55min left
Process: 2383 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCE>
Main PID: 1020 (nginx)
Tasks: 5 (limit: 49290)
Memory: 19.1M
CGroup: /system.slice/nginx.service
├─1020 nginx: master process /usr/sbin/nginx
├─2387 nginx: worker process
├─2388 nginx: worker process
├─2389 nginx: worker process
└─2390 nginx: worker process
2.2.4 service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt '192.168.25.147' service.enabled nginx
192.168.25.147:
True
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Wed 2021-11-03 06:54:56 EDT; 4h 55min left
Process: 2383 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCE>
Main PID: 1020 (nginx)
Tasks: 5 (limit: 49290)
Memory: 19.1M
CGroup: /system.slice/nginx.service
├─1020 nginx: master process /usr/sbin/nginx
├─2387 nginx: worker process
├─2388 nginx: worker process
├─2389 nginx: worker process
└─2390 nginx: worker process
2.2.5 service.disable
设置指定服务开机不自动启动
[root@master ~]# salt '192.168.25.147' service.disable nginx
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' service.enabled nginx
192.168.25.147:
False
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset>
Active: active (running) since Wed 2021-11-03 06:54:56 EDT; 4h 54min left
Main PID: 1020 (nginx)
Tasks: 5 (limit: 49290)
Memory: 19.1M
CGroup: /system.slice/nginx.service
├─1020 nginx: master process /usr/sbin/nginx
├─2387 nginx: worker process
├─2388 nginx: worker process
├─2389 nginx: worker process
└─2390 nginx: worker process
2.2.6 service.enable
设置指定服务开机自动启动
[root@master ~]# salt '192.168.25.147' service.enable nginx
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' service.enabled nginx
192.168.25.147:
True
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Wed 2021-11-03 06:54:56 EDT; 4h 53min left
Main PID: 1020 (nginx)
Tasks: 5 (limit: 49290)
Memory: 19.1M
CGroup: /system.slice/nginx.service
├─1020 nginx: master process /usr/sbin/nginx
├─2387 nginx: worker process
├─2388 nginx: worker process
├─2389 nginx: worker process
└─2390 nginx: worker process
2.2.7 service.reload
重新加载指定服务
[root@master ~]# salt '192.168.25.147' service.reload nginx
192.168.25.147:
True
2.2.8 service.stop
停止指定服务
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Wed 2021-11-03 02:02:42 EDT; 1s ago
Process: 2493 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2491 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2488 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0>
Main PID: 2494 (nginx)
Tasks: 5 (limit: 49290)
Memory: 8.9M
CGroup: /system.slice/nginx.service
├─2494 nginx: master process /usr/sbin/nginx
├─2495 nginx: worker process
├─2496 nginx: worker process
├─2497 nginx: worker process
└─2498 nginx: worker process
[root@master ~]# salt '192.168.25.147' service.stop nginx
192.168.25.147:
True
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: inactive (dead) since Wed 2021-11-03 02:03:30 EDT; 15s ago
Process: 2493 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2491 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2488 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0>
Main PID: 2494 (code=exited, status=0/SUCCESS)
2.2.9 service.start
启动指定服务
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: inactive (dead) since Wed 2021-11-03 04:17:20 EDT; 5s ago
Main PID: 2527 (code=exited, status=0/SUCCESS)
[root@master ~]# salt '192.168.25.147' service.start nginx
192.168.25.147:
True
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Wed 2021-11-03 04:17:52 EDT; 9s ago
Process: 25670 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 25667 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 25665 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=>
Main PID: 25671 (nginx)
Tasks: 5 (limit: 49290)
Memory: 8.8M
CGroup: /system.slice/nginx.service
├─25671 nginx: master process /usr/sbin/nginx
├─25672 nginx: worker process
├─25673 nginx: worker process
├─25674 nginx: worker process
└─25675 nginx: worker process
2.2.10 service.restart
重启指定服务
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: inactive (dead) since Wed 2021-11-03 02:03:30 EDT; 15s ago
Process: 2493 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2491 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2488 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0>
Main PID: 2494 (code=exited, status=0/SUCCESS)
[root@master ~]# salt '192.168.25.147' service.restart nginx
192.168.25.147:
True
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Wed 2021-11-03 02:04:37 EDT; 9s ago
Process: 2526 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2524 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2522 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0>
Main PID: 2527 (nginx)
Tasks: 5 (limit: 49290)
Memory: 8.9M
CGroup: /system.slice/nginx.service
├─2527 nginx: master process /usr/sbin/nginx
├─2528 nginx: worker process
├─2529 nginx: worker process
├─2530 nginx: worker process
└─2531 nginx: worker process
2.2.11 service.status
查看指定服务的状态
[root@master ~]# salt '192.168.25.147' service.status nginx
192.168.25.147:
True
2.2.12 service.missing
查看指定服务是否存在
[root@master ~]# salt '192.168.25.147' service.missing nginx
192.168.25.147:
False
[root@master ~]# salt '192.168.25.147' service.missing httpd
192.168.25.147:
True
2.3 SaltStack常用模块之pkg
2.3.1 pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion
端安装yum-utils
,可以使用 cmd.run
进行远程安装
[root@master ~]# salt '192.168.25.147' pkg.download wget
192.168.25.147:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm //下载好的软件放在这里
[root@minion ~]# ls /var/cache/yum/packages/
wget-1.19.5-10.el8.x86_64.rpm
2.3.2 pkg.file_list
列出指定包或系统中已安装的所有包的文件
//列出已安装的nginx软件包提供的所有文件
[root@master ~]# salt '192.168.25.147' pkg.file_list nginx
192.168.25.147:
----------
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
- /etc/nginx/koi-win
- /etc/nginx/mime.types
- /etc/nginx/mime.types.default
- /etc/nginx/nginx.conf
- /etc/nginx/nginx.conf.default
- /etc/nginx/scgi_params
- /etc/nginx/scgi_params.default
- /etc/nginx/uwsgi_params
- /etc/nginx/uwsgi_params.default
- /etc/nginx/win-utf
- /usr/bin/nginx-upgrade
- /usr/lib/.build-id
- /usr/lib/.build-id/2d
- /usr/lib/.build-id/2d/da6018ae12edb856ad3d2cf61bf586b6b4873c
- /usr/lib/systemd/system/nginx.service
- /usr/lib64/nginx/modules
- /usr/sbin/nginx
- /usr/share/doc/nginx
- /usr/share/doc/nginx/CHANGES
- /usr/share/doc/nginx/README
- /usr/share/doc/nginx/README.dynamic
- /usr/share/licenses/nginx
- /usr/share/licenses/nginx/LICENSE
- /usr/share/man/man3/nginx.3pm.gz
- /usr/share/man/man8/nginx-upgrade.8.gz
- /usr/share/man/man8/nginx.8.gz
- /usr/share/nginx/html/404.html
- /usr/share/nginx/html/50x.html
- /usr/share/nginx/html/index.html
- /usr/share/nginx/html/nginx-logo.png
- /usr/share/nginx/html/poweredby.png
- /usr/share/vim/vimfiles/ftdetect/nginx.vim
- /usr/share/vim/vimfiles/indent/nginx.vim
- /usr/share/vim/vimfiles/syntax/nginx.vim
- /var/lib/nginx
- /var/lib/nginx/tmp
- /var/log/nginx
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
2.3.3 pkg.group_list
列出系统中所有的包组
[root@master ~]# salt '192.168.25.147' pkg.group_list
192.168.25.147:
----------
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.4 pkg.group_info
查看包组的信息
[root@master ~]# salt '192.168.25.147' pkg.group_info 'Development Tools'
192.168.25.147:
----------
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.5 pkg.install
安装软件
[root@master ~]# salt '192.168.25.147' pkg.install wget
192.168.25.147:
----------
wget:
----------
new:
1.19.5-10.el8
old:
[root@master ~]# salt '192.168.25.147' cmd.run 'rpm -q wget'
192.168.25.147:
wget-1.19.5-10.el8.x86_64
2.3.6 pkg.list_downloaded
列出已下载到本地的软件包(本机上的软件包)
[root@master ~]# salt '192.168.25.147' pkg.list_downloaded
192.168.25.147:
----------
wget:
----------
1.14-18.el7:
----------
creation_date_time:
2019-02-22T17:11:52
creation_date_time_t:
1550826712
path:
/var/cache/yum/packages/wget-1.14-18.el7.x86_64.rpm
size:
560152
2.3.7 pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt '192.168.25.147' pkg.list_pkgs
192.168.25.147:
----------
GeoIP:
1.5.0-11.el7
NetworkManager:
1:1.8.0-9.el7
NetworkManager-libnm:
1:1.8.0-9.el7
NetworkManager-team:
1:1.8.0-9.el7
NetworkManager-tui:
1:1.8.0-9.el7
NetworkManager-wifi:
1:1.8.0-9.el7
PyYAML:
3.11-1.el7
...此处省略N行
2.3.8 pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt '192.168.25.147' pkg.owner /usr/sbin/nginx //要使用绝对路径
192.168.25.147:
nginx
[root@master ~]# salt '192.168.25.147' pkg.owner /usr/bin/wget
192.168.25.147:
wget
[root@master ~]# salt '192.168.25.147' pkg.owner /etc/passwd //查看文件由那个软件包提供
192.168.25.147:
setup
2.3.9 pkg.remove
卸载指定软件
[root@master ~]# salt '192.168.25.147' cmd.run 'rpm -qa|grep wget'
192.168.25.147:
wget-1.19.5-10.el8.x86_64
[root@master ~]# salt '192.168.25.147' pkg.remove wget
192.168.25.147:
----------
wget:
----------
new:
old:
1.19.5-10.el8
[root@master ~]# salt '192.168.25.147' cmd.run 'rpm -qa|grep wget'
192.168.25.147:
ERROR: Minions returned with non-zero exit code
//若要卸载多个文件,中间需要用逗号隔开
2.3.10 pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt '192.168.25.147' cmd.run 'rpm -qa|grep wget'
192.168.25.147:
wget-1.14-18.el7_6.1.x86_64
[root@master ~]# salt '192.168.25.147' pkg.upgrade name=wget
192.168.25.147:
----------
wget:
----------
new:
1.19.5-10.el8
old:
1.14-18.el7_6.1
//若想升级系统中所有的软件包则把 name 参数去掉即可
2.3.11 pkg.group_install
安装包组
[root@master ~]# salt '192.168.25.147' pkg.group_install 'Development Tools'
192.168.25.147:
----------
adobe-mappings-cmap:
----------
new:
20171205-3.el8
old:
adobe-mappings-cmap-deprecated:
----------
new:
20171205-3.el8
old:
adobe-mappings-pdf:
----------
new:
20180407-1.el8
old:
annobin:
----------
new:
10.15-2.el8
old:
asciidoc:
----------
new:
8.6.10-0.5.20180627gitf7c2274.el8
old:
autoconf:
----------
new:
2.69-29.el8
old:
automake:
----------
new:
1.16.1-7.el8
old:
binutils:
----------
new:
2.30-108.el8
old:
bison:
----------
new:
3.0.4-10.el8
old:
boost-atomic:
----------
new:
1.66.0-10.el8
old:
//如果安装多个包组,包组之间使用‘,’隔开
2.4 SaltStack常用模块之state
2.4.1 state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt '*' state.show_highstate
192.168.25.147:
----------
master:
----------
192.168.25.161:
----------
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
192.168.25.148:
----------
2.4.2 state.highstate
执行高级状态
[root@master ~]# salt '192.168.25.161' state.highstate web.apache.install
192.168.25.161:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 15:16:43.031222
Duration: 596.505 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 15:16:43.629936
Duration: 32.736 ms
Changes:
Summary for 192.168.25.161
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 629.241 ms
2.4.3 state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt '*' state.show_state_usage
192.168.25.147:
Passed invalid arguments to state.show_state_usage: argument of type 'NoneType' is not iterable
Retrieve the highstate data from the salt master to analyse used and unused states
Custom Pillar data can be passed with the ``pillar`` kwarg.
CLI Example:
.. code-block:: bash
salt '*' state.show_state_usage
master:
Passed invalid arguments to state.show_state_usage: argument of type 'NoneType' is not iterable
Retrieve the highstate data from the salt master to analyse used and unused states
Custom Pillar data can be passed with the ``pillar`` kwarg.
CLI Example:
.. code-block:: bash
salt '*' state.show_state_usage
192.168.25.148:
Passed invalid arguments to state.show_state_usage: argument of type 'NoneType' is not iterable
Retrieve the highstate data from the salt master to analyse used and unused states
Custom Pillar data can be passed with the ``pillar`` kwarg.
CLI Example:
.. code-block:: bash
salt '*' state.show_state_usage
192.168.25.161:
----------
base:
----------
count_all:
3
count_unused:
2
count_used:
1
unused:
- top
- web.nginx.install
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:
test:
----------
count_all:
0
count_unused:
0
count_used:
0
unused:
used:
2.4.4 state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt '*' state.show_top
master:
----------
192.168.25.148:
----------
192.168.25.147:
----------
192.168.25.161:
----------
base:
- web.apache.install
2.4.5 state.top
执行指定的top file,而不是默认的
[root@master ~]# salt '192.168.25.161' state.top jj.sls
192.168.25.161:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 15:21:00.680857
Duration: 661.246 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 15:21:01.344391
Duration: 35.299 ms
Changes:
Summary for 192.168.25.161
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 696.545 ms
2.4.6 state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt '192.168.25.161' state.show_sls web.apache.install
192.168.25.161:
----------
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
2.5.1 user.add
创建用户
// 语法
salt '*' user.add name <uid> <gid> <groups> <home> <shell>
[root@master ~]# salt '192.168.25.147' user.add jj 875
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'id jj'
192.168.25.147:
uid=875(jj) gid=1000(jj) groups=1000(jj)
2.5.2 user.chfullname
更改用户的全名(对用户进行描述)
[root@master ~]# salt '192.168.25.147' user.chfullname jj "无效用户"
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /etc/passwd | tail -1'
192.168.25.147:
jj:x:875:1000:无效用户:/home/jj:/bin/bash
2.5.3 user.chuid
为指定用户更改 uid
[root@master ~]# salt '192.168.25.147' cmd.run 'id jj'
192.168.25.147:
uid=875(jj) gid=997(systemd-coredump) groups=997(systemd-coredump)
[root@master ~]# salt '192.168.25.147' user.chuid jj 987
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'id jj'
192.168.25.147:
uid=987(jj) gid=997(systemd-coredump) groups=997(systemd-coredump)
2.5.4 user.chgid
更改用户GID(强制使用 GID 作为新的主要组)
[root@master ~]# salt '192.168.25.147' cmd.run 'id jj'
192.168.25.147:
uid=875(jj) gid=1000(jj) groups=1000(jj)
[root@master ~]# salt '192.168.25.147' user.chgid jj 997
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'id jj'
192.168.25.147:
uid=875(jj) gid=997(systemd-coredump) groups=997(systemd-coredump)
//前提是要有这个GID的组
2.5.5 user.chgroups
更改此用户所属的组(也可添加附加组)
[root@master ~]# salt '192.168.25.147' user.chgroups jj root,jj True
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'id jj'
192.168.25.147:
uid=987(jj) gid=997(systemd-coredump) groups=997(systemd-coredump),1000(jj),0(root)
2.5.6 user.chhome
更改用户的家居目录,如果旧主页目录存在,则通过 True 将文件移动到新家庭目录
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /etc/passwd | tail -1'
192.168.25.147:
jj:x:875:1000:无效用户:/home/jj:/bin/bash
[root@master ~]# salt '192.168.25.147' user.chhome jj /home/users/jj True
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /etc/passwd | tail -1'
192.168.25.147:
jj:x:987:997:无效用户:/home/users/jj:/bin/bash
2.5.7 user.chshell
修改用户的默认shell
[root@master ~]# salt '192.168.25.147' user.chshell jj /bin/zsh
192.168.25.147:
True
2.5.8 user.list_groups
返回指定用户所属的组列表
[root@master ~]# salt '192.168.25.147' user.list_groups jj
192.168.25.147:
- jj
- root
- systemd-coredump
2.5.9 user.list_users
返回所有用户的列表
[root@master ~]# salt '192.168.25.147' user.list_users
192.168.25.147:
- adm
- bin
- chrony
- daemon
- dbus
- ftp
- games
- halt
- jj
- lp
- mail
- nginx
- nobody
- operator
- pesign
- polkitd
- rngd
- root
- shutdown
- sshd
- sssd
- sync
- systemd-coredump
- systemd-resolve
- tss
- unbound
2.5.10 user.rename
修改命名用户的用户名
[root@master ~]# salt '192.168.25.147' user.rename jj zj
192.168.25.147:
False
[root@master ~]# salt '192.168.25.147' cmd.run 'id jj'
192.168.25.147:
id: 'jj': no such user
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt '192.168.25.147' cmd.run 'id zj'
192.168.25.147:
uid=987(zj) gid=997(systemd-coredump) groups=997(systemd-coredump),1000(jj),0(root)
2.5.11 user.info
返回用户信息
[root@master ~]# salt '192.168.25.147' user.info zj
192.168.25.147:
----------
fullname:
无效用户
gid:
997
groups:
- jj
- root
- systemd-coredump
home:
/home/users/jj
homephone:
name:
zj
other:
passwd:
x
roomnumber:
shell:
/bin/zsh
uid:
987
workphone:
2.5.12 user.getent
返回所有系统用户信息的列表
[root@master ~]# salt '192.168.25.147' user.getent
192.168.25.147:
|_
----------
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:
|_
----------
fullname:
daemon
gid:
2
groups:
- daemon
home:
/sbin
homephone:
name:
daemon
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
2
workphone:
|_
----------
fullname:
adm
gid:
4
groups:
- adm
home:
/var/adm
homephone:
name:
adm
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
3
workphone:
|_
----------
fullname:
lp
gid:
7
groups:
- lp
home:
/var/spool/lpd
homephone:
name:
lp
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
4
workphone:
|_
----------
fullname:
sync
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
sync
other:
passwd:
x
roomnumber:
shell:
/bin/sync
uid:
5
workphone:
|_
----------
fullname:
shutdown
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
shutdown
other:
passwd:
x
roomnumber:
shell:
/sbin/shutdown
uid:
6
workphone:
|_
----------
fullname:
halt
gid:
0
groups:
- root
home:
/sbin
homephone:
name:
halt
other:
passwd:
x
roomnumber:
shell:
/sbin/halt
uid:
7
workphone:
|_
----------
fullname:
mail
gid:
12
groups:
- mail
home:
/var/spool/mail
homephone:
name:
mail
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
8
workphone:
|_
----------
fullname:
operator
gid:
0
groups:
- root
home:
/root
homephone:
name:
operator
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
......
2.5.13 user.delete
删除用户
[root@master ~]# salt '192.168.25.147' user.delete zj
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'id zj'
192.168.25.147:
id: 'zj': no such user
ERROR: Minions returned with non-zero exit code
2.5.14 为用户设置密码
[root@master ~]# salt '192.168.25.147' user.add tom
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'echo "jj" | passwd --stdin tom'
192.168.25.147:
Changing password for user tom.
passwd: all authentication tokens updated successfully.
2.6 SaltStack常用模块之salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt '192.168.25.147' cmd.run 'ls /usr/src'
192.168.25.147:
debug
kernels
wget-1.14-18.el7_6.1.x86_64.rpm
[root@master ~]# salt-cp '192.168.25.147' /root/test.sh '/usr/src'
192.168.25.147:
----------
/usr/src/test.sh:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls /usr/src'
192.168.25.147:
debug
kernels
test.sh
wget-1.14-18.el7_6.1.x86_64.rpm
//远程控制执行脚本
[root@master ~]# salt '192.168.25.147' cmd.run '/bin/bash /usr/src/test.sh'
192.168.25.147:
hello word
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# ls
abc anaconda-ks.cfg test.sh
[root@master ~]# salt-cp '192.168.25.147' /root/abc /root/test.sh /usr/src/
192.168.25.147:
----------
/usr/src/abc:
True
/usr/src/test.sh:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls /usr/src'
192.168.25.147:
abc
debug
kernels
test.sh
2.7 SaltStack常用模块之file
2.7.1 file.access
检查指定路径是否存在
[root@master ~]# salt '192.168.25.147' cmd.run 'ls /usr/src'
192.168.25.147:
debug
kernels
test.sh
[root@master ~]# salt '192.168.25.147' file.access /usr/src/test.sh f
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' file.access /usr/src/abc f
192.168.25.147:
False
检查指定文件的权限信息
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /usr/src'
192.168.25.147:
total 4
drwxr-xr-x. 2 root root 6 May 18 2020 debug
drwxr-xr-x. 3 root root 35 Nov 3 03:20 kernels
-rw-r--r-- 1 root root 31 Nov 4 02:40 test.sh
[root@master ~]# salt '192.168.25.147' file.access /usr/src/test.sh r //是否有读权限
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' file.access /usr/src/test.sh w //是否有写权限
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' file.access /usr/src/test.sh x //是否有执行权限
192.168.25.147:
False
2.7.2 file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root/abc'
192.168.25.147:
-rw-r--r-- 1 root root 0 Nov 3 22:22 /root/abc
[root@master ~]# salt '192.168.25.147' file.append /root/abc "hehe" "xix"
192.168.25.147:
Wrote 2 lines to "/root/abc"
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
hehe
xix
2.7.3 file.basename
获取指定路径的基名
[root@master ~]# salt '192.168.25.147' file.basename '/usr/src/wget-1.14-18.el7_6.1.x86_64.rpm'
192.168.25.147:
wget-1.14-18.el7_6.1.x86_64.rpm
2.7.4 file.dirname
获取指定路径的目录名
[root@master ~]# salt '192.168.25.147' file.dirname '/usr/local/nginx'
192.168.25.147:
/usr/local
2.7.5 file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt '192.168.25.147' cmd.run 'md5sum /usr/src/test.sh'
192.168.25.147:
9e6b5edd195795da117f73922534165f /usr/src/test.sh
[root@master ~]# salt '192.168.25.147' file.check_hash /usr/src/test.sh 9e6b5edd195795da117f73922534165f
192.168.25.147:
True
2.7.6 file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件,而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改,不能删除、更改、移动 | 任何的进程只能修改目录之下的文件,不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@master ~]# salt '192.168.25.147' cmd.run 'lsattr /root'
192.168.25.147:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/test.sh
//添加属性
[root@master ~]# salt '192.168.25.147' file.chattr /root/test.sh operator=add attributes=ai
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'lsattr /root'
192.168.25.147:
-------------------- /root/anaconda-ks.cfg
----ia-------------- /root/test.sh
[root@master ~]# salt '192.168.25.147' cmd.run 'rm -rf /root/test.sh'
192.168.25.147:
rm: cannot remove '/root/test.sh': Operation not permitted
给指定文件去除属性
[root@master ~]# salt '192.168.25.147' file.chattr /root/test.sh operator=remove attributes=a
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'lsattr /root'
192.168.25.147:
-------------------- /root/anaconda-ks.cfg
----i--------------- /root/test.sh
[root@master ~]# salt '192.168.25.147' file.chattr /root/test.sh operator=remove attributes=i
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'lsattr /root'
192.168.25.147:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/test.sh
2.7.7 file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
-rw-r--r-- 1 root root 9 Nov 3 22:24 abc
-rw-------. 1 root root 1023 Sep 26 05:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 3 22:09 test.sh
[root@master ~]# salt '192.168.25.147' file.chown /root/abc nginx nginx //属主和属组必须都写上
192.168.25.147:
None
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
-rw-r--r-- 1 nginx nginx 9 Nov 3 22:24 abc
-rw-------. 1 root root 1023 Sep 26 05:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 3 22:09 test.sh
2.7.8 file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-rw-r--r-- 1 root root 9 Nov 3 22:24 abc
-rw-------. 1 root root 1023 Sep 26 05:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 3 22:09 test.sh
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
hehe
xix
[root@master ~]# salt '192.168.25.147' file.copy /root/abc /root/jj
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
-rw-r--r-- 1 root root 9 Nov 3 22:24 abc
-rw-------. 1 root root 1023 Sep 26 05:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 9 Nov 3 22:29 jj
-rw-r--r-- 1 root root 0 Nov 3 22:09 test.sh
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/jj'
192.168.25.147:
hehe
xix
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt '192.168.25.147' cmd.run 'ls /opt'
192.168.25.147:
abc
[root@master ~]# salt '192.168.25.147' file.copy /opt/ /root/ recurse=True
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls /root'
192.168.25.147:
abc
anaconda-ks.cfg
test.sh
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /tmp/kk'
192.168.25.147:
total 0
-rw-r--r-- 1 root root 0 Nov 4 02:53 ab
-rw-r--r-- 1 root root 0 Nov 4 02:53 abc
-rw-r--r-- 1 root root 0 Nov 4 02:53 test.sh
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root/'
192.168.25.147:
total 12
-rw-r--r-- 1 nginx nginx 9 Nov 3 22:55 abc
-rw-------. 1 root root 1023 Sep 26 05:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:53 test.sh
//拷贝目录
[root@master ~]# salt '192.168.25.147' file.copy /root/ /opt/kk recurse=True remove_existing=True
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /opt/kk'
192.168.25.147:
total 12
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
2.7.9 file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /usr/local'
192.168.25.147:
total 0
drwxr-xr-x. 2 root root 6 May 18 2020 bin
drwxr-xr-x. 2 root root 6 May 18 2020 etc
drwxr-xr-x. 2 root root 6 May 18 2020 games
drwxr-xr-x. 2 root root 6 May 18 2020 include
drwxr-xr-x. 2 root root 6 May 18 2020 lib
drwxr-xr-x. 3 root root 17 Sep 26 05:24 lib64
drwxr-xr-x. 2 root root 6 May 18 2020 libexec
drwxr-xr-x. 2 root root 6 May 18 2020 sbin
drwxr-xr-x. 5 root root 49 Sep 26 05:24 share
drwxr-xr-x. 2 root root 6 May 18 2020 src
[root@master ~]# salt '192.168.25.147' file.directory_exists /usr/local/src
192.168.25.147:
True
2.7.10 file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt '192.168.25.147' cmd.run 'du -sb /usr'
192.168.25.147:
2158351272 /usr
[root@master ~]# salt '192.168.25.147' file.diskusage /usr
192.168.25.147:
2152377082
2.7.11 file.file_exists
判断指定文件是否存在
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root/'
192.168.25.147:
total 8
-rw-r--r-- 1 nginx nginx 9 Nov 3 22:55 abc
-rw-------. 1 root root 1023 Sep 26 05:27 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Nov 3 22:09 test.sh
[root@master ~]# salt '192.168.25.147' file.file_exists /root/test.sh
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' file.file_exists /root/test
192.168.25.147:
False
2.7.12 file.find
类似 find 命令并返回符合指定条件的路径列表
The options include match criteria
name = path-glob # case sensitive
iname = path-glob # case insensitive
regex = path-regex # case sensitive
iregex = path-regex # case insensitive
type = file-types # match any listed type
user = users # match any listed user
group = groups # match any listed group
size = [+-]number[size-unit] # default unit = byte
mtime = interval # modified since date
grep = regex # search file contents
and/or actions
delete [= file-types] # default type = 'f'
exec = command [arg ...] # where {} is replaced by pathname
print [= print-opts]
and/or depth criteria
maxdepth = maximum depth to transverse in path
mindepth = minimum depth to transverse before checking files or directories
The default action is print=path
path-glob
* = match zero or more chars
? = match any char
[abc] = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y] = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c} = match a or b or c
path-regex
: a Python Regex (regular expression) pattern to match pathnames
file-types
: a string of one or more of the following
a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket
users
: a space and/or comma separated list of user names and/or uids
groups
: a space and/or comma separated list of group names and/or gids
size-unit
b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes
interval
[<num>w] [<num>d] [<num>h] [<num>m] [<num>s]
where:
w: week
d: day
h: hour
m: minute
s: second
print-opts: a comma and/or space separated list of one or more of the following
group: group name
md5: MD5 digest of file contents
mode: file permissions (as integer)
mtime: last modification time (as time_t)
name: file basename
path: file absolute path
size: file size in bytes
type: file type
user: user name
示例
salt '*' file.find / type=f name=\*.bak size=+10m
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete
2.7.13 file.get_gid
获取指定文件的gid
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root/abc'
192.168.25.147:
-rw-r--r-- 1 nginx nginx 9 Nov 3 22:55 /root/abc
[root@master ~]# salt '192.168.25.147' file.get_gid /root/abc
192.168.25.147:
990
2.7.14 file.get_group
获取指定文件的组名
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root/abc'
192.168.25.147:
-rw-r--r-- 1 nginx nginx 9 Nov 3 22:55 /root/abc
[root@master ~]# salt '192.168.25.147' file.get_group /root/abc
192.168.25.147:
nginx
2.7.15 file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt '192.168.25.147' cmd.run 'sha256sum /root/abc'
192.168.25.147:
9d7dd933a1bd57ac1460dd3e3dd0b598b5f4af10cdd7021afaf119376f28782d /root/abc
[root@master ~]# salt '192.168.25.147' file.get_hash /root/abc
192.168.25.147:
9d7dd933a1bd57ac1460dd3e3dd0b598b5f4af10cdd7021afaf119376f28782d
2.7.16 file.get_mode
获取指定文件的权限,以数字方式显示
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root/abc'
192.168.25.147:
-rw-r--r-- 1 nginx nginx 9 Nov 3 22:55 /root/abc
[root@master ~]# salt '192.168.25.147' file.get_mode /root/abc
192.168.25.147:
0644
2.7.17 file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -Z /root/anaconda-ks.cfg'
192.168.25.147:
system_u:object_r:admin_home_t:s0 /root/anaconda-ks.cfg
[root@master ~]# salt '192.168.25.147' file.get_selinux_context /root/anaconda-ks.cfg
192.168.25.147:
system_u:object_r:admin_home_t:s0
2.7.18 file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@master ~]# salt '192.168.25.147' cmd.run 'sha256sum /root/abc'
192.168.25.147:
9d7dd933a1bd57ac1460dd3e3dd0b598b5f4af10cdd7021afaf119376f28782d /root/abc
[root@master ~]# salt '192.168.25.147' file.get_sum /root/abc
192.168.25.147:
9d7dd933a1bd57ac1460dd3e3dd0b598b5f4af10cdd7021afaf119376f28782d
[root@master ~]# salt '192.168.25.147' cmd.run 'md5sum /root/abc'
192.168.25.147:
18f5413d793e939aa86af095a71d2472 /root/abc
[root@master ~]# salt '192.168.25.147' file.get_sum /root/abc md5
192.168.25.147:
18f5413d793e939aa86af095a71d2472
2.7.19 file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root/abc'
192.168.25.147:
-rw-r--r-- 1 nginx nginx 9 Nov 3 22:55 /root/abc
[root@master ~]# salt '192.168.25.147' file.get_uid /root/abc
192.168.25.147:
993
[root@master ~]# salt '192.168.25.147' file.get_user /root/abc
192.168.25.147:
nginx
2.7.20 file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root/abc'
192.168.25.147:
-rw-r--r-- 1 nginx nginx 9 Nov 3 22:55 /root/abc
[root@master ~]# salt '192.168.25.147' file.get_gid /root/abc
192.168.25.147:
990
[root@master ~]# salt '192.168.25.147' file.gid_to_group 990
192.168.25.147:
nginx
[root@master ~]# salt '192.168.25.147' file.gid_to_group 0
192.168.25.147:
root
[root@maste
2.7.21 file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt '192.168.25.147' file.group_to_gid root
192.168.25.147:
0
[root@master ~]# salt '192.168.25.147' file.group_to_gid nginx
192.168.25.147:
990
2.7.22 file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
[root@master ~]# salt '192.168.25.147' file.grep /etc/passwd nobody
192.168.25.147:
----------
pid:
2434
retcode:
0
stderr:
stdout:
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
[root@master ~]# salt '192.168.25.147' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 bootproto -- -i
192.168.25.147:
----------
pid:
3077
retcode:
0
stderr:
stdout:
BOOTPROTO=dhcp
[root@master ~]# salt '192.168.25.147' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 bootproto -- -i -A2
192.168.25.147:
----------
pid:
3082
retcode:
0
stderr:
stdout:
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
[root@master ~]# salt '192.168.25.147' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 bootproto -- -i -B2
192.168.25.147:
----------
pid:
3087
retcode:
0
stderr:
stdout:
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
[root@master ~]# salt '192.168.25.147' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 bootproto -- -i -C2
192.168.25.147:
----------
pid:
3092
retcode:
0
stderr:
stdout:
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
[root@master ~]# salt '192.168.25.147' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 bootproto -- -i -l
192.168.25.147:
----------
pid:
3097
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@master ~]# salt '192.168.25.147' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 bootproto -- -il
192.168.25.147:
----------
pid:
3102
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-ens33
2.7.23 file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt '192.168.25.147' file.is_blkdev /dev/sr0
192.168.25.147:
True
2.7.24 file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
[root@master ~]# salt '192.168.25.147' file.lsattr /root/abc
192.168.25.147:
----------
/root/abc:
[root@master ~]# salt '192.168.25.147' cmd.run 'lsattr /root/abc'
192.168.25.147:
----ia-------------- /root/abc
[root@master ~]# salt '192.168.25.147' cmd.run 'chattr +ai /root/abc'
192.168.25.147:
[root@master ~]# salt '192.168.25.147' file.lsattr /root/abc
192.168.25.147:
----------
/root/abc:
- i
- a
2.7.25 file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
[root@master ~]# salt '192.168.25.147' file.mkdir /root/MM
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
drwxr-xr-x 2 root root 6 Nov 4 03:25 MM
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
[root@master ~]# salt '192.168.25.147' file.mkdir /root/QQ tom root 456
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
drwxr-xr-x 2 root root 6 Nov 4 03:25 MM
dr--r-xrw- 2 tom root 6 Nov 4 03:26 QQ
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
2.7.26 file.move
移动或重命名
//重命名
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
drwxr-xr-x 2 root root 6 Nov 4 03:25 MM
dr--r-xrw- 2 tom root 6 Nov 4 03:26 QQ
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
[root@master ~]# salt '192.168.25.147' file.move /root/MM /root/WW
192.168.25.147:
----------
comment:
'/root/MM' moved to '/root/WW'
result:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
dr--r-xrw- 2 tom root 6 Nov 4 03:26 QQ
drwxr-xr-x 2 root root 6 Nov 4 03:25 WW
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
//移动
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
dr--r-xrw- 2 tom root 6 Nov 4 03:26 QQ
drwxr-xr-x 2 root root 6 Nov 4 03:25 WW
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /opt'
192.168.25.147:
total 0
drwxr-xr-x. 2 root root 55 Nov 4 02:58 kk
[root@master ~]# salt '192.168.25.147' file.move /root/WW /opt/
192.168.25.147:
----------
comment:
'/root/WW' moved to '/opt/'
result:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /opt'
192.168.25.147:
total 0
drwxr-xr-x 2 root root 6 Nov 4 03:25 WW
drwxr-xr-x. 2 root root 55 Nov 4 02:58 kk
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
dr--r-xrw- 2 tom root 6 Nov 4 03:26 QQ
-rw-r--r-- 1 root root 8 Nov 4 02:58 abc
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
-rw-r--r-- 1 root root 31 Nov 4 02:58 test.sh
2.7.27 file.prepend
把文本插入指定文件的开头
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
jjj
yyy
[root@master ~]# salt '192.168.25.147' file.prepend /root/abc "hehe" "xixi"
192.168.25.147:
Prepended 2 lines to "/root/abc"
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
hehe
xixi
jjj
yyy
2.7.28 file.sed
修改文本文件的内容
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
hehe haha
xixi hehe
jjj xixi
yyy kkk yyy hehe
aaa ccc hehe
[root@master ~]# salt '192.168.25.147' file.sed /root/abc 'hehe' 'run'
192.168.25.147:
----------
pid:
2705
retcode:
0
stderr:
stdout:
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
run haha
xixi run
jjj xixi
yyy kkk yyy run
aaa ccc run
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
run haha
xixi run run
jjj xixi
yyy kkk yyy run
aaa run ccc run
[root@master ~]# salt '192.168.25.147' file.sed /root/abc 'run' 'time' flags=2
192.168.25.147:
----------
pid:
2763
retcode:
0
stderr:
stdout:
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
run haha
xixi run time
jjj xixi
yyy kkk yyy run
aaa run ccc time
[root@master ~]# salt '192.168.25.147' file.sed /root/abc 'run' 'time' flags=1
192.168.25.147:
----------
pid:
2773
retcode:
0
stderr:
stdout:
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
time haha
xixi time time
jjj xixi
yyy kkk yyy time
aaa time ccc time
2.7.29 file.read
读取文件内容
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/abc'
192.168.25.147:
time haha
xixi time time
jjj xixi
yyy kkk yyy time
aaa time ccc time
[root@master ~]# salt '192.168.25.147' file.read /root/abc
192.168.25.147:
time haha
xixi time time
jjj xixi
yyy kkk yyy time
aaa time ccc time
2.7.30 file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt '192.168.25.147' file.readdir /root
192.168.25.147:
- .
- ..
- abc
- anaconda-ks.cfg
- test.sh
- MM
- QQ
- .bash_history
2.7.31 file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt '192.168.25.147' cmd.run 'tree /root/'
192.168.25.147:
/root/
|-- QQ
| `-- ww
| `-- abc.bak
|-- abc
|-- anaconda-ks.cfg
`-- test.sh
2 directories, 4 files
[root@master ~]# salt '192.168.25.147' file.remove /root/test.sh
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'tree /root/'
192.168.25.147:
/root/
|-- QQ
| `-- ww
| `-- abc.bak
|-- abc
`-- anaconda-ks.cfg
2 directories, 3 files
[root@master ~]# salt '192.168.25.147' file.remove /root/QQ/ww/abc.bak
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'tree /root/'
192.168.25.147:
/root/
|-- QQ
| `-- ww
|-- abc
`-- anaconda-ks.cfg
2 directories, 2 files
2.7.32 file.rename
重命名文件或目录
// 重命名文件
[root@master ~]# salt '192.168.25.147' cmd.run 'tree /root'
192.168.25.147:
/root
|-- QQ
| `-- ww
|-- abc
`-- anaconda-ks.cfg
2 directories, 2 files
[root@master ~]# salt '192.168.25.147' file.rename /root/abc /root/AAA
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'tree /root'
192.168.25.147:
/root
|-- AAA
|-- QQ
| `-- ww
`-- anaconda-ks.cfg
//重命名目录
[root@master ~]# salt '192.168.25.147' cmd.run 'tree /root'
192.168.25.147:
/root
|-- AAA
|-- QQ
| `-- ww
`-- anaconda-ks.cfg
2 directories, 2 files
[root@master ~]# salt '192.168.25.147' file.rename /root/QQ/ww /root/QQ/RR
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'tree /root'
192.168.25.147:
/root
|-- AAA
|-- QQ
| `-- RR
`-- anaconda-ks.cfg
2.7.33 file.set_mode
给指定文件设置权限
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-rw-r--r-- 1 root root 70 Nov 5 04:26 AAA
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
[root@master ~]# salt '192.168.25.147' file.set_mode /root/AAA 0444
192.168.25.147:
0444
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-r--r--r-- 1 root root 70 Nov 5 04:26 AAA
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
2.7.34 file.symlink
给指定的文件创建软链接
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-rw-r--r-- 1 root root 70 Nov 5 04:40 AAA
-rw-r--r-- 1 root root 0 Nov 5 04:39 CCC
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /opt/'
192.168.25.147:
total 0
[root@master ~]# salt '192.168.25.147' file.symlink /root/AAA /opt/mm
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /opt/'
192.168.25.147:
total 0
lrwxrwxrwx 1 root root 9 Nov 5 04:49 mm -> /root/AAA
2.7.35 file.touch
创建空文件或更新时间戳
//创建空文件
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-r--r--r-- 1 root root 70 Nov 5 04:26 AAA
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
[root@master ~]# salt '192.168.25.147' file.touch /root/CCC
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-r--r--r-- 1 root root 70 Nov 5 04:26 AAA
-rw-r--r-- 1 root root 0 Nov 5 04:39 CCC
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
//更新时间戳
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-r--r--r-- 1 root root 70 Nov 5 04:26 AAA
-rw-r--r-- 1 root root 0 Nov 5 04:39 CCC
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
[root@master ~]# salt '192.168.25.147' file.touch /root/AAA
192.168.25.147:
True
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-r--r--r-- 1 root root 70 Nov 5 04:40 AAA
-rw-r--r-- 1 root root 0 Nov 5 04:39 CCC
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
2.7.36 file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@master ~]# salt '192.168.25.147' file.uid_to_user 0
192.168.25.147:
root
[root@master ~]# salt '192.168.25.147' file.uid_to_user 1000
192.168.25.147:
tom
2.7.37 file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@master ~]# salt '192.168.25.147' file.user_to_uid tom
192.168.25.147:
1000
[root@master ~]# salt '192.168.25.147' file.user_to_uid nginx
192.168.25.147:
993
2.7.38 file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/AAA'
192.168.25.147:
time haha
xixi time time
jjj xixi
yyy kkk yyy time
aaa time ccc time
[root@master ~]# salt '192.168.25.147' file.write /root/AAA "tom" "jerry" "zhangsan" "lisi"
192.168.25.147:
Wrote 4 lines to "/root/AAA"
[root@master ~]# salt '192.168.25.147' cmd.run 'cat /root/AAA'
192.168.25.147:
tom
jerry
zhangsan
lisi
//创建新文件写入内容
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 8
-rw-r--r-- 1 root root 27 Nov 5 04:52 AAA
-rw-r--r-- 1 root root 0 Nov 5 04:39 CCC
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg
[root@master ~]# salt '192.168.25.147' file.write /root/MMM "tom" "haha" "jerry" "wuangwu"
192.168.25.147:
Wrote 4 lines to "/root/MMM"
[root@master ~]# salt '192.168.25.147' cmd.run 'ls -l /root'
192.168.25.147:
total 12
-rw-r--r-- 1 root root 27 Nov 5 04:52 AAA
-rw-r--r-- 1 root root 0 Nov 5 04:39 CCC
-rw-r--r-- 1 root root 23 Nov 5 04:53 MMM
dr--r-xrw- 3 tom root 16 Nov 5 04:35 QQ
-rw-r--r-- 1 root root 1023 Nov 4 02:58 anaconda-ks.cfg