自動化運維工具SaltStack(3)
1. SaltStack模組介紹
目錄
- 1. SaltStack模組介紹
- 2. SaltStack常用模組
- 2.1 SaltStack常用模組之network
- 2.1.1 network.active_tcp
- 2.1.2 network.connect
- 2.1.3 network.default_route
- 2.1.4 network.get_hostname
- 2.1.5 network.get_route
- 2.1.6 network.hw_addr
- 2.1.7 network.in_subnet
- 2.1.8 network.interface
- 2.1.9 network.interface_ip
- 2.1.10 network.interfaces
- 2.1.11 network.ip_addrs
- 2.1.12 network.netstat
- 2.1.13 network.ping
- 2.2 SaltStack常用模組之service
- 2.3 SaltStack常用模組之pkg
- 2.4 SaltStack常用模組之state
- 2.5 SaltStack常用模組之salt-cp
- 2.6 SaltStack常用模組之file
- 2.6.1 file.access
- 2.6.2 file.append
- 2.6.3 file.basename
- 2.6.4 file.dirname
- 2.6.5 file.check_hash
- 2.6.6 file.chown
- 2.6.7 file.copy
- 2.6.8 file.ditectory_exists
- 2.6.9 file.diskusage
- 2.6.10 file.file_exists
- 2.6.11 file.get_gid
- 2.6.12 file.get_group
- 2.6.13 file.get_hash
- 2.6.14 file.get_mode
- 2.6.15 file.get_selinux_context
- 2.6.16 file.get_sum
- 2.6.17 file.get_uid與file.get_user
- 2.6.18 file.gid_to_group
- 2.6.19 file.group_to_gid
- 2.6.20 file.grep
- 2.6.21 file.is_blkdev
- 2.6.22 file.mkdir
- 2.6.23 file.move
- 2.6.24 file.prepend
- 2.6.25 file.sed
- 2.6.26 file.readdir
- 2.6.27 file.remove
- 2.6.28 file.rename
- 2.6.29 file.set_mode
- 2.6.30 file.symlink
- 2.6.31 file.touch
- 2.6.32 file.uid_to_uid
- 2.6.33 file.user_to_user
- 2.6.34 file.write
- 2.1 SaltStack常用模組之network
關於模組的介紹可以去官方文件檢視
Module是日常使用SaltStack接觸最多的一個元件,其用於管理物件操作,這也是SaltStack通過Push的方式進行管理的入口,比如我們日常簡單的執行命令、檢視包安裝情況、檢視服務執行情況等工作都是通過SaltStack Module來實現的。
當安裝好Master和Minion包後,系統上會安裝很多Module,大家可以通過以下命令檢視支援的所有Module列表:
//檢視所有module列表
[root@node01 ~]# salt 'node02' sys.list_modules
node02:
- acl
- aliases
- alternatives
- archive
- artifactory
- blockdev
- btrfs
- buildout
- cloud
- cmd
- composer
- config
- container_resource
- cp
- cron
- data
- defaults
- devmap
- disk
- django
- dnsmasq
- dnsutil
- drbd
- elasticsearch
- environ
- etcd
- event
- extfs
- file
- firewalld
- gem
- genesis
- gnome
- grains
- group
- hashutil
- hg
- hipchat
- hosts
- http
- img
..........
//檢視指定module的所有function
[root@node01 ~]# salt 'node02' sys.list_functions cmd
node02:
- cmd.exec_code
- cmd.exec_code_all
- cmd.has_exec
- cmd.retcode
- cmd.run
- cmd.run_all
- cmd.run_chroot
- cmd.run_stderr
- cmd.run_stdout
- cmd.script
- cmd.script_retcode
- cmd.shell
- cmd.shells
- cmd.tty
- cmd.which
- cmd.which_bin
//檢視指定module的用法
[root@node01 ~]# salt 'node02' sys.doc file
..........
'file.get_uid:'
Return the id of the user that owns a given file
path
file or directory of which to get the uid
follow_symlinks
indicated if symlinks should be followed
CLI Example:
salt '*' file.get_uid /etc/passwd
Changed in version 0.16.4
``follow_symlinks`` option added
'file.get_user:'
Return the user that owns a given file
path
file or directory of which to get the user
follow_symlinks
indicated if symlinks should be followed
CLI Example:
salt '*' file.get_user /etc/passwd
Changed in version 0.16.4
``follow_symlinks`` option added
'file.gid_to_group:'
Convert the group id to the group name on this system
gid
gid to convert to a group name
CLI Example:
salt '*' file.gid_to_group 0
'file.grep:'
Grep for a string in the specified file
Note:
This function's return value is slated for refinement in future
versions of Salt
path
A file path
pattern
A string. For example:
``test``
``a[0-5]``
args
grep options. For example:
``" -v"``
``" -i -B2"``
CLI Example:
..........
//SaltStack預設也支援一次執行多個Module,Module之間通過逗號隔開,預設傳參之間也是用逗號分隔,也支援指定傳參分隔符號--args-separator=@即可
[root@node01 ~]# salt 'node02' test.echo,cmd.run,service.status hello,hostname,salt-minion
node02:
----------
cmd.run:
node02
service.status:
True
test.echo:
hello
2. SaltStack常用模組
關於模組的用法可以去官方文件檢視
2.1 SaltStack常用模組之network
2.1.1 network.active_tcp
返回所有活動的tcp連線
[root@node01 ~]# salt 'node02' network.active_tcp
node02:
----------
0:
----------
local_addr:
0.0.0.0
local_port:
22
remote_addr:
0.0.0.0
remote_port:
0
1:
----------
local_addr:
127.0.0.1
local_port:
25
remote_addr:
0.0.0.0
remote_port:
0
2:
----------
local_addr:
192.168.159.137
local_port:
43887
remote_addr:
192.168.159.136
remote_port:
4506
3:
----------
local_addr:
192.168.159.137
local_port:
22
remote_addr:
192.168.159.1
remote_port:
61572
4:
----------
local_addr:
192.168.159.137
local_port:
52034
remote_addr:
192.168.159.136
remote_port:
4505
5:
----------
local_addr:
192.168.159.137
local_port:
22
remote_addr:
192.168.159.1
remote_port:
51667
2.1.2 network.connect
測試minion至某一臺伺服器的網路是否連通
[root@node01 ~]# salt 'node02' network.connect baidu.com 80
node02:
----------
comment:
Successfully connected to baidu.com (220.181.38.148) on tcp port 80
result:
True
2.1.3 network.default_route
檢視預設路由
[root@node01 ~]# salt 'node02' network.default_route
node02:
|_
----------
addr_family:
inet
destination:
0.0.0.0
flags:
UG
gateway:
192.168.159.2
interface:
eno16777736
netmask:
0.0.0.0
|_
----------
addr_family:
inet6
destination:
::/0
flags:
-1
gateway:
::
interface:
lo
netmask:
|_
----------
addr_family:
inet6
destination:
::/0
flags:
-1
gateway:
::
interface:
lo
netmask:
2.1.4 network.get_hostname
獲取主機名
[root@node01 ~]# salt 'node02' network.get_hostname
node02:
node02
2.1.5 network.get_route
查詢到一個目標網路的路由資訊
[root@node01 ~]# salt 'node02' network.get_route 192.168.159.139
node02:
----------
destination:
192.168.159.139
gateway:
None
interface:
eno16777736
source:
192.168.159.137
2.1.6 network.hw_addr
返回指定網絡卡的MAC地址
[root@node01 ~]# salt 'node02' network.hwaddr eno16777736
node02:
00:0c:29:b2:1a:0e
2.1.7 network.in_subnet
判斷當前主機是否在某一個網段內
[root@node01 ~]# salt 'node02' network.in_subnet 192.168.69.0/24
node02:
False
[root@node01 ~]# salt 'node02' network.in_subnet 192.168.159.0/24
node02:
True
2.1.8 network.interface
返回指定網絡卡的資訊
[root@node01 ~]# salt 'node02' network.interface eno16777736
node02:
|_
----------
address:
192.168.159.137
broadcast:
192.168.159.255
label:
eno16777736
netmask:
255.255.255.0
2.1.9 network.interface_ip
返回指定網絡卡的IP地址
[root@node01 ~]# salt 'node02' network.interface_ip eno16777736
node02:
192.168.159.137
2.1.10 network.interfaces
返回當前系統中所有的網絡卡資訊
[root@node01 ~]# salt 'node02' network.interfaces
node02:
----------
eno16777736:
----------
hwaddr:
00:0c:29:b2:1a:0e
inet:
|_
----------
address:
192.168.159.137
broadcast:
192.168.159.255
label:
eno16777736
netmask:
255.255.255.0
inet6:
|_
----------
address:
fe80::20c:29ff:feb2:1a0e
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.11 network.ip_addrs
返回一個IPv4的地址列表
該函式將會忽略掉127.0.0.1
的地址
[root@node01 ~]# salt 'node02' network.ip_addrs
node02:
- 192.168.159.137
2.1.12 network.netstat
返回所有開啟的埠和狀態
[root@node01 ~]# salt 'node02' network.netstat
node02:
|_
----------
inode:
17491
local-address:
0.0.0.0:22
program:
1076/sshd
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
18288
local-address:
127.0.0.1:25
program:
1492/master
proto:
tcp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
19363
local-address:
192.168.159.137:43887
program:
2234/python
proto:
tcp
recv-q:
0
remote-address:
192.168.159.136:4506
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
23685
local-address:
192.168.159.137:22
program:
3109/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.159.1:61572
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
19458
local-address:
192.168.159.137:52034
program:
2234/python
proto:
tcp
recv-q:
0
remote-address:
192.168.159.136:4505
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
18986
local-address:
192.168.159.137:22
program:
2205/sshd:
proto:
tcp
recv-q:
0
remote-address:
192.168.159.1:51667
send-q:
0
state:
ESTABLISHED
user:
0
|_
----------
inode:
17500
local-address:
:::22
program:
1076/sshd
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
18289
local-address:
::1:25
program:
1492/master
proto:
tcp6
recv-q:
0
remote-address:
:::*
send-q:
0
state:
LISTEN
user:
0
|_
----------
inode:
23622
local-address:
0.0.0.0:9352
program:
3092/dhclient
proto:
udp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
user:
0
|_
----------
inode:
23639
local-address:
0.0.0.0:68
program:
3092/dhclient
proto:
udp
recv-q:
0
remote-address:
0.0.0.0:*
send-q:
0
user:
0
|_
----------
inode:
23623
local-address:
:::53139
program:
3092/dhclient
proto:
udp6
recv-q:
0
remote-address:
:::*
send-q:
0
user:
0
2.1.13 network.ping
使用ping命令測試到某主機的連通性
[root@node01 ~]# salt 'node02' network.ping baidu.com
node02:
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148: icmp_seq=1 ttl=128 time=71.5 ms
64 bytes from 220.181.38.148: icmp_seq=2 ttl=128 time=57.3 ms
64 bytes from 220.181.38.148: icmp_seq=4 ttl=128 time=83.9 ms
--- baidu.com ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3029ms
rtt min/avg/max/mdev = 57.320/70.940/83.946/10.880 ms
2.2 SaltStack常用模組之service
2.2.1 service.available
判斷指定的服務是否可用
[root@node01 ~]# salt 'node02' service.available sshd
node02:
True
[root@node01 ~]# salt 'node02' service.available mysql
node02:
False
2.2.2 service.get_all
獲取所有正在執行的服務
[root@node01 ~]# salt 'node02' service.get_all
node02:
- -.mount
- NetworkManager
- NetworkManager-dispatcher
- NetworkManager-wait-online
- README
- arp-ethers
- auditd
- autovt@
- basic.target
- blk-availability
- bluetooth.target
- boot.mount
- brandbot
- brandbot.path
- console-getty
- console-shell
- container-getty@
- cpupower
- crond
- cryptsetup-pre.target
- cryptsetup.target
- ctrl-alt-del.target
- dbus
- dbus-org.freedesktop.NetworkManager
- dbus-org.freedesktop.hostname1
- dbus-org.freedesktop.import1
..........
2.2.3 service.disabled
檢查指定服務是否開機不自動啟動
[root@node01 ~]# salt 'node02' service.disabled httpd
node02:
True
[root@node01 ~]# salt 'node02' service.disabled sshd
node02:
False
2.2.4 service.enabled
檢查指定服務是否開機自動啟動
[root@node01 ~]# salt 'node02' service.enabled httpd
node02:
False
[root@node01 ~]# salt 'node02' service.enabled sshd
node02:
True
2.2.5 service.disable
設定指定服務開機不自動啟動
[root@node01 ~]# salt 'node02' service.disable httpd
node02:
True
[root@node01 ~]# salt 'node02' service.enabled httpd
node02:
False
2.2.6 service.enable
設定指定服務開機自動啟動
[root@node01 ~]# salt 'node02' service.enable httpd
node02:
True
[root@node01 ~]# salt 'node02' service.enabled httpd
node02:
True
2.2.7 service.reload
重新載入指定服務
[root@node01 ~]# salt 'node02' service.reload httpd
node02:
True
2.2.8 service.stop
停止指定服務
[root@node01 ~]# salt 'node02' service.stop httpd
node02:
True
2.2.9 service.start
啟動指定服務
[root@node01 ~]# salt 'node02' service.start httpd
node02:
True
2.2.10 service.restart
重啟指定服務
[root@node01 ~]# salt 'node02' service.restart httpd
node02:
True
2.2.11 service.status
檢視指定服務的狀態
[root@node01 ~]# salt 'node02' service.status httpd
node02:
True
2.3 SaltStack常用模組之pkg
2.3.1 pkg.download
只下載軟體包但不安裝
此功能將會下載指定的軟體包,但是需要在minion端安裝yum-utils
,可以使用 cmd.run 進行遠端安裝
[root@node01 ~]# salt 'node02' pkg.download httpd
node02:
----------
httpd:
/var/cache/yum/packages/httpd-2.4.6-93.el7.centos.x86_64.rpm ///這裡是軟體包下載的位置
[root@node02 ~]# ls /var/cache/yum/packages/
httpd-2.4.6-93.el7.centos.x86_64.rpm
2.3.2 pkg.file_list
列出指定包或系統中已安裝的所有包的檔案
[root@node01 ~]# salt 'node02' pkg.file_list httpd
node02:
----------
errors:
files:
- /etc/httpd
- /etc/httpd/conf
- /etc/httpd/conf.d
- /etc/httpd/conf.d/README
- /etc/httpd/conf.d/autoindex.conf
- /etc/httpd/conf.d/userdir.conf
- /etc/httpd/conf.d/welcome.conf
- /etc/httpd/conf.modules.d
- /etc/httpd/conf.modules.d/00-base.conf
- /etc/httpd/conf.modules.d/00-dav.conf
- /etc/httpd/conf.modules.d/00-lua.conf
- /etc/httpd/conf.modules.d/00-mpm.conf
- /etc/httpd/conf.modules.d/00-proxy.conf
- /etc/httpd/conf.modules.d/00-systemd.conf
- /etc/httpd/conf.modules.d/01-cgi.conf
- /etc/httpd/conf/httpd.conf
- /etc/httpd/conf/magic
- /etc/httpd/logs
- /etc/httpd/modules
- /etc/httpd/run
- /etc/logrotate.d/httpd
- /etc/sysconfig/htcacheclean
- /etc/sysconfig/httpd
- /run/httpd
- /run/httpd/htcacheclean
- /usr/lib/systemd/system/htcacheclean.service
- /usr/lib/systemd/system/httpd.service
- /usr/lib/tmpfiles.d/httpd.conf
- /usr/lib64/httpd
..........
//當不提供引數時,將會列出當前系統中所有已安裝軟體的檔案列表
[root@node01 ~]# salt 'node02' pkg.file_list
..........
- /usr/share/man/man8/lvs.8.gz
- /usr/share/man/man8/lvscan.8.gz
- /usr/share/man/man8/pvchange.8.gz
- /usr/share/man/man8/pvck.8.gz
- /usr/share/man/man8/pvcreate.8.gz
- /usr/share/man/man8/pvdisplay.8.gz
- /usr/share/man/man8/pvmove.8.gz
- /usr/share/man/man8/pvremove.8.gz
- /usr/share/man/man8/pvresize.8.gz
- /usr/share/man/man8/pvs.8.gz
- /usr/share/man/man8/pvscan.8.gz
- /usr/share/man/man8/vgcfgbackup.8.gz
- /usr/share/man/man8/vgcfgrestore.8.gz
- /usr/share/man/man8/vgchange.8.gz
- /usr/share/man/man8/vgck.8.gz
- /usr/share/man/man8/vgconvert.8.gz
- /usr/share/man/man8/vgcreate.8.gz
- /usr/share/man/man8/vgdisplay.8.gz
- /usr/share/man/man8/vgexport.8.gz
- /usr/share/man/man8/vgextend.8.gz
- /usr/share/man/man8/vgimport.8.gz
- /usr/share/man/man8/vgimportclone.8.gz
- /usr/share/man/man8/vgmerge.8.gz
- /usr/share/man/man8/vgmknodes.8.gz
- /usr/share/man/man8/vgreduce.8.gz
- /usr/share/man/man8/vgremove.8.gz
- /usr/share/man/man8/vgrename.8.gz
- /usr/share/man/man8/vgs.8.gz
- /usr/share/man/man8/vgscan.8.gz
- /usr/share/man/man8/vgsplit.8.gz
2.3.3 pkg.group_info
檢視包組的資訊
[root@node01 ~]# salt 'node02' pkg.group_info 'Development Tools'
node02:
----------
conditional:
default:
- byacc
- cscope
- ctags
- diffstat
- doxygen
- elfutils
- gcc-gfortran
- git
- indent
- intltool
- patchutils
- rcs
- subversion
- swig
- systemtap
description:
A basic development environment.
group:
Development Tools
id:
development
mandatory:
- autoconf
- automake
- binutils
- bison
- flex
- gcc
- gcc-c++
- gettext
- libtool
- make
- patch
- pkgconfig
- redhat-rpm-config
- rpm-build
- rpm-sign
optional:
- ElectricFence
- ant
- babel
- bzr
- ccache
- chrpath
- clips
- clips-devel
- clips-doc
- clips-emacs
- clips-xclips
- clipsmm-devel
- clipsmm-doc
- cmake
- cmucl
- colordiff
- compat-gcc-44
- compat-gcc-44-c++
- cvs
- cvsps
- darcs
- dejagnu
- email2trac
- expect
- ftnchek
- gcc-gnat
- gcc-objc
- gcc-objc++
- ghc
- git
- haskell-platform
- imake
- javapackages-tools
- ksc
- lua
- mercurial
- mock
- mod_dav_svn
- nasm
- nqc
- nqc-doc
- ocaml
- perltidy
- qgit
- rpmdevtools
- rpmlint
- sbcl
- scorep
- systemtap-sdt-devel
- systemtap-server
- trac
- trac-git-plugin
- trac-mercurial-plugin
- trac-webadmin
- translate-toolkit
type:
package group
2.3.4 pkg.group_list
列出系統中所有的包組
[root@node01 ~]# salt 'node02' pkg.group_list
node02:
----------
available:
- Additional Development
- Anaconda Tools
- Backup Client
- Backup Server
- Base
- Buildsystem building group
- CentOS Linux Client product core
- CentOS Linux ComputeNode product core
- CentOS Linux Server product core
- CentOS Linux Workstation product core
- Cinnamon
- Common NetworkManager submodules
- Compatibility Libraries
- Conflicts (Client)
- Conflicts (ComputeNode)
- Conflicts (Server)
- Conflicts (Workstation)
- Console Internet Tools
- Core
- DNS Name Server
- Debugging Tools
- Desktop Debugging and Performance Tools
- Development Tools
- Dial-up Networking Support
- Directory Client
- Directory Server
- E-mail Server
- Educational Software
- Electronic Lab
- Emacs
- FTP Server
- Fedora Packager
- File and Storage Server
- Fonts
- GNOME
- GNOME Applications
- General Purpose Desktop
- Graphical Administration Tools
- Graphics Creation Tools
- Guest Agents
- Guest Desktop Agents
- Hardware Monitoring Utilities
- Haskell
- High Availability
- Hyper-v platform specific packages
- Identity Management Server
- Infiniband Support
- Input Methods
- Internet Applications
- Internet Browser
- Java Platform
- KDE
- KDE Applications
- KDE Multimedia Support
- KVM platform specific packages
- LXQt Desktop
- Large Systems Performance
- Legacy UNIX Compatibility
- Legacy X Window System Compatibility
- Load Balancer
- MATE
- Mainframe Access
- MariaDB Database Client
- MariaDB Database Server
- Milkymist
- Multimedia
- Network File System Client
- Network Infrastructure Server
- Networking Tools
- Office Suite and Productivity
- PHP Support
- Performance Tools
- Perl Support
- Perl for Web
- Platform Development
- PostgreSQL Database Client
- PostgreSQL Database Server
- Print Server
- Printing Client
- Python
- Remote Desktop Clients
- Remote Management for Linux
- Resilient Storage
- Ruby Support
- Scientific Support
- Security Tools
- Smart Card Support
- System Administration Tools
- System Management
- Technical Writing
- TurboGears application framework
- VMware platform specific packages
- Virtualization Client
- Virtualization Hypervisor
- Virtualization Platform
- Virtualization Tools
- Web Server
- Web Servlet Engine
- X Window System
- Xfce
available environments:
- Minimal Install
- Compute Node
- Infrastructure Server
- File and Print Server
- Cinnamon Desktop
- MATE Desktop
- Basic Web Server
- Virtualization Host
- Server with GUI
- GNOME Desktop
- KDE Plasma Workspaces
- Development and Creative Workstation
available languages:
----------
installed:
installed environments:
2.3.5 pkg.install
安裝軟體
[root@node01 ~]# salt 'node02' pkg.install wget
node02:
----------
wget:
----------
new:
1.14-18.el7_6.1
old:
2.3.6 pkg.list_downloaded
列出已下載到本地的軟體包
[root@node01 ~]# salt 'node02' pkg.list_pkgs
node02:
----------
NetworkManager:
1:1.0.6-27.el7
NetworkManager-libnm:
1:1.0.6-27.el7
NetworkManager-team:
1:1.0.6-27.el7
NetworkManager-tui:
1:1.0.6-27.el7
PyYAML:
3.10-11.el7
acl:
2.2.51-12.el7
aic94xx-firmware:
30-6.el7
alsa-firmware:
1.0.28-2.el7
alsa-lib:
1.0.28-2.el7
alsa-tools-firmware:
1.0.28-2.el7
apr:
1.4.8-5.el7
apr-util:
1.5.2-6.el7
audit:
2.4.1-5.el7
audit-libs:
2.4.1-5.el7
authconfig:
6.2.8-10.el7
avahi-autoipd:
0.6.31-15.el7
avahi-libs:
0.6.31-15.el7
basesystem:
10.0-7.el7.centos
bash:
4.2.46-19.el7
bind-libs-lite:
32:9.9.4-29.el7
bind-license:
32:9.9.4-29.el7
binutils:
2.23.52.0.1-55.el7
biosdevname:
0.6.2-1.el7
btrfs-progs:
3.19.1-1.el7
bzip2-libs:
1.0.6-13.el7
ca-certificates:
2015.2.4-71.el7
..........
2.3.7 pkg.owner
列出指定檔案是由哪個包提供的
[root@node01 ~]# salt 'node02' pkg.owner /usr/sbin/apachectl
node02:
httpd
[root@node01 ~]# salt 'node02' pkg.owner /usr/sbin/apachectl /etc/httpd/conf/httpd.conf
node02:
----------
/etc/httpd/conf/httpd.conf:
httpd
/usr/sbin/apachectl:
httpd
2.3.8 pkg.remove
解除安裝指定軟體
[root@node01 ~]# salt 'node02' cmd.run 'rpm -qa|grep wget'
node02:
wget-1.14-18.el7_6.1.x86_64
[root@node01 ~]# salt 'node02' pkg.remove wget
node02:
----------
wget:
----------
new:
old:
1.14-18.el7_6.1
//若要解除安裝多個檔案,中間需要用逗號隔開
2.3.9 pkg.upgrade
升級系統中所有的軟體包或升級指定的軟體包
[root@node01 ~]# salt 'node02' pkg.upgrade name=openssl
node02:
----------
openssl:
----------
new:
1:1.0.2k-16.el7
old:
1:1.0.2k-8.el7
openssl-libs:
----------
new:
1:1.0.2k-16.el7
old:
1:1.0.2k-8.el7
//若想升級系統中所有的軟體包則把 name 引數去掉即可
2.4 SaltStack常用模組之state
2.4.1 state.show_highstate
顯示當前系統中有哪些高階狀態
[root@node01 ~]# salt 'node02' state.show_highstate
node02:
----------
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
2.4.1 state.show_top
返回minion將用於highstate的頂級資料
[root@node01 ~]# salt 'node02' state.show_top
node02:
----------
base:
- web.apache.apache
2.4.2 state.show_sls
顯示 master 上特定sls或sls檔案列表中的狀態資料
node02:
----------
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
2.5 SaltStack常用模組之salt-cp
salt-cp
能夠很方便的把 master 上的檔案批量傳到 minion上
//拷貝單個檔案到目標主機的/usr/src目錄下
[root@node01 ~]# salt 'node02' cmd.run 'ls /usr/src'
node02:
debug
kernels
[root@node01 ~]# touch abc
[root@node01 ~]# salt-cp 'node02' abc /usr/src/
{'node02': {'/usr/src/abc': True}}
[root@node01 ~]#
[root@node01 ~]# salt 'node02' cmd.run 'ls /usr/src'
node02:
abc
debug
kernels
//拷貝多個檔案到目標主機的/usr/src目錄下
[root@node01 ~]# touch haha xixi
[root@node01 ~]# salt 'node02' cmd.run 'ls /usr/src'
node02:
abc
debug
kernels
[root@node01 ~]# salt-cp 'node02' haha xixi /usr/src/
{'node02': {'/usr/src/haha': True, '/usr/src/xixi': True}}
[root@node01 ~]# salt 'node02' cmd.run 'ls /usr/src'
node02:
abc
debug
haha
kernels
xixi
2.6 SaltStack常用模組之file
2.6.1 file.access
檢查指定路徑是否存在
[root@node01 ~]# salt 'node02' cmd.run 'ls /usr/src'
node02:
abc
debug
haha
kernels
xixi
[root@node01 ~]# salt 'node02' file.access /usr/src/xixi f
node02:
True
[root@node01 ~]# salt 'node02' file.access /usr/src/sdada f
node02:
False
檢查指定檔案的許可權資訊
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /usr/src/'
node02:
total 0
-rw-r--r-- 1 root root 0 Aug 18 19:41 abc
drwxr-xr-x. 2 root root 6 Apr 11 2018 debug
-rw-r--r-- 1 root root 0 Aug 18 19:43 haha
drwxr-xr-x. 2 root root 6 Apr 11 2018 kernels
-rw-r--r-- 1 root root 0 Aug 18 19:43 xixi
[root@node01 ~]# salt 'node02' file.access /usr/src/xixi r //是否有讀許可權
node02:
True
[root@node01 ~]# salt 'node02' file.access /usr/src/xixi w //是否有寫許可權
node02:
True
[root@node01 ~]# salt 'node02' file.access /usr/src/xixi x //是否有執行許可權
node02:
False
2.6.2 file.append
往一個檔案裡追加內容,若此檔案不存在則會報異常
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/abc'
node02:
ls: cannot access /root/abc: No such file or directory
ERROR: Minions returned with non-zero exit code
[root@node01 ~]# salt 'node02' cmd.run 'touch /root/abc'
node02:
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/abc'
node02:
-rw-r--r-- 1 root root 0 Aug 18 19:47 /root/abc
[root@node01 ~]# salt 'node02' file.append /root/abc "hello world" "haha" "xixi"
node02:
Wrote 3 lines to "/root/abc"
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/abc'
node02:
-rw-r--r-- 1 root root 22 Aug 18 19:47 /root/abc
[root@node01 ~]# salt 'node02' cmd.run 'cat /root/abc'
node02:
hello world
haha
xixi
2.6.3 file.basename
獲取指定路徑的基名
[root@node01 ~]# salt 'node02' file.basename '/root/year/dragon'
node02:
dragon
2.6.4 file.dirname
獲取指定路徑的目錄名
[root@node01 ~]# salt 'node02' file.dirname '/root/year/dragon'
node02:
/root/year
2.6.5 file.check_hash
檢查指定的檔案與hash字串是否匹配,匹配則返回 True 否則返回 False
[root@node01 ~]# salt 'node02' cmd.run 'md5sum /etc/passwd'
node02:
405cd8e89a3a1408b58747f248dae6db /etc/passwd
[root@node01 ~]# salt 'node02' file.check_hash /etc/passwd 405cd8e89a3a1408b58747f248dae6db
node02:
True
2.6.6 file.chown
設定指定檔案的屬主、屬組資訊
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 8
-rw-r--r-- 1 tom tom 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.chown /root/abc root root
node02:
None
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 8
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
2.6.7 file.copy
在遠端主機上覆制檔案或目錄
拷貝檔案
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 8
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.copy /root/abc /root/qwq
node02:
True
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
2.6.8 file.ditectory_exists
判斷指定目錄是否存在,存在則返回 True ,否則返回 False
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.directory_exists /root/year
node02:
True
[root@node01 ~]# salt 'node02' file.directory_exists /root/abcd
node02:
False
2.6.9 file.diskusage
遞迴計算指定路徑的磁碟使用情況並以位元組為單位返回
[root@node01 ~]# salt 'node02' cmd.run 'du -sb /etc'
node02:
32710524 /etc
[root@node01 ~]# salt 'node02' file.diskusage /etc
node02:
34149345
2.6.10 file.file_exists
判斷指定檔案是否存在
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.file_exists /root/abc
node02:
True
2.6.11 file.get_gid
獲取指定檔案的gid
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.get_gid /root/abc
node02:
0
2.6.12 file.get_group
獲取指定檔案的組名
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.get_group /root/abc
node02:
root
2.6.13 file.get_hash
獲取指定檔案的hash值,該值通過 sha256 演算法得來
[root@node01 ~]# salt 'node02' cmd.run 'sha256sum /root/abc'
node02:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/abc
[root@node01 ~]# salt 'node02' file.get_hash /root/abc
node02:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
2.6.14 file.get_mode
獲取指定檔案的許可權,以數字方式顯示
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.get_mode /root/abc
node02:
0644
2.6.15 file.get_selinux_context
獲取指定檔案的 SELINUX 上下文資訊
[root@node01 ~]# salt 'node02' cmd.run 'ls -Z /root/anaconda-ks.cfg'
node02:
-rw-------. root root system_u:object_r:admin_home_t:s0 /root/anaconda-ks.cfg
[root@node01 ~]# salt 'node02' file.get_selinux_context /root/anaconda-ks.cfg
node02:
system_u:object_r:admin_home_t:s0
2.6.16 file.get_sum
按照指定的演算法計算指定檔案的特徵碼並顯示,預設使用的sha256演算法。
該函式可使用的演算法引數有:
- md5
- sha1
- sha224
- sha256 (default)
- sha384
- sha512
[root@node01 ~]# salt 'node02' cmd.run 'sha256sum /root/abc'
node02:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a /root/abc
[root@node01 ~]# salt 'node02' file.get_sum /root/abc
node02:
11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
[root@node01 ~]# salt 'node02' cmd.run 'md5sum /root/abc'
node02:
671ded4ec86c82a8779c8df17823f810 /root/abc
[root@node01 ~]# salt 'node02' file.get_sum /root/abc md5
node02:
671ded4ec86c82a8779c8df17823f810
2.6.17 file.get_uid與file.get_user
獲取指定檔案的 uid 或 使用者名稱
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/abc'
node02:
-rw-r--r-- 1 root root 22 Aug 18 19:47 /root/abc
[root@node01 ~]# salt 'node02' file.get_uid /root/abc
node02:
0
[root@node01 ~]# salt 'node02' file.get_user /root/abc
node02:
root
2.6.18 file.gid_to_group
將指定的 gid 轉換為組名並顯示
[root@node01 ~]# salt 'node02' file.gid_to_group 1000
node02:
tom
[root@node01 ~]# salt 'node02' file.gid_to_group 0
node02:
root
2.6.19 file.group_to_gid
將指定的組名轉換為 gid 並顯示
[root@node01 ~]# salt 'node02' file.group_to_gid root
node02:
0
[root@node01 ~]# salt 'node02' file.group_to_gid tom
node02:
1000
2.6.20 file.grep
在指定檔案中檢索指定內容
該函式支援萬用字元,若在指定的路徑中用萬用字元則必須用雙引號引起來
salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i -B2
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
2.6.21 file.is_blkdev
判斷指定的檔案是否是塊裝置檔案
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /dev/sr0'
node02:
brw-rw---- 1 root cdrom 11, 0 Aug 18 14:23 /dev/sr0
[root@node01 ~]# salt 'node02' file.is_blkdev /dev/sr0
node02:
True
2.6.22 file.mkdir
建立目錄並設定屬主、屬組及許可權
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.mkdir /root/hehe
node02:
None
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.mkdir /root/xixi tom tom 400
node02:
None
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
dr-------- 2 tom tom 6 Aug 18 20:23 xixi
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
2.6.23 file.move
移動或重新命名
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-r--r-- 1 root root 22 Aug 18 19:47 abc
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
dr-------- 2 tom tom 6 Aug 18 20:23 xixi
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.move /root/abc /root/qwe
node02:
----------
comment:
'/root/abc' moved to '/root/qwe'
result:
True
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe
-rw-r--r-- 1 root root 22 Aug 18 19:47 qwe
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
dr-------- 2 tom tom 6 Aug 18 20:23 xixi
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
//移動
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root'
node02:
total 12
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe
-rw-r--r-- 1 root root 22 Aug 18 19:47 qwe
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
dr-------- 2 tom tom 6 Aug 18 20:23 xixi
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /opt'
node02:
total 0
[root@node01 ~]# salt 'node02' file.move /root/xixi /opt/
node02:
----------
comment:
'/root/xixi' moved to '/opt/'
result:
True
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /opt'
node02:
total 0
dr-------- 2 tom tom 6 Aug 18 20:23 xixi
2.6.24 file.prepend
把文字插入指定檔案的開頭
[root@node01 ~]# salt 'node02' cmd.run 'cat /root/abc'
node02:
hello world
xixi
haha
[root@node01 ~]# salt 'node02' file.prepend /root/abc "hehe" "qwq" "qwer"
node02:
Prepended 3 lines to "/root/abc"
[root@node01 ~]# salt 'node02' cmd.run 'cat /root/abc'
node02:
hehe
qwq
qwer
hello world
xixi
haha
2.6.25 file.sed
修改文字檔案的內容
[root@node01 ~]# salt 'node02' cmd.run 'cat /root/abc'
node02:
hehe
qwq
qwer
hello world
xixi
haha
[root@node01 ~]# salt 'node02' file.sed /root/abc 'hello' 'dragon'
node02:
----------
pid:
42255
retcode:
0
stderr:
stdout:
[root@node01 ~]# salt 'node02' cmd.run 'cat /root/abc'
node02:
hehe
qwq
qwer
dragon world
xixi
haha
2.6.26 file.readdir
列出指定目錄下的所有檔案或目錄,包括隱藏檔案
[root@node01 ~]# salt 'node02' file.readdir /root/
node02:
- .
- ..
- .bash_logout
- .bash_profile
- .bashrc
- .cshrc
- .tcshrc
- anaconda-ks.cfg
- .bash_history
- .pki
- .ssh
- year
- qwq
- hehe
- qwe
- .viminfo
- abc.bak
- abc
2.6.27 file.remove
刪除指定的檔案或目錄,若給出的是目錄,將遞迴刪除
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/'
node02:
total 20
-rw-r--r-- 1 root root 37 Aug 18 20:43 abc
-rw-r--r-- 1 root root 36 Aug 18 20:41 abc.bak
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe
-rw-r--r-- 1 root root 22 Aug 18 19:47 qwe
-rw-r--r-- 1 root root 22 Aug 18 19:59 qwq
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.remove /root/qwe
node02:
True
[root@node01 ~]# salt 'node02' file.remove /root/qwq
node02:
True
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/'
node02:
total 12
-rw-r--r-- 1 root root 37 Aug 18 20:43 abc
-rw-r--r-- 1 root root 36 Aug 18 20:41 abc.bak
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
2.6.28 file.rename
重新命名檔案或目錄
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/'
node02:
total 12
-rw-r--r-- 1 root root 37 Aug 18 20:43 abc
-rw-r--r-- 1 root root 36 Aug 18 20:41 abc.bak
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.rename /root/hehe /root/hehe.bak
node02:
True
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/'
node02:
total 12
-rw-r--r-- 1 root root 37 Aug 18 20:43 abc
-rw-r--r-- 1 root root 36 Aug 18 20:41 abc.bak
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe.bak
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
2.6.29 file.set_mode
給指定檔案設定許可權
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/'
node02:
total 12
-rw-r--r-- 1 root root 37 Aug 18 20:43 abc
-rw-r--r-- 1 root root 36 Aug 18 20:41 abc.bak
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe.bak
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.set_mode /root/abc 0400
node02:
0400
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/'
node02:
total 12
-r-------- 1 root root 37 Aug 18 20:43 abc
-rw-r--r-- 1 root root 36 Aug 18 20:41 abc.bak
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe.bak
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
2.6.30 file.symlink
給指定的檔案建立軟連結
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/'
node02:
total 12
-r-------- 1 root root 37 Aug 18 20:43 abc
-rw-r--r-- 1 root root 36 Aug 18 20:41 abc.bak
-rw-------. 1 root root 952 Jul 27 14:24 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Aug 18 20:23 hehe.bak
drwxr-xr-x 2 root root 19 Aug 18 19:48 year
[root@node01 ~]# salt 'node02' file.symlink /root/abc /opt/abc
node02:
True
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /root/,ls -l /opt/'
node02:
ls: cannot access /root/,ls: No such file or directory
/opt/:
total 0
lrwxrwxrwx 1 root root 9 Aug 18 20:49 abc -> /root/abc
dr-------- 2 tom tom 6 Aug 18 20:23 xixi
2.6.31 file.touch
建立空檔案或更新時間戳
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /opt'
node02:
total 0
lrwxrwxrwx 1 root root 9 Aug 18 20:49 abc -> /root/abc
dr-------- 2 tom tom 6 Aug 18 20:23 xixi
[root@node01 ~]# salt 'node02' file.touch /opt/year
node02:
True
[root@node01 ~]# salt 'node02' file.touch /opt/xixi
node02:
True
[root@node01 ~]# salt 'node02' cmd.run 'ls -l /opt'
node02:
total 0
lrwxrwxrwx 1 root root 9 Aug 18 20:49 abc -> /root/abc
dr-------- 2 tom tom 6 Aug 18 20:51 xixi
-rw-r--r-- 1 root root 0 Aug 18 20:50 year
2.6.32 file.uid_to_uid
將指定的使用者轉換成 uid 並顯示出來
[root@node01 ~]# salt 'node02' file.user_to_uid tom
node02:
1000
[root@node01 ~]# salt 'node02' file.user_to_uid root
node02:
0
2.6.33 file.user_to_user
將指定的 uid 轉換成使用者名稱顯示出來
[root@node01 ~]# salt 'node02' file.uid_to_user 0
node02:
root
[root@node01 ~]# salt 'node02' file.uid_to_user 1000
node02:
tom
2.6.34 file.write
往一個指定的檔案裡覆蓋寫入指定內容
[root@node01 ~]# salt 'node02' cmd.run 'cat /root/abc'
node02:
hehe
qwq
qwer
dragon world
xixi
haha
[root@node01 ~]# salt 'node02' file.write /root/abc "I'm tom" "haha" "xixi"
node02:
Wrote 3 lines to "/root/abc"
[root@node01 ~]# salt 'node02' cmd.run 'cat /root/abc'
node02:
I'm tom
haha
xixi