SaltStack常用模块——file

检查指定路径是否存在

[root@master ~]# salt '*' cmd.run 'ls /usr/src'
192.168.136.132:
    debug
    group
    kernels
    passwd
    shadow

[root@master ~]# salt '*' file.access /usr/src/tjj f
192.168.136.132:
    False

检查指定文件的权限信息

[root@master ~]# salt '*' cmd.run 'ls -l /usr/src/'
192.168.136.132:
    total 12
    drwxr-xr-x. 2 root root   6 Apr 11  2018 debug
    -rw-r--r--  1 root root 477 Aug 19 22:58 group
    drwxr-xr-x. 2 root root   6 Apr 11  2018 kernels
    -rw-r--r--  1 root root 946 Aug 19 22:57 passwd
    -rw-r--r--  1 root root 604 Aug 19 22:58 shadow
[root@master ~]# salt '*' file.access /usr/src/passwd r 
192.168.136.132:
    True
[root@master ~]# salt '*' file.access /usr/src/passwd w 
192.168.136.132:
    True
[root@master ~]# salt '*' file.access /usr/src/passwd x
192.168.136.132:
    False

file.append
往一个文件里追加内容,若此文件不存在则会报异常

[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.136.132:
    -rw-r--r-- 1 root root 0 Aug 19 22:04 /root/a
[root@master ~]# salt '*' file.append /root/a "hello world" "world hello"
192.168.136.132:
    Wrote 2 lines to "/root/a"
[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    hello world
    world hello

file.basename
获取指定路径的基名,等同于basename命令

[root@master ~]# salt '*' file.basename '/usr/local/tjj'
192.168.136.132:
    tjj

file.dirname
获取指定路径的目录名

[root@master ~]# salt '*' file.dirname '/usr/local/tjj'
192.168.136.132:
    /usr/local

file.check_hash
检查指定的文件与hash字符串是否匹配,匹配则返回 True 否则返回 False

[root@master ~]# salt '*' cmd.run 'md5sum /etc/passwd'
192.168.136.132:
    73020f0a33302qwe6a8d1615a0a9d64ad  /etc/passwd
    
[root@master ~]# salt '*' file.check_hash /etc/passwd 73020f0a33302qwe6a8d1615a0a9d64ad
192.168.136.132:
    True
    

file.chattr
修改指定文件的属性

属性	对文件的意义	对目录的意义
a	只允许在这个文件之后追加数据, 不允许任何进程覆盖或截断这个文件	只允许在这个目录下建立和修改文件, 而不允许删除任何文件
i	不允许对这个文件进行任何的修改, 不能删除、更改、移动	任何的进程只能修改目录之下的文件, 不允许建立和删除文件

给指定文件添加属性

[root@master ~]# salt '*' file.chattr /root/a operator=add attributes=ai
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'lsattr /root'
192.168.136.132:
    ---------------- /root/anaconda-ks.cfg
    ----ia---------- /root/a

给指定文件去除属性

[root@master ~]# salt '*' file.chattr /root/a operator=remove attributes=ai
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'lsattr /root'
192.168.136.132:
    ---------------- /root/anaconda-ks.cfg
    ---------------- /root/a

file.chown
设置指定文件的属主、属组信息

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 8
    -rw-r--r--  1 root root   24 Aug 19 22:04 a
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
[root@master ~]# salt '*' file.chown /root/a tom tom
192.168.136.132:
    None
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 8
    -rw-r--r--  1 tom  tom    24 Aug 19 22:04 a
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg

file.copy
在远程主机上复制文件或目录

拷贝文件

[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 8
    -rw-r--r--  1 tom  tom    24 Aug 19 22:04 a
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
[root@master ~]# salt '*' file.copy /root/a /root/tjj
192.168.136.132:
    True
[root@master ~]#  salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 12
    -rw-r--r--  1 tom  tom    24 Aug 19 22:04 a
    -rw-r--r--  1 tom  tom    24 Aug 19 22:13 tjj
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg

覆盖并拷贝目录,将会覆盖同名文件或目录

[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 12
    -rw-r--r--  1 tom  tom    24 Aug 19 22:04 a
    -rw-r--r--  1 tom  tom    24 Aug 19 22:13 tjj
    -rw-------. 1 root root 1257 Apr  8 02:56 anaconda-ks.cfg

[root@master ~]#  salt '*' file.copy /tmp/ /root/tjjd recurse=True
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 22:04 a
    -rw-r--r--   1 tom  tom    24 Aug 19 22:13 tjj
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg

删除目标目录中同名的文件或目录并拷贝新内容至其中

[root@master ~]# salt '*' cmd.run 'ls -l /root/test'
192.168.136.132:
    total 0
    -rw-r--r-- 1 root root 0 Aug 19 22:19 111
    -rw-r--r-- 1 root root 0 Aug 19 22:19 222
    -rw-r--r-- 1 root root 0 Aug 19 22:19 333
[root@master ~]# salt '*' cmd.run 'ls -l /opt/test'
192.168.136.132:
    total 0
    -rw-r--r-- 1 root root 0 Aug 19 22:19 hhh
    -rw-r--r-- 1 root root 0 Aug 19 22:19 uuu
    -rw-r--r-- 1 root root 0 Aug 19 22:19 nnn
[root@master ~]# salt '*' file.copy /opt/test/ /root/test/ recurse=True remove_existing=True
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/test'
192.168.136.132:
    total 0
    -rw-r--r-- 1 root root 0 Aug 19 22:19 hhh
    -rw-r--r-- 1 root root 0 Aug 19 22:19 uuu
    -rw-r--r-- 1 root root 0 Aug 19 22:19 nnn

file.ditectory_exists
判断指定目录是否存在,存在则返回 True ,否则返回 False

[root@master ~]# salt '*' file.directory_exists /opt/test
192.168.136.132:
    True
[root@master ~]# salt '*' file.directory_exists /opt/test/qqqq
192.168.136.132:
    False

file.diskusage
递归计算指定路径的磁盘使用情况并以字节为单位返回

[root@master ~]# salt '*' cmd.run 'du -sb /etc'
192.168.136.132:
    27980060	/etc
[root@master ~]# salt '*' file.diskusage /etc/
192.168.136.132:
    29498564

file.file_exists
判断指定文件是否存在

[root@master ~]# salt '*' file.file_exists /usr/local/
192.168.136.132:
    False
[root@master ~]# salt '*' file.file_exists /root/anaconda-ks.cfg 
192.168.136.132:
    True

file.find
类似 find 命令并返回符合指定条件的路径列表

匹配条件:

TEXTname    = 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:

TEXTdelete [= file-types]               # default type = 'f'
exec    = command [arg ...]         # where {} is replaced by pathname
print  [= print-opts]

and/or depth criteria:

TEXTmaxdepth = maximum depth to transverse in path
mindepth = minimum depth to transverse before checking files or directories

The default action is print=path

path-regex: a Python Regex (regular expression) pattern to match pathnames

file-types: 包含以下一项或多项的字符串

a: all file types
b: block device
c: character device
d: directory
p: FIFO (named pipe)
f: plain file
l: symlink
s: socket

users: 用空格或都好分隔用户名或uid

groups: 用空格或都好分隔用户名或gid

size-unit:

b: bytes
k: kilobytes
m: megabytes
g: gigabytes
t: terabytes

时间间隔:

[<num>w] [<num>d] [<num>h] [<num>m] [<num>s]

where:
    w: week
    d: day
    h: hour
    m: minute
    s: second

file.get_gid
获取指定文件的gid

[root@master ~]# salt '*' file.get_gid /etc/passwd
192.168.136.132:
    0

file.get_group
获取指定文件的组名

H[root@master ~]# salt '*' file.get_group /etc/passwd
192.168.136.132:
    root

file.get_hash
获取指定文件的hash值,该值通过 sha256 算法得来

[root@master ~]# salt '*' cmd.run 'sha256sum /etc/passwd'
192.168.136.132:
    4683e8cb2eae644f3758da7bd37002bf83ef2041ad6edc62f697a69c2d75df74  /etc/passwd
[root@master ~]# salt '*' file.get_hash /etc/passwd
192.168.136.132:
    4683e8cb2eae644f3758da7bd37002bf83ef2041ad6edc62f697a69c2d75df74

file.get_mode
获取指定文件的权限,以数字方式显示

[root@master ~]# salt '*' cmd.run 'ls -l /etc/passwd'
192.168.136.132:
    -rw-r--r-- 1 root root 998 Aug 19 22:12 /etc/passwd
[root@master ~]# salt '*' file.get_mode /etc/passwd
192.168.136.132:
    0644

file.get_sum
按照指定的算法计算指定文件的特征码并显示,默认使用的sha256算法。
该函数可使用的算法参数有:

md5
sha1
sha224
sha256 (default)
sha384
sha512
[root@master ~]# salt '*' cmd.run 'sha256sum /root/a'
192.168.136.132:
    ededca6726748c692616b9658e937a8a7badd7437c4272d44f1b9aadf9d1f6d7  /root/a
[root@master ~]# salt '*' file.get_sum /root/a
192.168.136.132:
    ededca6726748c692616b9658e937a8a7badd7437c4272d44f1b9aadf9d1f6d7
[root@master ~]# salt '*' cmd.run 'md5sum /root/a'
192.168.136.132:
    6a5f5fb87030a20cc1e371904fdcdf18  /root/a
[root@master ~]# salt '*' file.get_sum /root/a md5
192.168.136.132:
    6a5f5fb87030a20cc1e371904fdcdf18

file.get_uid与file.get_user
获取指定文件的 uid 或 用户名

[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.136.132:
    -rw-r--r-- 1 tom tom 24 Aug 19 22:04 /root/a
[root@master ~]# salt '*' file.get_uid /root/a
192.168.136.132:
    1000
[root@master ~]# salt '*' file.get_user /root/a
192.168.136.132:
    tom

file.gid_to_group
将指定的 gid 转换为组名并显示

[root@master ~]# salt '*' file.group_to_gid root
192.168.136.132:
    0
[root@master ~]# salt '*' file.group_to_gid tom
192.168.136.132:
    1000

file.grep
在指定文件中检索指定内容
该函数支持通配符,若在指定的路径中用通配符则必须用双引号引起来

[root@master ~]# salt '*' file.grep /etc/passwd nobody
192.168.136.132:
    ----------
    pid:
        3910
    retcode:
        0
    stderr:
    stdout:
        nobody:x:99:99:Nobody:/:/sbin/nologin


[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i
192.168.136.132:
    ----------
    pid:
        3971
    retcode:
        0
    stderr:
    stdout:
        IPADDR="192.168.136.132"


[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 ipaddr -- -i -B2
192.168.136.132:
    ----------
    pid:
        4003
    retcode:
        0
    stderr:
    stdout:
        DEVICE=ens33
        ONBOOT=yes
        IPADDR="192.168.136.132"


[root@master ~]# salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
[root@master ~]# salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
192.168.136.132:
    ----------
    pid:
        4022
    retcode:
        0
    stderr:
    stdout:
        /etc/sysconfig/network-scripts/ifcfg-lo
        /etc/sysconfig/network-scripts/ifdown-ipv6
        /etc/sysconfig/network-scripts/ifup-aliases
        /etc/sysconfig/network-scripts/ifup-eth
        /etc/sysconfig/network-scripts/ifup-ippp
        /etc/sysconfig/network-scripts/ifup-ipv6
        /etc/sysconfig/network-scripts/ifup-isdn
        /etc/sysconfig/network-scripts/ifup-plip
        /etc/sysconfig/network-scripts/ifup-plusb
        /etc/sysconfig/network-scripts/ifup-post
        /etc/sysconfig/network-scripts/ifup-ppp
        /etc/sysconfig/network-scripts/ifup-tunnel
        /etc/sysconfig/network-scripts/network-functions
        /etc/sysconfig/network-scripts/ifcfg-ens33

file.is_blkdev
判断指定的文件是否是块设备文件

[root@master ~]# salt '*' file.is_blkdev /dev/sr0
192.168.136.132:
    True

file.lsattr
检查并显示出指定文件的属性信息

[root@master ~]# salt '*' file.lsattr /etc/passwd
192.168.136.132:
    ----------
    /etc/passwd:

file.mkdir
创建目录并设置属主、属组及权限

[root@master ~]# salt '*' file.mkdir /root/hhh
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 22:04 a
    drwxr-xr-x   2 root root    6 Aug 19 22:53 hhh
    -rw-r--r--   1 tom  tom    24 Aug 19 22:13 tjj
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test


[root@master ~]# salt '*' file.mkdir /root/uuu tom tom 400
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 22:04 a
    drwxr-xr-x   2 root root    6 Aug 19 22:53 hhh
    -rw-r--r--   1 tom  tom    24 Aug 19 22:13 tjj
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    dr--------   2 tom  tom     6 Aug 19 22:54 uuu
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test

file.move
移动或重命名

重命名
[root@master ~]# salt '*' file.move /root/hhh /root/nnn
192.168.136.132:
    ----------
    comment:
        '/root/hhh' moved to '/root/nnn'
    result:
        True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 22:04 a
    -rw-r--r--   1 tom  tom    24 Aug 19 22:13 tjj
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    dr--------   2 tom  tom     6 Aug 19 22:54 uuu
    drwxr-xr-x   2 root root    6 Aug 19 22:53 nnn
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test


#移动
[root@master ~]# salt '*' file.move /root/uuu /opt/ggg
192.168.136.132:
    ----------
    comment:
        '/root/uuu' moved to '/opt/ggg'
    result:
        True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 16
    -rw-r--r--   1 tom  tom    24 Aug 19 22:04 a
    -rw-r--r--   1 tom  tom    24 Aug 19 22:13 tjj
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root    6 Aug 19 22:53 nnn
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
192.168.136.132:
    total 0
    dr-------- 2 tom  tom   6 Aug 19 22:54 ggg
    drwxr-xr-x 2 root root 39 Aug 19 22:19 test

file.prepend
把文本插入指定文件的开头

[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    hello world
    world hello
[root@master ~]# salt '*' file.prepend /root/a "11111" "22222" "33333"
192.168.136.132:
    Prepended 3 lines to "/root/a"
[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    11111
    22222
    33333
    hello world
    world hello

file.sed
修改文本文件的内容

[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    11111
    22222
    33333
    hello world
    world hello
[root@master ~]# salt '*' file.sed /root/a 'hello' 'hi'
192.168.136.132:
    ----------
    pid:
        4574
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    11111
    22222
    33333
    hi world
    world hi

每行第一个hi改成hello

[root@master ~]# salt '*' file.sed /root/a 'hi' 'hello' flags=1
192.168.136.132:
    ----------
    pid:
        4926
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    11111
    22222
    33333
    hello world
    world hello

file.read
读取文件内容

[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    11111
    22222
    33333
    hello world
    world hello
[root@master ~]# salt '*' file.read /root/a
192.168.136.132:
    11111
    22222
    33333
    hello world
    world hello

file.readdir
列出指定目录下的所有文件或目录,包括隐藏文件

[root@master ~]# salt '*' file.readdir /root
192.168.136.132:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .pki
    - .bash_history
    - .viminfo
    - tjj
    - tjjd
    - test
    - nnn
    - a.bak
    - a

file.remove
删除指定的文件或目录,若给出的是目录,将递归删除

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 20
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-r--r--   1 tom  tom    24 Aug 19 22:13 tjj
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root    6 Aug 19 22:53 nnn
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test


[root@master ~]# salt '*' file.remove /root/tjj 
192.168.136.132:
    True
[root@master ~]# salt '*' file.remove /root/nnn
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 16
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test

file.rename
重命名文件或目录

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 16
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjd
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test
[root@master ~]# salt '*' file.rename /root/tjjd /root/tjjD
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjD
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test

file.set_mode
给指定文件设置权限

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjD
    -rw-r--r--   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test
[root@master ~]# salt '*' file.set_mode /root/a 0755
192.168.136.132:
    0755
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjD
    -rwxr-xr-x   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test

file.symlink
给指定的文件创建软链接

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.136.132:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjD
    -rwxr-xr-x   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test
[root@master ~]# salt '*' file.symlink /root/a /opt/a
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
192.168.136.132:
    total 0
    lrwxrwxrwx 1 root root  7 Aug 19 22:15 a -> /root/a
    dr-------- 2 tom  tom   6 Aug 19 22:54 ggg
    drwxr-xr-x 2 root root 39 Aug 19 22:19 test

file.touch
创建空文件或更新时间戳

[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjD
    -rwxr-xr-x   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Apr  8 02:56 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test
[root@master ~]# salt '*' file.touch /root/anaconda-ks.cfg 
192.168.136.132:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.136.132:
    total 16
    drwxrwxrwt  24 root root 4096 Aug 19 22:15 tjjD
    -rwxr-xr-x   1 tom  tom    39 Aug 19 22:06 a
    -rw-r--r--   1 tom  tom    33 Aug 19 22:05 a.bak
    -rw-------.  1 root root 1257 Aug 19 22:17 anaconda-ks.cfg
    drwxr-xr-x   2 root root   39 Aug 19 22:19 test

file.uid_to_user
将指定的 uid 转换成用户名显示出来

[root@master ~]# salt '*' file.user_to_uid tom
192.168.136.132:
    1000
[root@master ~]# salt '*' file.user_to_uid root
192.168.136.132:
    0

file.write
往一个指定的文件里覆盖写入指定内容

[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    11111
    22222
    33333
    hello world
    world hello
[root@master ~]# salt '*' file.write /root/a "111111" "222222" "333333"
192.168.136.132:
    Wrote 3 lines to "/root/a"
[root@master ~]# salt '*' cmd.run 'cat /root/a'
192.168.136.132:
    111111
    222222
    333333
上一篇:开发中常用的 4 种加密方法


下一篇:SaltStack之数据系统