SaltStack模块介绍
Module是日常使用SaltStack接触最多的一个组件,其用于管理对象操作,这也是SaltStack通过Push的方式进行管理的入口,比如我们日常简单的执行命令、查看包安装情况、查看服务运行情况等工作都是通过SaltStack Module来实现的。
当安装好Master和Minion包后,系统上会安装很多Module,大家可以通过以下命令查看支持的所有Module列表:
//查看node1的所有module列表
[root@master ~]# salt 'node1' sys.list_modules
node1:
- 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
- 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
- tls
- travisci
- tuned
- udev
- uptime
- user
- vault
- vbox_guest
- virtualenv
- vsphere
- webutil
- x509
- xfs
- xml
- zabbix
- zenoss
//查看node1的指定module的所有function
[root@master ~]# salt 'node1' sys.list_functions cmd
node1:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.powershell
- cmd.powershell_all
- cmd.retcode
- cmd.run
- cmd.run_all
- cmd.run_bg
- cmd.run_chroot
- cmd.run_stderr
- cmd.run_stdout
- cmd.script
- cmd.script_retcode
- cmd.shell
- cmd.shell_info
- cmd.shells
- cmd.tty
- cmd.which
- cmd.which_bin
//查看指定module的用法
[root@master ~]# salt 'node1' sys.doc cmd|less
cmd.exec_code:
Pass in two strings, the first naming the executable language, aka -
python2, python3, ruby, perl, lua, etc. the second string containing
the code you wish to execute. The stdout will be returned.
All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.
CLI Example:
salt '*' cmd.exec_code ruby 'puts "cheese"'
salt '*' cmd.exec_code ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
略......
//SaltStack默认也支持一次执行多个Module,Module之间通过逗号隔开,默认传参之间也是用逗号分隔,也支持指定传参分隔符号--args-separator=@即可
[root@master ~]# salt 'node1' test.echo,cmd.run,service.status hello,date,sshd
node1:
----------
cmd.run:
Wed Nov 3 23:49:20 CST 2021
service.status:
True
test.echo:
hello
SaltStack常用模块
SaltStack常用模块之network
network.active_tcp
返回所有活动的tcp连接
[root@master ~]# salt 'node1' network.active_tcp
node1:
----------
0:
----------
local_addr:
192.168.111.142
local_port:
36216
remote_addr:
192.168.111.141
remote_port:
4505
1:
----------
local_addr:
192.168.111.142
local_port:
22
remote_addr:
192.168.111.1
remote_port:
50256
2:
----------
local_addr:
192.168.111.142
local_port:
22
remote_addr:
192.168.111.1
remote_port:
50255
network.calc_net
通过IP和子网掩码计算出网段
[root@master ~]# salt 'node1' network.calc_net 192.168.111.142 255.255.255.0
node1:
192.168.111.0/24
[root@master ~]# salt 'node1' network.calc_net 192.168.111.142 255.255.248.0
node1:
192.168.104.0/21
network.connect
测试minion至某一台服务器的网络是否连通
[root@master ~]# salt 'node1' network.connect baidu.com 80
node1:
----------
comment:
Successfully connected to baidu.com (220.181.38.148) on tcp port 80
result:
True
network.default_route
查看默认路由
[root@master ~]# salt 'node1' network.default_route
node1:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.111.2
interface:
ens160
netmask:
0.0.0.0
network.get_fqdn
查看主机的fqdn(完全限定域名)
[root@master ~]# salt 'node1' network.get_fqdn
node1:
node1
[root@node1 ~]# hostname node1.example.com
[root@node1 ~]# bash
[root@master ~]# salt 'node1' network.get_fqdn
node1:
node1.example.com
network.get_hostname
获取主机名
[root@master ~]# salt 'node1' network.get_hostname
node1:
node1.example.com
network.get_route
查询到一个目标网络的路由信息
[root@master ~]# salt 'node1' network.get_route 192.168.222.20
node1:
----------
destination:
192.168.222.20
gateway:
192.168.111.2
interface:
ens160
source:
192.168.111.142
network.hw_addr
返回指定网卡的MAC地址
[root@master ~]# salt 'node1' network.hw_addr ens160
node1:
00:0c:29:91:4b:ec
network.ifacestartswith
从特定CIDR检索接口名称
[root@master ~]# salt '*' network.ifacestartswith 192.168
node1:
- ens160
master:
- ens160
network.in_subnet
判断当前主机是否在某一个网段内
[root@master ~]# salt 'node1' network.in_subnet 192.168.0.0/16
node1:
True
network.interface
返回指定网卡的信息
[root@master ~]# salt 'node1' network.interface ens160
node1:
|_
----------
address:
192.168.111.142
broadcast:
192.168.111.255
label:
ens160
netmask:
255.255.255.0
network.interface_ip
返回指定网卡的IP地址
[root@master ~]# salt 'node1' network.interface_ip ens160
node1:
192.168.111.142
network.interfaces
[root@master ~]# salt 'node1' network.interfaces
node1:
----------
ens160:
----------
hwaddr:
00:0c:29:91:4b:ec
inet:
|_
----------
address:
192.168.111.142
broadcast:
192.168.111.255
label:
ens160
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::9bdb:c6a0:688b:97d0
prefixlen:
64
scope:
link
up:
True
lo:
----------
hwaddr:
00:00:00:00:00:00
inet:
|_
----------
address:
127.0.0.1
broadcast:
None
label:
lo
netmask:
255.0.0.0
inet6:
|_
----------
address:
::1
prefixlen:
128
scope:
host
up:
True
network.ip_addrs
返回一个IPv4的地址列表
该函数将会忽略掉127.0.0.1的地址
[root@master ~]# salt 'node1' network.ip_addrs
node1:
- 192.168.111.142
network.netstat
返回所有打开的端口和状态
[root@master ~]# salt 'node1' network.netstat
node1:
|_
----------
inode:
28166
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:
27656
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:
30120
local-address:
192.168.111.142:36216
program:
salt-minion
proto:
tcp
recv-q:
0
remote-address:
192.168.111.141:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
31408
local-address:
192.168.111.142:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.111.1:50256
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
31043
local-address:
192.168.111.142:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
192.168.111.1:50255
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
28176
local-address:
[::]:22
program:
sshd
proto:
tcp
recv-q:
0
remote-address:
[::]:*
send-q:
128
state:
LISTEN
user:
0
|_
----------
inode:
27657
local-address:
[::]:80
program:
nginx
proto:
tcp
recv-q:
0
remote-address:
[::]:*
send-q:
128
state:
LISTEN
user:
0
network.ping
使用ping命令测试到某主机的连通性
[root@master ~]# salt 'node1' network.ping www.baidu.com
node1:
PING www.a.shifen.com (182.61.200.7) 56(84) bytes of data.
64 bytes from localhost (182.61.200.7): icmp_seq=1 ttl=128 time=24.2 ms
64 bytes from localhost (182.61.200.7): icmp_seq=2 ttl=128 time=22.4 ms
64 bytes from localhost (182.61.200.7): icmp_seq=3 ttl=128 time=22.1 ms
64 bytes from localhost (182.61.200.7): icmp_seq=4 ttl=128 time=22.7 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 8ms
rtt min/avg/max/mdev = 22.149/22.853/24.218/0.808 ms
network.reverse_ip
返回一个指定的IP地址的反向地址
[root@master ~]# salt 'node1' network.reverse_ip 192.168.111.1
node1:
1.111.168.192.in-addr.arpa
SaltStack常用模块之service
service.available
判断指定的服务是否可用
[root@master ~]# salt 'node1' service.available sshd
node1:
True
[root@master ~]# salt 'node1' service.available vsftpd
node1:
False
service.get_all
获取所有正在运行的服务
[root@master ~]# salt 'node1' service.get_all
node1:
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- 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
- htcacheclean
- httpd
- httpd.socket
- httpd@
- 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
- 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
- rhnsd
- rhsm
- rhsm-facts
- rhsmcertd
- rngd
- rngd-wake-threshold
- rpcbind.target
- rsyslog
- 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
- timers.target
- tmp.mount
- tuned
- umount.target
- unbound-anchor
- unbound-anchor.timer
- user-runtime-dir@
- user@
- vgauthd
- vmtoolsd
service.disabled
检查指定服务是否开机不自动启动
// node1主机没有安装httpd服务
[root@node1 ~]# systemctl status httpd
Unit httpd.service could not be found.
[root@master ~]# salt 'node1' service.disabled httpd
node1:
True
service.enabled
检查指定服务是否开机自动启动
[root@master ~]# salt 'node1' service.enabled httpd
node1:
False
service.disable
设置指定服务开机不自动启动
[root@master ~]# salt 'node1' service.disable nginx
node1:
True
// 查看node1主机 nginx状态
[root@node1 ~]# 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 22:04:24 CST; 2h 56min ago
Main PID: 1036 (nginx)
Tasks: 2 (limit: 11300)
Memory: 11.6M
CGroup: /system.slice/nginx.service
├─1036 nginx: master process /usr/sbin/nginx
└─1037 nginx: worker process
11月 03 22:04:22 node1 systemd[1]: Starting The nginx HTTP and reverse proxy serve>
11月 03 22:04:23 node1 nginx[988]: nginx: the configuration file /etc/nginx/nginx.>
11月 03 22:04:23 node1 nginx[988]: nginx: configuration file /etc/nginx/nginx.conf>
11月 03 22:04:24 node1 systemd[1]: Started The nginx HTTP and reverse proxy server.
service.enable
设置指定服务开机自动启动
[root@master ~]# salt 'node1' service.enable nginx
node1:
True
// 查看node1主机 nginx状态
[root@node1 ~]# 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 22:04:24 CST; 2h 57min ago
Main PID: 1036 (nginx)
Tasks: 2 (limit: 11300)
Memory: 11.6M
CGroup: /system.slice/nginx.service
├─1036 nginx: master process /usr/sbin/nginx
└─1037 nginx: worker process
11月 03 22:04:22 node1 systemd[1]: Starting The nginx HTTP and reverse proxy serve>
11月 03 22:04:23 node1 nginx[988]: nginx: the configuration file /etc/nginx/nginx.>
11月 03 22:04:23 node1 nginx[988]: nginx: configuration file /etc/nginx/nginx.conf>
11月 03 22:04:24 node1 systemd[1]: Started The nginx HTTP and reverse proxy server.
service.reload
重新加载指定服务
[root@master ~]# salt 'node1' service.reload nginx
node1:
True
service.stop
停止指定服务
[root@master ~]# salt 'node1' service.stop nginx
node1:
True
// 查看node1主机的nginx服务是否停止
[root@node1 ~]# 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 Thu 2021-11-04 01:04:11 CST; 19s ago
Process: 2101 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCES>
Main PID: 1036 (code=exited, status=0/SUCCESS)
11月 03 22:04:22 node1 systemd[1]: Starting The nginx HTTP and reverse proxy serve>
11月 03 22:04:23 node1 nginx[988]: nginx: the configuration file /etc/nginx/nginx.>
11月 03 22:04:23 node1 nginx[988]: nginx: configuration file /etc/nginx/nginx.conf>
11月 03 22:04:24 node1 systemd[1]: Started The nginx HTTP and reverse proxy server.
11月 04 01:03:10 node1.example.com systemd[1]: Reloading The nginx HTTP and revers>
11月 04 01:03:10 node1.example.com systemd[1]: Reloaded The nginx HTTP and reverse>
11月 04 01:04:11 node1.example.com systemd[1]: Stopping The nginx HTTP and reverse>
11月 04 01:04:11 node1.example.com systemd[1]: Stopped The nginx HTTP and reverse >
service.start
启动指定服务
[root@master ~]# salt 'node1' service.start nginx
node1:
True
// 查看node1主机的nginx服务是否停止
[root@node1 ~]# 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 Thu 2021-11-04 01:06:26 CST; 28s ago
Process: 2101 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCES>
Process: 2128 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 2126 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 2125 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/>
Main PID: 2130 (nginx)
Tasks: 2 (limit: 11300)
Memory: 4.1M
CGroup: /system.slice/nginx.service
├─2130 nginx: master process /usr/sbin/nginx
└─2131 nginx: worker process
11月 04 01:06:26 node1.example.com systemd[1]: Starting The nginx HTTP and reverse>
11月 04 01:06:26 node1.example.com nginx[2126]: nginx: the configuration file /etc>
11月 04 01:06:26 node1.example.com nginx[2126]: nginx: configuration file /etc/ngi>
11月 04 01:06:26 node1.example.com systemd[1]: nginx.service: Failed to parse PID >
11月 04 01:06:26 node1.example.com systemd[1]: Started The nginx HTTP and reverse >
service.restart
重启指定服务
[root@master ~]# salt 'node1' service.restart nginx
node1:
True
service.status
查看指定服务的状态
// nginx服务开启状态查看的时候返回True
[root@master ~]# salt 'node1' service.status nginx
node1:
True
// 关闭nginx服务查看状态返回Flase
[root@node1 ~]# systemctl stop nginx
[root@master ~]# salt 'node1' service.status nginx
node1:
False
service.missing
查看指定服务是否存在
[root@master ~]# salt 'node1' service.missing nginx
node1:
False
SaltStack常用模块之pkg
pkg.download
只下载软件包但不安装
此功能将会下载指定的软件包,但是需要在minion端安装yum-utils,可以使用 cmd.run 进行远程安装
// 在node1主机上安装wget
[root@master ~]# salt 'node1' pkg.download wget
node1:
----------
wget:
/var/cache/yum/packages/wget-1.19.5-10.el8.x86_64.rpm
// 查看下载情况
[root@node1 ~]# cd /var/cache/yum/packages/
[root@node1 packages]# ls
wget-1.19.5-10.el8.x86_64.rpm
pkg.file_list
列出指定包或系统中已安装的所有包的文件
[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
- /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
略.......
//当不提供参数时,将会列出当前系统中所有已安装软件的文件列表
[root@master ~]# salt '*' pkg.file_list
node1:
----------
errors:
files:
VALUE_TRIMMED
master:
----------
errors:
files:
VALUE_TRIMMED
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
pkg.group_list
列出系统中所有的包组
[root@master ~]# salt 'node1' pkg.group_list
node1:
----------
available:
- Conflicts BaseOS
- Dial-up Networking Support
- Hardware Monitoring Utilities
- Hardware Support
- Large Systems Performance
- Legacy UNIX Compatibility
- Python Web
- Server product core
- Windows File Server
- Additional Development
- Anaconda tools
- Backup Client
- Base
- base-x
- Conflicts AppStream
- Container Management
- Debugging Tools
- Desktop Debugging and Performance Tools
- Development Tools
- .NET Core Development
- File and Storage Server
- Fonts
- FTP Server
- GNOME Applications
- GNOME
- Graphical Administration Tools
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Headless Management
- Infiniband Support
- Input Methods
- Internet Applications
- Internet Browser
- Java Platform
- Legacy X Window System Compatibility
- Mail Server
- Mainframe Access
- Multimedia
- Network File System Client
- Network Servers
- Networking Tools
- Common NetworkManager submodules
- Office Suite and Productivity
- Atomic Host ostree support
- Performance Tools
- Platform Development
- KVM platform specific packages
- Hyper-v platform specific packages
- Printing Client
- Remote Desktop Clients
- Remote Management for Linux
- RPM Development Tools
- Scientific Support
- Security Tools
- Smart Card Support
- Standard
- System Tools
- TeX formatting system
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Basic Web Server
- Workstation product core
available environments:
- Server with GUI
- Server
- Workstation
- Custom Operating System
- Virtualization Host
available languages:
----------
installed:
- Core
- VMware platform specific packages
installed environments:
- Minimal Install
pkg.install
安装软件
[root@master ~]# salt 'node1' pkg.install tree
node1:
----------
tree:
----------
new:
1.7.0-15.el8
old:
// 查看安装情况
[root@node1 ~]# rpm -qa | grep tree
tree-1.7.0-15.el8.x86_64
pkg.list_downloaded
列出已下载到本地的软件包
[root@master ~]# salt 'node1' pkg.list_downloaded
node1:
----------
wget:
----------
1.14-18.el7:
----------
creation_date_time:
2021-11-03T13:11:53
creation_date_time_t:
1550826712
path:
/var/cache/yum/packages/wget-1.14-18.el7.x86_64.rpm
size:
560152
pkg.list_pkgs
以字典的方式列出当前已安装的软件包
[root@master ~]# salt 'node1' pkg.list_pkgs
node1:
----------
NetworkManager:
1:1.22.8-4.el8
NetworkManager-libnm:
1:1.22.8-4.el8
NetworkManager-team:
1:1.22.8-4.el8
NetworkManager-tui:
1:1.22.8-4.el8
acl:
2.2.53-1.el8
apr:
1.6.3-11.el8
apr-util:
1.6.1-6.el8
apr-util-bdb:
1.6.1-6.el8
apr-util-openssl:
1.6.1-6.el8
audit:
3.0-0.17.20191104git1c2f876.el8
audit-libs:
3.0-0.17.20191104git1c2f876.el8
authselect:
1.1-2.el8
authselect-libs:
1.1-2.el8
basesystem:
11-5.el8
略......
pkg.owner
列出指定文件是由哪个包提供的
[root@master ~]# salt 'node1' pkg.owner /usr/sbin/apachectl
node1:
httpd
[root@master ~]# salt 'node1' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
node1:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
pkg.remove
卸载指定软件
// 查看node1主机是否下载tree
[root@node1 ~]# rpm -qa | grep tree
tree-1.7.0-15.el8.x86_64
// 使用万能模块cmd.run过滤tree
[root@master ~]# salt 'node1' cmd.run 'rpm -qa | grep tree'
node1:
tree-1.7.0-15.el8.x86_64
// 开始删除
[root@master ~]# salt 'node1' pkg.remove tree
node1:
----------
tree:
----------
new:
old:
1.7.0-15.el8
// 删除成功
[root@node1 ~]# rpm -qa | grep tree
// 若要卸载多个文件,中间需要用逗号隔开
pkg.upgrade
升级系统中所有的软件包或升级指定的软件包
[root@master ~]# salt 'node1' pkg.upgrade name=openssl
node1:
----------
openssl:
----------
new:
1:1.1.1g-15.el8_3
old:
1:1.1.1c-15.el8
openssl-libs:
----------
new:
1:1.1.1g-15.el8_3
old:
1:1.1.1c-15.el8
// 若想升级系统中所有的软件包则把 name 参数去掉即可
pkg.group_install
安装包组
[root@master ~]# salt 'node1' pkg.group_install 'Development Tools'
node1:
----------
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:
9.50-1.el8
old:
asciidoc:
----------
new:
8.6.10-0.5.20180627gitf7c2274.el8
old:
atk:
----------
new:
2.28.1-1.el8
old:
autoconf:
----------
new:
2.69-27.el8
old:
automake:
----------
new:
1.16.1-7.el8
old:
avahi-libs:
----------
new:
0.7-20.el8
old:
binutils:
----------
new:
2.30-93.el8
old:
bison:
----------
new:
3.0.4-10.el8
old:
boost-atomic:
----------
new:
1.66.0-10.el8
old:
boost-chrono:
----------
new:
1.66.0-10.el8
old:
boost-date-time:
----------
new:
1.66.0-10.el8
old:
boost-filesystem:
----------
new:
1.66.0-10.el8
old:
boost-regex:
----------
new:
1.66.0-10.el8
old:
boost-system:
----------
new:
1.66.0-10.el8
old:
SaltStack常用模块之state
state.show_highstate
显示当前系统中有哪些高级状态
[root@master ~]# salt 'node1' state.show_highstate
node1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.apache
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10002
apache-service:
----------
__env__:
base
__sls__:
web.apache.apache
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10003
nginx-install:
----------
__env__:
base
__sls__:
web.nginx.nginx
pkg:
|_
----------
name:
nginx
- installed
|_
----------
order:
10000
nginx-service:
----------
__env__:
base
__sls__:
web.nginx.nginx
service:
|_
----------
name:
nginx
|_
----------
enable:
True
- running
|_
----------
order:
10001
state.highstate
执行高级状态
pillar自定义数据:
在master的配置文件里找pillar_roots可以看到其存放pillar的位置
pillar自定义数据:
在master的配置文件里找pillar_roots可以看到其存放pillar的位置
[root@master ~]# vim /etc/salt/master
# Pillar is laid out in the same fashion as the file server, with environments,
# a top file and sls files. However, pillar data does not need to be in the
# highstate format, and is generally just key/value pairs.
pillar_roots: // 取消这三行注释
base:
- /srv/pillar/base
#
#ext_pillar:
# - hiera: /etc/hiera.yaml
# - cmd_yaml: cat /etc/salt/yaml
[root@master ~]# systemctl restart salt-master
[root@master ~]# mkdir -p /srv/pillar/base
[root@master ~]# vim /srv/pillar/base/test.sls
{% if grains['os'] == 'RedHat' %}
test: httpd
{% elif grains['os'] == 'CentOS' %}
test: nginx
{% endif %}
[root@master ~]# vim /srv/pillar/base/top.sls
base: // 指定环境
'node1': // 指定目标
- test
[root@master ~]# vim /etc/salt/master
#pillar_opts: True // 这一行重新注释
[root@master ~]# salt '*' pillar.items
master:
----------
node1:
----------
test:
httpd
// 在salt下修改nginx的状态文件,引用pillar的数据
[root@master ~]# vim /srv/salt/base/web/apache/apache.sls
apache-install:
pkg.installed:
- name: "{{ pillar['test'] }}"
apache-service:
service.running:
- name: "{{ pillar['test'] }}"
- enable: True
// 高级状态
[root@master ~]# salt 'node1' state.highstate web.apache.apache
node1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 02:03:23.868897
Duration: 1064.547 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: None
Comment: Service nginx is set to start
Started: 02:03:24.938348
Duration: 56.676 ms
Changes:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 02:03:24.995305
Duration: 30.289 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd is set to start
Started: 02:03:25.025926
Duration: 49.064 ms
Changes:
Summary for node1
------------
Succeeded: 4 (unchanged=2)
Failed: 0
------------
Total states run: 4
Total run time: 1.201 s
state.show_state_usage
显示当前系统中的高级状态执行情况
[root@master ~]# salt 'node1' state.show_state_usage
node1:
----------
base:
----------
count_all:
3
count_unused:
1
count_used:
2
unused:
- top
used:
- web.apache.apache
- web.nginx.nginx
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:
state.show_top
返回minion将用于highstate的顶级数据
[root@master ~]# salt 'node1' state.show_top
node1:
----------
base:
- web.nginx.nginx
- web.apache.apache
state.top
执行指定的top file
// 在top file里面使用Grains
[root@master ~]# vim /srv/salt/base/amu.sls
base:
'os:RedHat':
- match: grain
- web.apache.apache
// 执行指定的top file,而不是默认的
[root@master ~]# salt "node1" state.top amu.sls
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 02:09:19.077445
Duration: 1068.419 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 02:09:20.148980
Duration: 775.653 ms
Changes:
----------
httpd:
True
Summary for node1
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 1.844 s
state.show_sls
显示 master 上特定sls或sls文件列表中的状态数据
[root@master ~]# salt 'node1' state.show_sls web.apache.apache
node1:
----------
apache-install:
----------
__env__:
base
__sls__:
web.apache.apache
pkg:
|_
----------
name:
httpd
- installed
|_
----------
order:
10000
apache-service:
----------
__env__:
base
__sls__:
web.apache.apache
service:
|_
----------
name:
httpd
|_
----------
enable:
True
- running
|_
----------
order:
10001
SaltStack常用模块之user
user.add
创建用户
[root@master ~]# id bzm
id: ‘bzm’: no such user
[root@master ~]# salt "master" user.add bzm
master:
True
[root@master ~]# id bzm
uid=1001(bzm) gid=1001(bzm) groups=1001(bzm)
user.chfullname
更改用户完整名称(用户信息进行描述)
[root@master ~]# salt "master" user.chfullname bzm "bzm Bar"
master:
True
[root@master ~]# cat /etc/passwd | tail -1
bzm:x:1001:1001:bzm Bar:/home/bzm:/bin/bash
user.chuid
更改用户UID
[root@master ~]# id bzm
uid=1001(bzm) gid=1001(bzm) groups=1001(bzm)
[root@master ~]# salt 'master' user.chuid bzm 2000
master:
True
[root@master ~]# id bzm
uid=2000(bzm) gid=1001(bzm) groups=1001(bzm)
user.chgid
更改用户GID
[root@master ~]# userdel -r xc
[root@master ~]# salt 'master' user.add xc
master:
True
// 更改gid,用户组得先存在
[root@master ~]# id xc
uid=1002(xc) gid=1002(xc) groups=1002(xc)
// 把amu的1001gid更改为1002
[root@master ~]# salt 'master' user.chgid bzm 1002
master:
True
[root@master ~]# id bzm
uid=1001(bzm) gid=1002(xc) groups=1002(xc)
user.info
返回用户信息
[root@master ~]# salt 'master' user.info bzm
master:
----------
fullname:
gid:
1002
groups:
- xc
home:
/home/bzm
homephone:
name:
bzm
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1001
workphone:
user.getent
返回所有系统用户信息的列表。
[root@master ~]# salt 'master' user.getent
master:
|_
----------
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
uid:
11
workphone:
|_
----------
fullname:
games
gid:
100
groups:
- users
home:
/usr/games
homephone:
name:
games
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
12
workphone:
|_
----------
fullname:
FTP User
gid:
50
groups:
- ftp
home:
/var/ftp
homephone:
name:
ftp
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
14
workphone:
|_
----------
fullname:
Kernel Overflow User
gid:
65534
groups:
- nobody
home:
/
homephone:
name:
nobody
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
65534
workphone:
|_
----------
fullname:
System message bus
gid:
81
groups:
- dbus
home:
/
homephone:
name:
dbus
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
81
workphone:
|_
----------
fullname:
systemd Core Dumper
gid:
997
groups:
- systemd-coredump
home:
/
homephone:
name:
systemd-coredump
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
999
workphone:
|_
----------
fullname:
systemd Resolver
gid:
193
groups:
- systemd-resolve
home:
/
homephone:
name:
systemd-resolve
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
193
workphone:
|_
----------
fullname:
Account used by the trousers package to sandbox the tcsd daemon
gid:
59
groups:
- tss
home:
/dev/null
homephone:
name:
tss
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
59
workphone:
|_
----------
fullname:
User for polkitd
gid:
996
groups:
- polkitd
home:
/
homephone:
name:
polkitd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
998
workphone:
|_
----------
fullname:
Unbound DNS resolver
gid:
995
groups:
- unbound
home:
/etc/unbound
homephone:
name:
unbound
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
997
workphone:
|_
----------
fullname:
User for sssd
gid:
993
groups:
- sssd
home:
/
homephone:
name:
sssd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
996
workphone:
|_
----------
fullname:
Privilege-separated SSH
gid:
74
groups:
- sshd
home:
/var/empty/sshd
homephone:
name:
sshd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
74
workphone:
|_
----------
fullname:
Random Number Generator Daemon
gid:
992
groups:
- rngd
home:
/var/lib/rngd
homephone:
name:
rngd
other:
passwd:
x
roomnumber:
shell:
/sbin/nologin
uid:
995
workphone:
|_
----------
fullname:
gid:
1000
groups:
- amu
home:
/home/amu
homephone:
name:
amu
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1000
workphone:
|_
----------
fullname:
gid:
1002
groups:
- xc
home:
/home/bzm
homephone:
name:
bzm
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1001
workphone:
|_
----------
fullname:
gid:
1002
groups:
- xc
home:
/home/xc
homephone:
name:
xc
other:
passwd:
x
roomnumber:
shell:
/bin/bash
uid:
1002
workphone:
user.list_groups
列出指定用户所属组的列表。
[root@master ~]# salt 'master' user.list_groups bzm
master:
- xc
user.list_users
返回所有用户的列表
[root@master ~]# salt 'master' user.list_users
master:
- adm
- amu
- bin
- bzm
- daemon
- dbus
- ftp
- games
- halt
- lp
- mail
- nobody
- operator
- polkitd
- rngd
- root
- shutdown
- sshd
- sssd
- sync
- systemd-coredump
- systemd-resolve
- tss
- unbound
- xc
user.rename
修改指定用户的用户名。
[root@master ~]# id bzm
uid=1001(bzm) gid=1002(xc) groups=1002(xc)
[root@master ~]# salt 'master' user.rename bzm bzm123
master:
False
[root@master ~]# id bzm
id: ‘bzm’: no such user
[root@master ~]# id bzm123
uid=1001(bzm123) gid=1002(xc) groups=1002(xc)
[root@master ~]# cat /etc/passwd | tail -2
xc:x:1002:1002::/home/xc:/bin/bash
bzm123:x:1001:1002::/home/bzm:/bin/bash
// 虽然返回False但是操作是成功完成了
user.delete
删除用户。
[root@master ~]# id bzm123
uid=1001(bzm123) gid=1002(xc) groups=1002(xc)
[root@master ~]# salt 'master' user.delete bzm123
master:
True
[root@master ~]# id bzm123
id: ‘bzm123’: no such user
[root@master ~]# cat /etc/passwd | tail -2
rngd:x:995:992:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
xc:x:1002:1002::/home/xc:/bin/bash
// bzm123用户已被删除
SaltStack常用模块之salt-cp
salt-cp能够很方便的把 master 上的文件批量传到 minion上
//拷贝单个文件到目标主机的/usr/src目录下
[root@master ~]# salt '*' cmd.run 'ls /usr/src/'
master:
debug
kernels
node2:
debug
kernels
node1:
debug
kernels
[root@master ~]# salt-cp '*' /etc/passwd /usr/src/
master:
----------
/usr/src/passwd:
True
node1:
----------
/usr/src/passwd:
True
node2:
----------
/usr/src/passwd:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
debug
kernels
passwd
node2:
debug
kernels
passwd
master:
debug
kernels
passwd
//拷贝多个文件到目标主机的/usr/src目录下
[root@master ~]# salt-cp '*' /etc/shadow /etc/group /usr/src
master:
----------
/usr/src/group:
True
/usr/src/shadow:
True
node1:
----------
/usr/src/group:
True
/usr/src/shadow:
True
node2:
----------
/usr/src/group:
True
/usr/src/shadow:
True
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
debug
group
kernels
passwd
shadow
node2:
debug
group
kernels
passwd
shadow
master:
debug
group
kernels
passwd
shadow
[root@master ~]# salt '*' cmd.run 'ls /usr/src -l'
node1:
total 12
drwxr-xr-x. 2 root root 6 Aug 12 2018 debug
-rw-r--r-- 1 root root 522 Nov 4 15:11 group
drwxr-xr-x. 3 root root 42 Nov 4 01:51 kernels
-rw-r--r-- 1 root root 1141 Nov 4 15:09 passwd
-rw-r--r-- 1 root root 679 Nov 4 15:11 shadow
node2:
total 12
drwxr-xr-x. 2 root root 6 Aug 12 2018 debug
-rw-r--r-- 1 root root 522 Nov 4 23:11 group
drwxr-xr-x. 2 root root 6 Aug 12 2018 kernels
-rw-r--r-- 1 root root 1141 Nov 4 23:09 passwd
-rw-r--r-- 1 root root 679 Nov 4 23:11 shadow
master:
total 12
drwxr-xr-x. 2 root root 6 Aug 12 2018 debug
-rw-r--r-- 1 root root 522 Nov 4 15:11 group
drwxr-xr-x. 2 root root 6 Aug 12 2018 kernels
-rw-r--r-- 1 root root 1141 Nov 4 15:09 passwd
-rw-r--r-- 1 root root 679 Nov 4 15:11 shadow
SaltStack常用模块之file
file.access
检查指定路径是否存在
[root@master ~]# salt '*' cmd.run 'ls /usr/src'
node1:
debug
group
kernels
passwd
shadow
node2:
debug
group
kernels
passwd
shadow
master:
debug
group
kernels
passwd
shadow
// 文件存在
[root@master ~]# salt '*' file.access /usr/src/passwd f
node2:
True
node1:
True
master:
True
// 文件不存在
[root@master ~]# salt '*' file.access /usr/src/abc f
node1:
False
node2:
False
master:
False
检查指定文件的权限信息
[root@master ~]# salt 'node1' cmd.run 'ls -l /usr/src'
node1:
total 12
drwxr-xr-x. 2 root root 6 Aug 12 2018 debug
-rw-r--r-- 1 root root 522 Nov 4 15:11 group
drwxr-xr-x. 3 root root 42 Nov 4 01:51 kernels
-rw-r--r-- 1 root root 1141 Nov 4 15:09 passwd
-rw-r--r-- 1 root root 679 Nov 4 15:11 shadow
//是否有读权限
[root@master ~]# salt 'node1' file.access /usr/src/passwd r
node1:
True
//是否有写权限
[root@master ~]# salt 'node1' file.access /usr/src/passwd w
node1:
True
//是否有执行权限
[root@master ~]# salt 'node1' file.access /usr/src/passwd x
node1:
False
file.append
往一个文件里追加内容,若此文件不存在则会报异常
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/bzm'
node1:
ls: cannot access '/root/bzm': No such file or directory
ERROR: Minions returned with non-zero exit code
// 创建文件
[root@master ~]# salt 'node1' cmd.run 'touch bzm'
node1:
[root@master ~]# salt 'node1' cmd.run 'ls /root'
node1:
anaconda-ks.cfg
bzm
// 文件存在,即可查看
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/bzm'
node1:
-rw-r--r-- 1 root root 0 Nov 4 16:39 /root/bzm
// 给文件添加内容
[root@master ~]# salt 'node1' file.append /root/bzm "harry" "hehe"
node1:
Wrote 2 lines to "/root/bzm"
// 查看添加效果
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/bzm'
node1:
-rw-r--r-- 1 root root 11 Nov 4 16:43 /root/bzm
[root@master ~]# salt 'node1' cmd.run 'cat /root/bzm'
node1:
harry
hehe
file.basename
获取指定路径的基名
[root@master ~]# salt 'node1' file.basename '/root/zabbix/abc'
node1:
abc
file.dirname
获取指定路径的目录名
[root@master ~]# salt 'node1' file.dirname '/root/zabbix/abc'
node1:
/root/zabbix
file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False
[root@master ~]# salt 'node1' cmd.run 'md5sum /etc/passwd'
node1:
87e65be9ac6a0137331fb08b15809bcd /etc/passwd
[root@master ~]# salt 'node1' file.check_hash /etc/passwd 87e65be9ac6a0137331fb08b15809bcd
node1:
True
file.chattr
修改指定文件的属性
属性 | 对文件的意义 | 对目录的意义 |
---|---|---|
a | 只允许在这个文件之后追加数据,不允许任何进程覆盖或截断这个文件 | 只允许在这个目录下建立和修改文件,而不允许删除任何文件 |
i | 不允许对这个文件进行任何的修改,不能删除、更改、移动 | 任何的进程只能修改目录之下的文件,不允许建立和删除文件 |
给指定文件添加属性
//查看当前属性
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
-------------------- /root/bzm
//添加属性
[root@master ~]# salt 'node1' file.chattr /root/bzm operator=add attributes=ai
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
----ia-------------- /root/bzm
给指定文件去除属性
[root@master ~]# salt 'node1' file.chattr /root/bzm operator=remove attributes=i
node1:
True
[root@master ~]# salt 'node1' cmd.run 'lsattr /root'
node1:
-------------------- /root/anaconda-ks.cfg
-----a-------------- /root/bzm
file.chown
设置指定文件的属主、属组信息
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 8
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
// 修改权限
[root@master ~]# salt 'node1' file.chown /root/bzm bzm bzm
node1:
None
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 8
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 bzm bzm 11 Nov 4 16:43 bzm
file.copy
在远程主机上复制文件或目录
拷贝文件
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 8
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 bzm bzm 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.copy /root/bzm /root/abc
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 bzm bzm 11 Nov 4 17:15 abc
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 bzm bzm 11 Nov 4 16:43 bzm
覆盖并拷贝目录,将会覆盖同名文件或目录
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 bzm bzm 11 Nov 4 17:15 abc
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 bzm bzm 11 Nov 4 16:43 bzm
// 拷贝tmp目录
[root@master ~]# salt 'node1' file.copy /tmp/ /root/abd recurse=True
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 bzm bzm 11 Nov 4 17:15 abc
drwxrwxrwt 12 root root 295 Nov 4 17:19 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
删除目标目录中同名的文件或目录并拷贝新内容至其中
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abd'
node1:
total 0
drwxr-xr-x 2 root root 18 Nov 4 17:19 hsperfdata_root
drwxr-xr-x 3 root root 17 Nov 4 17:19 systemd-private-eaadcce61bc243bc9bfe94487812705c-httpd.service-ZqYO4E
drwxr-xr-x 2 root root 6 Nov 4 17:19 vmware-root_959-3979643072
drwxr-xr-x 2 root root 6 Nov 4 17:19 vmware-root_968-2965448017
drwxr-xr-x 2 root root 6 Nov 4 17:19 vmware-root_976-2966103375
[root@master ~]# salt 'node1' cmd.run 'ls -l /tmp/'
node1:
total 0
drwxr-xr-x 2 root root 18 Nov 4 01:50 hsperfdata_root
drwx------ 3 root root 17 Nov 4 02:09 systemd-private-eaadcce61bc243bc9bfe94487812705c-httpd.service-ZqYO4E
drwx------ 2 root root 6 Nov 3 22:04 vmware-root_959-3979643072
drwx------ 2 root root 6 Nov 2 01:53 vmware-root_968-2965448017
drwx------ 2 root root 6 Nov 2 01:35 vmware-root_976-2966103375
[root@master ~]# salt 'node1' file.copy /tmp/ /root/abd recurse=True remove_existing=True
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/abd'
node1:
total 0
drwxr-xr-x 2 root root 18 Nov 4 01:50 hsperfdata_root
drwx------ 3 root root 17 Nov 4 02:09 systemd-private-eaadcce61bc243bc9bfe94487812705c-httpd.service-ZqYO4E
drwx------ 2 root root 6 Nov 3 22:04 vmware-root_959-3979643072
drwx------ 2 root root 6 Nov 2 01:53 vmware-root_968-2965448017
drwx------ 2 root root 6 Nov 2 01:35 vmware-root_976-2966103375
file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False
// 查看指定目录下有哪些目录文件
[root@master ~]# salt 'node1' cmd.run 'ls -l /tmp'
node1:
total 0
drwxr-xr-x 2 root root 18 Nov 4 01:50 hsperfdata_root
drwx------ 3 root root 17 Nov 4 02:09 systemd-private-eaadcce61bc243bc9bfe94487812705c-httpd.service-ZqYO4E
drwx------ 2 root root 6 Nov 3 22:04 vmware-root_959-3979643072
drwx------ 2 root root 6 Nov 2 01:53 vmware-root_968-2965448017
drwx------ 2 root root 6 Nov 2 01:35 vmware-root_976-2966103375
[root@master ~]# salt 'node1' file.directory_exists /tmp/vmware-root_959-3979643072
node1:
True
file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回
[root@master ~]# salt '*' cmd.run 'du -sb /opt'
node1:
6 /opt
node2:
6 /opt
[root@master ~]# salt '*' file.diskusage /opt
node1:
0
node2:
0
file.file_exists
判断指定文件是否存在
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 16
-rw-r--r-- 1 bzm bzm 11 Nov 4 17:15 abc
drwxrwxrwt. 12 root root 4096 Nov 4 15:54 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.file_exists /root/bzm
node1:
True
//返回False是因为abc是目录而非文件
[root@master ~]# salt 'node1' file.file_exists /root/abd
node1:
False
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]
The default action is print=path
path-glob:
* = match zero or more chars
? = match any char
[abc] = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y] = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c} = match a or b or c
path-regex
: a Python Regex (regular expression) pattern to match pathnames
file-types
: a string of one or more of the following:
a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket
users
: a space and/or comma separated list of user names and/or uids
groups
: a space and/or comma separated list of group names and/or gids
size-unit
:
b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes
interval:
[<num>w] [<num>d] [<num>h] [<num>m] [<num>s]
where:
w: week
d: day
h: hour
m: minute
s: second
print-opts: a comma and/or space separated list of one or more of the following:
group: group name
md5: MD5 digest of file contents
mode: file permissions (as integer)
mtime: last modification time (as time_t)
name: file basename
path: file absolute path
size: file size in bytes
type: file type
user: user name
示例:
salt '*' file.find / type=f name=\*.bak size=+10m
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime
salt '*' file.find /var/log name=\*.[0-9] mtime=+30d size=+10m delete
file.get_gid
获取指定文件的gid
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/bzm'
node1:
-rw-r--r-- 1 root root 11 Nov 4 16:43 /root/bzm
[root@master ~]# salt 'node1' file.get_gid /root/bzm
node1:
0
file.get_group
获取指定文件的组名
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/bzm'
node1:
-rw-r--r-- 1 root root 11 Nov 4 16:43 /root/bzm
[root@master ~]# salt 'node1' file.get_group /root/bzm
node1:
root
file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来
[root@master ~]# salt 'node1' cmd.run 'sha256sum /root/bzm'
node1:
544a0d0e8acfb13cea980e3f976afdb64941d4bcf09ef53b0c0e0eba4764b148 /root/bzm
[root@master ~]# salt 'node1' file.get_hash '/root/bzm'
node1:
544a0d0e8acfb13cea980e3f976afdb64941d4bcf09ef53b0c0e0eba4764b148
file.get_mode
获取指定文件的权限,以数字方式显示
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/bzm'
node1:
-rw-r--r-- 1 root root 11 Nov 4 16:43 /root/bzm
[root@master ~]# salt 'node1' file.get_mode /root/bzm
node1:
0644
file.get_selinux_context
获取指定文件的 SELINUX 上下文信息
[root@master ~]# salt 'node1' cmd.run 'ls -Z /root/anaconda-ks.cfg'
node1:
system_u:object_r:admin_home_t:s0 /root/anaconda-ks.cfg
[root@master ~]# salt 'node1' file.get_selinux_context /root/anaconda-ks.cfg
node1:
system_u:object_r:admin_home_t:s0
file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
// 查看sha256算法的值
[root@master ~]# salt 'node1' cmd.run 'sha256sum /root/bzm'
node1:
544a0d0e8acfb13cea980e3f976afdb64941d4bcf09ef53b0c0e0eba4764b148 /root/bzm
[root@master ~]# salt 'node1' file.get_sum /root/bzm
node1:
544a0d0e8acfb13cea980e3f976afdb64941d4bcf09ef53b0c0e0eba4764b148
// 查看md5算法的值
[root@master ~]# salt 'node1' cmd.run 'md5sum /root/bzm'
node1:
286fe4b542e90278e65617973642b232 /root/bzm
[root@master ~]# salt 'node1' file.get_sum /root/bzm md5
node1:
286fe4b542e90278e65617973642b232
file.get_uid与file.get_user
获取指定文件的 uid 或 用户名
// 查看文件属性
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/bzm'
node1:
-rw-r--r-- 1 root root 11 Nov 4 16:43 /root/bzm
// 查看文件UID
[root@master ~]# salt 'node1' file.get_uid /root/bzm
node1:
0
// 查看文件用户名
[root@master ~]# salt 'node1' file.get_user /root/bzm
node1:
root
file.gid_to_group
将指定的 gid 转换为组名并显示
[root@master ~]# salt 'node1' file.gid_to_group 1000
node1:
bzm
[root@master ~]# salt 'node1' file.gid_to_group 0
node1:
root
file.group_to_gid
将指定的组名转换为 gid 并显示
[root@master ~]# salt 'node1' file.group_to_gid root
node1:
0
[root@master ~]# salt 'node1' file.group_to_gid bzm
node1:
1000
file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i -B2
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
[root@master ~]# salt 'node1' file.grep /etc/passwd nobody
node1:
----------
pid:
25791
retcode:
0
stderr:
stdout:
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i
node1:
----------
pid:
25796
retcode:
0
stderr:
stdout:
IPADDR=192.168.111.142
// 查看ipaddr前两行
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -B2
node1:
----------
pid:
25802
retcode:
0
stderr:
stdout:
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.111.142
// 查看ipaddr后两行
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -A2
node1:
----------
pid:
25807
retcode:
0
stderr:
stdout:
IPADDR=192.168.111.142
PREFIX=24
GATEWAY=192.168.111.2
// 查看ipaddr前后两行
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -C2
node1:
----------
pid:
25812
retcode:
0
stderr:
stdout:
DEVICE=ens160
ONBOOT=yes
IPADDR=192.168.111.142
PREFIX=24
GATEWAY=192.168.111.2
[root@master ~]# salt 'node1' file.grep /etc/sysconfig/network-scripts/ifcfg-ens160 ipaddr -- -i -l
node1:
----------
pid:
25818
retcode:
0
stderr:
stdout:
/etc/sysconfig/network-scripts/ifcfg-ens160
file.is_blkdev
判断指定的文件是否是块设备文件
[root@master ~]# salt 'node1' cmd.run 'ls -l /dev/sr0'
node1:
brw-rw---- 1 root cdrom 11, 0 Nov 3 22:04 /dev/sr0
[root@master ~]# salt 'node1' file.is_blkdev /dev/sr0
node1:
True
file.lsattr
检查并显示出指定文件的属性信息
[root@master ~]# salt 'node1' cmd.run 'lsattr /root/bzm'
node1:
-------------------- /root/bzm
[root@master ~]# salt 'node1' cmd.run 'chattr +i /root/bzm'
node1:
[root@master ~]# salt 'node1' cmd.run 'lsattr /root/bzm'
node1:
----i--------------- /root/bzm
[root@master ~]# salt 'node1' file.lsattr /root/bzm
node1:
----------
/root/bzm:
- i
file.mkdir
创建目录并设置属主、属组及权限
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 16
-rw-r--r-- 1 bzm bzm 11 Nov 4 17:15 abc
drwxrwxrwt. 12 root root 4096 Nov 4 15:54 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.mkdir /root/aaa
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 16
drwxr-xr-x 2 root root 6 Nov 4 18:10 aaa
-rw-r--r-- 1 bzm bzm 11 Nov 4 17:15 abc
drwxrwxrwt. 12 root root 4096 Nov 4 15:54 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.mkdir /root/hehe tom tom 400
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 16
drwxr-xr-x 2 root root 6 Nov 4 18:10 aaa
-rw-r--r-- 1 bzm bzm 11 Nov 4 17:15 abc
drwxrwxrwt. 12 root root 4096 Nov 4 15:54 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
dr-------- 2 root root 6 Nov 4 18:11 hehe
file.move
移动或重命名
//重命名
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 20
-rw-r--r-- 1 bzm bzm 28 Nov 5 17:05 abc
-rw-r--r-- 1 bzm bzm 26 Nov 5 17:04 abc.bak
drwxrwxrwt. 12 root root 4096 Nov 4 15:54 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
dr-------- 2 root root 6 Nov 4 18:11 hehe
[root@master ~]# salt 'node1' file.move /root/hehe /root/haha
node1:
----------
comment:
'/root/hehe' moved to '/root/haha'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 20
-rw-r--r-- 1 bzm bzm 28 Nov 5 17:05 abc
-rw-r--r-- 1 bzm bzm 26 Nov 5 17:04 abc.bak
drwxrwxrwt. 12 root root 4096 Nov 4 15:54 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
dr-------- 2 root root 6 Nov 4 18:11 haha
//移动
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 20
-rw-r--r-- 1 bzm bzm 28 Nov 5 17:05 abc
-rw-r--r-- 1 bzm bzm 26 Nov 5 17:04 abc.bak
drwxrwxrwt. 12 root root 4096 Nov 4 15:54 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
dr-------- 2 root root 6 Nov 4 18:11 haha
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 4
drwxr-xr-x 2 root root 6 Nov 4 18:10 bbb
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.move /root/haha /opt/
node1:
----------
comment:
'/root/haha' moved to '/opt/'
result:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 4
drwxr-xr-x 2 root root 6 Nov 4 18:10 bbb
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
dr-------- 2 root root 6 Nov 4 18:11 haha
file.prepend
把文本插入指定文件的开头
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
xixi
hello world
harry
hehe
[root@master ~]# salt 'node1' file.prepend /root/abc "zzz" "passwd" "runtime"
node1:
Prepended 3 lines to "/root/abc"
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
zzz
passwd
runtime
xixi
hello world
harry
hehe
file.sed
修改文本文件的内容
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
zzz
passwd
runtime
xixi
hello world
harry
hehe
[root@master ~]# salt 'node1' file.sed /root/abc 'hello' 'runtime'
node1:
----------
pid:
1993
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
zzz
passwd
runtime
xixi
runtime world
harry
hehe
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
zzz
passwd
runtime
xixi
runtime world
harry
hehe
[root@master ~]# salt 'node1' file.sed /root/abc 'runtime' 'holle'
node1:
----------
pid:
2004
retcode:
0
stderr:
stdout:
[root@master ~]# salt 'node1' cmd.run 'cat /root/abc'
node1:
zzz
passwd
holle
xixi
holle world
harry
hehe
file.read
读取文件内容
[root@master ~]# salt 'node1' cmd.run 'cat /root/bzm'
node1:
harry
hehe
[root@master ~]# salt 'node1' file.read /root/bzm
node1:
harry
hehe
file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件
[root@master ~]# salt 'node1' file.readdir /root
node1:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .bash_history
- .viminfo
- bzm
- abd
- abc.bak
- abc
file.remove
删除指定的文件或目录,若给出的是目录,将递归删除
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 20
-rw-r--r-- 1 bzm bzm 45 Nov 7 22:44 abc
-rw-r--r-- 1 bzm bzm 49 Nov 7 22:42 abc.bak
drwxrwxrwt. 12 root root 4096 Nov 4 15:54 abd
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.remove /root/abc
node1:
True
[root@master ~]# salt 'node1' file.remove /root/abd
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 12
-rw-r--r-- 1 bzm bzm 49 Nov 7 22:42 abc.bak
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
file.rename
重命名文件或目录
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 12
-rw-r--r-- 1 bzm bzm 49 Nov 7 22:42 abc.bak
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.rename /root/abc.bak /root/abc
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root/'
node1:
total 12
-rw-r--r-- 1 bzm bzm 49 Nov 7 22:42 abc
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
file.set_mode
给指定文件设置权限
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-rw-r--r-- 1 bzm bzm 49 Nov 7 22:42 abc
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.set_mode /root/abc 0400
node1:
0400
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-r-------- 1 bzm bzm 49 Nov 7 22:42 abc
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
file.symlink
给指定的文件创建软链接
[root@master ~]# salt 'node1' cmd.run 'ls -l /root'
node1:
total 12
-r-------- 1 bzm bzm 49 Nov 7 22:42 abc
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
[root@master ~]# salt 'node1' file.symlink /root/bzm /opt/hehe
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /root;ls -l /opt/'
node1:
total 12
-r-------- 1 bzm bzm 49 Nov 7 22:42 abc
-rw-------. 1 root root 1204 Oct 14 17:05 anaconda-ks.cfg
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
total 4
drwxr-xr-x 2 root root 6 Nov 4 18:10 bbb
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
dr-------- 2 root root 6 Nov 4 18:11 haha
lrwxrwxrwx 1 root root 9 Nov 7 23:12 hehe -> /root/bzm
file.touch
创建空文件或更新时间戳
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 4
drwxr-xr-x 2 root root 6 Nov 4 18:10 bbb
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
dr-------- 2 root root 6 Nov 4 18:11 haha
lrwxrwxrwx 1 root root 9 Nov 7 23:12 hehe -> /root/bzm
[root@master ~]# salt 'node1' file.touch /opt/bbb
node1:
True
[root@master ~]# salt 'node1' file.touch /opt/haha
node1:
True
[root@master ~]# salt 'node1' file.touch /opt/a
node1:
True
[root@master ~]# salt 'node1' cmd.run 'ls -l /opt'
node1:
total 4
-rw-r--r-- 1 root root 0 Nov 7 23:20 a
drwxr-xr-x 2 root root 6 Nov 7 23:18 bbb
-rw-r--r-- 1 root root 11 Nov 4 16:43 bzm
dr-------- 2 root root 6 Nov 7 23:19 haha
lrwxrwxrwx 1 root root 9 Nov 7 23:12 hehe -> /root/bzm
file.uid_to_user
将指定的 uid 转换成用户名显示出来
[root@master ~]# salt 'node1' file.uid_to_user 0
node1:
root
[root@master ~]# salt 'node1' file.uid_to_user 1000
node1:
bzm
file.user_to_uid
将指定的用户转换成 uid 并显示出来
[root@master ~]# salt 'node1' file.user_to_uid bzm
node1:
1000
[root@master ~]# salt 'node1' file.user_to_uid root
node1:
0
file.write
往一个指定的文件里覆盖写入指定内容
[root@master ~]# salt 'node1' cmd.run 'cat /root/hehe'
node1:
I'm bzm
xixi
[root@master ~]# salt 'node1' file.write /root/hehe "I'm harry" "hehe"
node1:
Wrote 2 lines to "/root/hehe"
[root@master ~]# salt 'node1' cmd.run 'cat /root/hehe'
node1:
I'm harry
hehe