执行高级状态
[root@node1 base]# salt '*' state.highstate web.apache.apache
node1:
----------
ID: states
Function: no.None
Result: False
Comment: No Top file or master_tops data matches found. Please see master log for details.
Changes:
Summary for node1
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
node2:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: None
Comment: The following packages would be installed/updated: httpd
Started: 19:46:51.313227
Duration: 1772.822 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd not present; if created in this state run, it would have been started
Started: 19:46:53.154353
Duration: 19.465 ms
Changes:
Summary for node2
------------
Succeeded: 2 (unchanged=2)
Failed: 0
------------
Total states run: 2
Total run time: 1.792 s
类似 find 命令并返回符合指定条件的路径列表
The options include match criteria:
name = path-glob # case sensitive
iname = path-glob # case insensitive
regex = path-regex # case sensitive
iregex = path-regex # case insensitive
type = file-types # match any listed type
user = users # match any listed user
group = groups # match any listed group
size = [+-]number[size-unit] # default unit = byte
mtime = interval # modified since date
grep = regex # search file contents
and/or actions:
delete [= file-types] # default type = 'f'
exec = command [arg ...] # where {} is replaced by pathname
print [= print-opts]
and/or depth criteria:
maxdepth = maximum depth to transverse in path
mindepth = minimum depth to transverse before checking files or directories
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
获取指定文件的gid
[root@node1 ~]# salt '*' cmd.run 'ls -l /root/a'
node2:
-rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
node1:
-rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
[root@node1 ~]# salt '*' file.get_gid /root/a
node2:
0
node1:
0
获取指定文件的组名
[root@node1 ~]# salt '*' cmd.run 'ls -l /root/a'
node1:
-rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
node2:
-rw-r--r-- 1 root root 22 Feb 23 19:56 /root/a
[root@node1 ~]# salt '*' file.get_group /root/a
node1:
root
node2:
root
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来
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@node1 ~]# salt '*' cmd.run 'ls -l /dev/sr0'
node1:
brw-rw---- 1 root cdrom 11, 0 Feb 23 19:28 /dev/sr0
node2:
brw-rw---- 1 root cdrom 11, 0 Feb 23 19:28 /dev/sr0
[root@node1 ~]# salt '*' file.is_blkdev /dev/sr0
node2:
True
node1:
检查并显示出指定文件的属性信息
[root@node1 ~]# salt '*' cmd.run 'lsattr /root/a'
node2:
----ia---------- /root/a
node1:
----ia---------- /root/a
[root@node1 ~]# salt '*' cmd.run 'chattr +i /root/a'
node2:
node1:
[root@node1 ~]# salt '*' file.lsattr /root/a
node1:
----------
/root/a:
- i
- a
node2:
----------
/root/a:
- i
- a
创建目录并设置属主、属组及权限
[root@node1 ~]# salt '*' file.mkdir /root/haha tom tom
node2:
True
node1:
True
[root@node1 ~]# salt '*' cmd.run 'ls -l /root/'
node2:
total 12
-rw-r--r-- 1 root root 22 Feb 23 19:56 a
-rw-------. 1 root root 1463 Oct 11 02:25 anaconda-ks.cfg
-rw-r--r-- 1 root root 22 Feb 23 20:04 cc
drwxr-xr-x 2 tom tom 6 Feb 27 16:48 haha
node1:
total 1020
-rw-r--r-- 1 root root 22 Feb 23 19:56 a
-rw-------. 1 root root 1451 Oct 11 00:40 anaconda-ks.cfg
drwxr-xr-x 2 tom tom 6 Feb 27 16:48 haha
drwxr-xr-x. 9 tom tom 186 Oct 11 18:02 nginx-1.16.1
-rw-r--r--. 1 root root 1032630 Oct 11 18:00 nginx-1.16.1.tar.gz
drwxr-xr-x 2 root root 17 Feb 23 19:58 zabbix
移动或重命名
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root'
node1:
total 1020
-rw-r--r-- 1 root root 22 Feb 23 19:56 a
-rw-r--r-- 1 root root 0 Feb 27 16:50 ab
-rw-------. 1 root root 1451 Oct 11 00:40 anaconda-ks.cfg
drwxr-xr-x 2 tom tom 6 Feb 27 16:48 haha
drwxr-xr-x. 9 tom tom 186 Oct 11 18:02 nginx-1.16.1
-rw-r--r--. 1 root root 1032630 Oct 11 18:00 nginx-1.16.1.tar.gz
drwxr-xr-x 2 root root 17 Feb 23 19:58 zabbix
[root@node1 ~]# salt '*' file.move /root/ab /root/bc
node2:
ERROR: Unable to move '/root/ab' to '/root/bc': [Errno 2] No such file or directory: u'/root/ab'
node1:
----------
comment:
'/root/ab' moved to '/root/bc'
result:
True
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /root'
node1:
total 1020
-rw-r--r-- 1 root root 22 Feb 23 19:56 a
-rw-------. 1 root root 1451 Oct 11 00:40 anaconda-ks.cfg
-rw-r--r-- 1 root root 0 Feb 27 16:50 bc
drwxr-xr-x 2 tom tom 6 Feb 27 16:48 haha
drwxr-xr-x. 9 tom tom 186 Oct 11 18:02 nginx-1.16.1
-rw-r--r--. 1 root root 1032630 Oct 11 18:00 nginx-1.16.1.tar.gz
drwxr-xr-x 2 root root 17 Feb 23 19:58 zabbix
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /opt'
node1:
total 1048576
drwxr-xr-x. 6 root root 54 Oct 11 18:06 nginx-1.16.1
drwxr-xr-x. 2 root root 6 Oct 11 15:52 sdb
drwxr-xr-x. 2 root root 6 Oct 11 16:12 sdc
-rw-r--r--. 1 root root 1073741824 Oct 12 14:49 swap_file
[root@node1 ~]# salt 'node1*' file.move /root/bc /opt/
node1:
----------
comment:
'/root/bc' moved to '/opt/'
result:
True
[root@node1 ~]# salt 'node1*' cmd.run 'ls -l /opt'
node1:
total 1048576
-rw-r--r-- 1 root root 0 Feb 27 16:50 bc
drwxr-xr-x. 6 root root 54 Oct 11 18:06 nginx-1.16.1
drwxr-xr-x. 2 root root 6 Oct 11 15:52 sdb
drwxr-xr-x. 2 root root 6 Oct 11 16:12 sdc
-rw-r--r--. 1 root root 1073741824 Oct 12 14:49 swap_file
把文本插入指定文件的开头
[root@node1 ~]# salt 'node1*' cmd.run 'cat /opt/bc'
node1:
[root@node1 ~]# salt 'node1*' file.prepend /opt/bc "hehe" "xixi" "haha"
node1:
Prepended 3 lines to "/opt/bc"
[root@node1 ~]# salt 'node1*' cmd.run 'cat /opt/bc'
node1:
hehe
xixi
haha