SaltStack常用的模块续

Posted 卑微小胡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltStack常用的模块续相关的知识,希望对你有一定的参考价值。

SaltStack常用的模块续

SaltStack常用模块之file

file.access

检查指定路径是否存在

[root@master ~]# salt '*' cmd.run 'ls /root'
minion1:
    123
    456
    abc
    anaconda-ks.cfg
[root@master ~]# salt '*' file.access /root/abc f
minion1:
    True
[root@master ~]# salt '*' file.access /root/121 f
minion1:
    False

检查指定文件的权限信息

[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 4
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 123
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 456
    -rw-r--r--. 1 root root    0 Jul  7 00:15 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
[root@master ~]# salt '*' file.access /root/abc r	#是否有读权限
minion1:
    True
[root@master ~]# salt '*' file.access /root/abc w	#是否有写权限
minion1:
    True
[root@master ~]# salt '*' file.access /root/abc x	#是否有执行权限
minion1:
    False

file.append

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
minion1:
    -rw-r--r--. 1 root root 0 Jul  7 00:15 /root/abc
[root@master ~]# salt '*' file.append /root/abc "hello world" "haha" "bwxh"
minion1:
    Wrote 3 lines to "/root/abc"
[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
minion1:
    -rw-r--r--. 1 root root 22 Jul  7 18:16 /root/abc
[root@master ~]# salt '*' cmd.run 'cat /root/abc'
minion1:
    hello world
    haha
    bwxh

file.basename

获取指定路径的基名

[root@master ~]# salt '*' file.basename '/root/abc'
minion1:
    abc

file.dirname

获取指定路径的目录名

[root@master ~]# salt '*' file.dirname '/root/abc'
minion1:
    /root

file.check_hash

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

[root@master ~]# salt '*' cmd.run 'md5sum /root/abc'
minion1:
    98974aaba92f3ebc1cb4eea07fa3d9eb  /root/abc
[root@master ~]# salt '*' file.check_hash /root/abc 98974aaba92f3ebc1cb4eea07fa3d9eb
minion1:
    True

file.chattr

修改指定文件的属性

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

给指定文件添加属性

[root@master ~]# salt '*' cmd.run 'lsattr /root'
minion1:
    -------------------- /root/anaconda-ks.cfg
    -------------------- /root/abc
    -------------------- /root/123
    -------------------- /root/456
[root@master ~]# salt '*' file.chattr /root/abc operator=add attributes=a
minion1:
    True
[root@master ~]# salt '*' cmd.run 'lsattr /root'
minion1:
    -------------------- /root/anaconda-ks.cfg
    -----a-------------- /root/abc
    -------------------- /root/123
    -------------------- /root/456
[root@minion1 ~]# echo 'abcd' >>abc
[root@minion1 ~]# cat abc
hello world
haha
bwxh
abcd
[root@minion1 ~]# echo 'abcd' >abc
-bash: abc: Operation not permitted

给指定文件去除属性

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

file.chown

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

[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 8
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 123
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 456
    -rw-r--r--. 1 root root   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
[root@master ~]# salt '*' file.chown /root/abc bwxh bwxh
minion1:
    None
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 8
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 123
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 456
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg

file.copy

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

拷贝文件

[root@master ~]# salt '*' file.copy /root/abc /opt/hello
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 4
    -rw-r--r--. 1 bwxh bwxh 27 Jul  7 18:42 hello
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 8
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 123
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 456
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg

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

[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 8
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 123
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 456
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
[root@master ~]# salt '*' file.copy /tmp/ /root/jjyy recurse=True
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 8
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 123
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 456
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drwxrwxrwt. 9 root root  161 Jul  7 18:45 jjyy

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/jjyy'
minion1:
    total 0
    drwxr-xr-x. 2 root root 6 Jul  7 18:45 vmware-root_870-2731086752
    drwxr-xr-x. 2 root root 6 Jul  7 18:45 vmware-root_878-2697663918
[root@master ~]# salt '*' cmd.run 'ls -l /opt/hehe'
minion1:
    total 4
    -rw-r--r--. 1 bwxh bwxh 27 Jul  7 18:42 hello
[root@master ~]# salt '*' file.copy /opt/hehe/ /root/jjyy/ recurse=True remove_existing=True
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/jjyy'
minion1:
    total 4
    -rw-r--r--. 1 root root 27 Jul  7 18:42 hello

file.ditectory_exists

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

[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drwxr-xr-x. 2 root root 19 Jul  7 18:50 hehe
[root@master ~]# salt '*' file.directory_exists /opt/hehe
minion1:
    True
[root@master ~]# salt '*' file.directory_exists /opt/heh
minion1:
    False

file.diskusage

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

[root@master ~]# salt '*' cmd.run 'du -sb /root/abc'
minion1:
    27	/root/abc
[root@master ~]# salt '*' file.diskusage /root/abc
minion1:
    27

file.file_exists

判断指定文件是否存在

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
minion1:
    total 8
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 123
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 456
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drwxr-xr-x. 2 root root   19 Jul  7 18:50 jjyy
[root@master ~]# salt '*' file.file_exists /root/abc
minion1:
    True
[root@master ~]# salt '*' file.file_exists /root/jjyy
minion1:
    False	#返回False是因为abc是目录而非文件

file.find

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

The options include match criteria:

name    = path-glob                 # case sensitive
iname   = path-glob                 # case insensitive
regex   = path-regex                # case sensitive
iregex  = path-regex                # case insensitive
type    = file-types                # match any listed type
user    = users                     # match any listed user
group   = groups                    # match any listed group
size    = [+-]number[size-unit]     # default unit = byte
mtime   = interval                  # modified since date
grep    = regex                     # search file contents

and/or actions:

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

and/or depth criteria:

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

The default action is print=path

path-glob:

*                = match zero or more chars
?                = match any char
[abc]            = match a, b, or c
[!abc] or [^abc] = match anything except a, b, and c
[x-y]            = match chars x through y
[!x-y] or [^x-y] = match anything except chars x through y
{a,b,c}          = match a or b or c

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

file-types: a string of one or more of the following:

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

users: a space and/or comma separated list of user names and/or uids

groups: a space and/or comma separated list of group names and/or gids

size-unit:

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

interval:

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

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

print-opts: a comma and/or space separated list of one or more of the following:

group: group name
md5:   MD5 digest of file contents
mode:  file permissions (as integer)
mtime: last modification time (as time_t)
name:  file basename
path:  file absolute path
size:  file size in bytes
type:  file type
user:  user name

示例:

[root@master ~]# salt '*' file.find / type=f name=\\*.conf size=+100k
minion1:
    - /etc/lvm/lvm.conf
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 '*' cmd.run 'ls -l /root/abc'
minion1:
    -rw-r--r--. 1 bwxh bwxh 27 Jul  7 18:26 /root/abc
[root@master ~]# salt '*' file.get_gid /root/abc
minion1:
    1000
[root@minion1 ~]# id bwxh
uid=1000(bwxh) gid=1000(bwxh) groups=1000(bwxh)

file.get_group

获取指定文件的组名

[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
minion1:
    -rw-r--r--. 1 bwxh bwxh 27 Jul  7 18:26 /root/abc
[root@master ~]# salt '*' file.get_group /root/abc
minion1:
    bwxh

file.get_hash

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

[root@master ~]# salt '*' cmd.run 'sha256sum /root/abc'
minion1:
    159ccb1a342a65c3902e4c831277d1f47c7c616f1400f148cd4db07fe492c998  /root/abc
[root@master ~]# salt '*' file.get_hash /root/abc
minion1:
    159ccb1a342a65c3902e4c831277d1f47c7c616f1400f148cd4db07fe492c998

file.get_mode

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
minion1:
    -rw-r--r--. 1 bwxh bwxh 27 Jul  7 18:26 /root/abc
[root@master ~]# salt '*' file.get_mode /root/abc
minion1:
    0644

file.get_selinux_context

获取指定文件的 SELINUX 上下文信息

[root@master ~]# salt '*' cmd.run 'ls -Z /root/abc'
minion1:
    system_u:object_r:admin_home_t:s0 /root/abc
[root@master ~]# salt '*' file.get_selinux_context /root/abc
minion1:
    system_u:object_r:admin_home_t:s0

file.get_sum

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

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@master ~]# salt '*' cmd.run 'sha256sum /root/abc'
minion1:
    159ccb1a342a65c3902e4c831277d1f47c7c616f1400f148cd4db07fe492c998  /root/abc
[root@master ~]# salt '*' file.get_sum /root/abc
minion1:
    159ccb1a342a65c3902e4c831277d1f47c7c616f1400f148cd4db07fe492c998
[root@master ~]# salt '*' cmd.run 'md5sum /root/abc'
minion1:
    26f6df2cbc04204bd0a2a2b1179d8999  /root/abc
[root@master ~]# salt '*' file.get_sum /root/abc md5
minion1:
    26f6df2cbc04204bd0a2a2b1179d8999

file.get_uid与file.get_user

获取指定文件的 uid 或 用户名

[root@master ~]# salt '*' cmd.run 'ls -l /root/abc'
minion1:
    -rw-r--r--. 1 bwxh bwxh 27 Jul  7 18:26 /root/abc
[root@master ~]# salt '*' file.get_uid /root/abc
minion1:
    1000
[root@master ~]# salt '*' file.get_user /root/abc
minion1:
    bwxh

file.gid_to_group

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

[root@master ~]# salt '*' file.gid_to_group 1000
minion1:
    bwxh
[root@master ~]# salt '*' file.gid_to_group 0
minion1:
    root

file.group_to_gid

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

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

file.grep

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

[root@master ~]# salt '*' file.grep /etc/passwd bwxh
minion1:
    ----------
    pid:
        5312
    retcode:
        0
    stderr:
    stdout:
        bwxh:x:1000:1000::/home/bwxh:/bin/bash

[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 uuid -- -i
minion1:
    ----------
    pid:
        5323
    retcode:
        0
    stderr:
    stdout:
        UUID=58eea1c8-3286-4f6a-a255-7b1259af7ace
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 uuid -- -i -B2
minion1:
    ----------
    pid:
        5328
    retcode:
        0
    stderr:
    stdout:
        IPV6_FAILURE_FATAL=no
        NAME=ens33
        UUID=58eea1c8-3286-4f6a-a255-7b1259af7ace
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 uuid -- -i -A2
minion1:
    ----------
    pid:
        5333
    retcode:
        0
    stderr:
    stdout:
        UUID=58eea1c8-3286-4f6a-a255-7b1259af7ace
        DEVICE=ens33
        ONBOOT=yes
[root@master ~]# salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-ens33 uuid -- -i -C2
minion1:
    ----------
    pid:
        5338
    retcode:
        0
    stderr:
    stdout:
        IPV6_FAILURE_FATAL=no
        NAME=ens33
        UUID=58eea1c8-3286-4f6a-a255-7b1259af7ace
        DEVICE=ens33
        ONBOOT=yes
[root@master ~]# salt '*' file.grep "/etc/sysconfig/network-scripts/*" uuid -- -i -l
minion1:
    ----------
    pid:
        5353
    retcode:
        0
    stderr:
    stdout:
        /etc/sysconfig/network-scripts/ifcfg-ens33

file.is_blkdev

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

[root@master ~]# salt '*' cmd.run 'ls -l /dev/sr0'
minion1:
    brw-rw----. 1 root cdrom 11, 0 Jul  6 22:25 /dev/sr0
[root@master ~]# salt '*' file.is_blkdev /dev/sr0
minion1:
    True
[root@master ~]# salt '*' file.is_blkdev /root/abc
minion1:
    False

file.lsattr

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

[root@master ~]# salt '*' cmd.run 'lsattr /root/abc'
minion1:
    -------------------- /root/abc
[root@master ~]# salt '*' cmd.run 'chattr +i /root/abc'
minion1:
[root@master ~]# salt '*' cmd.run 'lsattr /root/abc'
minion1:
    ----i--------------- /root/abc
[root@master ~]# salt '*' file.lsattr /root/abc
minion1:
    ----------
    /root/abc:
        - i

file.mkdir

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

[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drwxr-xr-x. 2 root root 19 Jul  7 18:50 hehe
[root@master ~]# salt '*' file.mkdir /opt/world
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drwxr-xr-x. 2 root root 19 Jul  7 18:50 hehe
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world
[root@master ~]# salt '*' file.mkdir /opt/bwxh bwxh bwxh 644
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drw-r--r--. 2 bwxh bwxh  6 Jul  7 21:58 bwxh
    drwxr-xr-x. 2 root root 19 Jul  7 18:50 hehe
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world

file.move

移动或重命名

#重命名
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drw-r--r--. 2 bwxh bwxh  6 Jul  7 21:58 bwxh
    drwxr-xr-x. 2 root root 19 Jul  7 18:50 hehe
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world
[root@master ~]# salt '*' file.move /opt/bwxh /opt/hyh
minion1:
    ----------
    comment:
        '/opt/bwxh' moved to '/opt/hyh'
    result:
        True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drwxr-xr-x. 2 root root 19 Jul  7 18:50 hehe
    drw-r--r--. 2 bwxh bwxh  6 Jul  7 21:58 hyh
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world

#移动
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drwxr-xr-x. 2 root root 19 Jul  7 18:50 hehe
    drw-r--r--. 2 bwxh bwxh  6 Jul  7 21:58 hyh
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world
[root@master ~]# salt '*' file.move /opt/hyh /root/
minion1:
    ----------
    comment:
        '/opt/hyh' moved to '/root/'
    result:
        True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 8
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 123
    -rw-rwxr--+ 1 root root    0 Jul  7 00:16 456
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh
    drwxr-xr-x. 2 root root   19 Jul  7 18:50 jjyy
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drwxr-xr-x. 2 root root 19 Jul  7 18:50 hehe
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world

file.prepend

把文本插入指定文件的开头

[root@master ~]# salt '*' cmd.run 'cat /root/123'
minion1:
    hello world
[root@master ~]# salt '*' file.prepend /root/123 "1" "2" "3" "4"
minion1:
    Prepended 4 lines to "/root/123"
[root@master ~]# salt '*' cmd.run 'cat /root/123'
minion1:
    1
    2
    3
    4
    hello world

file.sed

修改文本文件的内容

[root@master ~]# salt '*' cmd.run 'cat /root/456'
minion1:
    bwxh
    haha
    xixi
    bwxh bwxh bwxh hello bwxh
    bwxh bwxh 666
[root@master ~]# salt '*' file.sed /root/456 'bwxh' 'hyh'
minion1:
    ----------
    pid:
        5513
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/456'
minion1:
    hyh
    haha
    xixi
    hyh hyh hyh hello hyh
    hyh hyh 666
[root@master ~]# salt '*' file.sed /root/456 'hyh' 'hello' flags=2
minion1:
    ----------
    pid:
        5523
    retcode:
        0
    stderr:
    stdout:
[root@master ~]# salt '*' cmd.run 'cat /root/456'
minion1:
    hyh
    haha
    xixi
    hyh hello hyh hello hyh
    hyh hello 666

file.read

读取文件内容

[root@master ~]# salt '*' cmd.run 'cat /root/123'
minion1:
    1
    2
    3
    4
    hello world
[root@master ~]# salt '*' file.read /root/123
minion1:
    1
    2
    3
    4
    hello world

file.readdir

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

[root@master ~]# salt '*' file.readdir /root
minion1:
    - .
    - ..
    - .bash_logout
    - .bash_profile
    - .bashrc
    - .cshrc
    - .tcshrc
    - anaconda-ks.cfg
    - .bash_history
    - .viminfo
    - abc
    - 123
    - jjyy
    - hyh
    - 456.bak
    - 456

file.remove

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

[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 20
    -rw-rwxr--+ 1 root root   20 Jul  7 22:07 123
    -rw-rwxr--+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 456.bak
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh
    drwxr-xr-x. 2 root root   19 Jul  7 18:50 jjyy
[root@master ~]# salt '*' file.remove /root/123
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 16
    -rw-rwxr--+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 456.bak
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh
    drwxr-xr-x. 2 root root   19 Jul  7 18:50 jjyy
[root@master ~]# salt '*' file.remove /root/jjyy
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 16
    -rw-rwxr--+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 456.bak
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh

file.rename

重命名文件或目录

[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 16
    -rw-rwxr--+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 456.bak
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh
[root@master ~]# salt '*' file.rename /root/456.bak /root/a
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 16
    -rw-rwxr--+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 a
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh

file.set_mode

给指定文件设置权限

[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 16
    -rw-rwxr--+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 a
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh
[root@master ~]# salt '*' file.set_mode /root/456 755
minion1:
    0755
[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 16
    -rwxr-xr-x+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 a
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh

file.symlink

给指定的文件创建软链接

[root@master ~]# salt '*' cmd.run 'ls -l /root'
minion1:
    total 16
    -rwxr-xr-x+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 a
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh
[root@master ~]# salt '*' file.symlink /root/a /opt/jj
minion1:
    True
root@master ~]# salt '*' cmd.run 'ls -l /root;ls -l /opt/'
minion1:
    total 16
    -rwxr-xr-x+ 1 root root   52 Jul  7 22:13 456
    -rw-rwxr--+ 1 root root   48 Jul  7 22:11 a
    -rw-r--r--. 1 bwxh bwxh   27 Jul  7 18:26 abc
    -rw-------. 1 root root 1092 May 11 17:16 anaconda-ks.cfg
    drw-r--r--. 2 bwxh bwxh    6 Jul  7 21:58 hyh
    total 0

file.touch

创建空文件或更新时间戳

[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    drwxr-xr-x. 2 root root 42 Jul  7 22:26 hehe
    lrwxrwxrwx. 1 root root  7 Jul  7 22:28 jj -> /root/a
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world
    -rw-r--r--. 1 root root  0 Jul  7 22:27 xxhh
[root@master ~]# salt '*' file.touch /opt/aa
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    -rw-r--r--. 1 root root  0 Jul  7 22:30 aa
    drwxr-xr-x. 2 root root 42 Jul  7 22:26 hehe
    lrwxrwxrwx. 1 root root  7 Jul  7 22:28 jj -> /root/a
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world
    -rw-r--r--. 1 root root  0 Jul  7 22:27 xxhh
[root@master ~]# salt '*' file.touch /opt/hehe
minion1:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /opt'
minion1:
    total 0
    -rw-r--r--. 1 root root  0 Jul  7 22:30 aa
    drwxr-xr-x. 2 root root 42 Jul  7 22:31 hehe
    lrwxrwxrwx. 1 root root  7 Jul  7 22:28 jj -> /root/a
    drwxr-xr-x. 2 root root  6 Jul  7 21:57 world
    -rw-r--r--. 1 root root  0 Jul  7 22:27 xxhh

file.uid_to_user

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

[root@master ~]# salt '*' file.uid_to_user 0
minion1:
    root
[root@master ~]# salt '*' file.uid_to_user 1000
minion1:
    bwxh

file.user_to_uid

将指定的用户转换成 uid 并显示出来

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

file.write

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

[root@master ~]# salt '*' cmd.run 'cat /root/456'
minion1:
    hyh
    haha
    xixi
    hyh hello hyh hello hyh
    hyh hello 666
[root@master ~]# salt '*' file.write /root/456 "hello world" "hello bwxh" "666"
minion1:
    Wrote 3 lines to "/root/456"
[root@master ~]# salt '*' cmd.run 'cat /root/456'
minion1:
    hello world
    hello bwxh
    666

以上是关于SaltStack常用的模块续的主要内容,如果未能解决你的问题,请参考以下文章

SaltStack常用的模块

SaltStack常用的模块

SaltStack常用的模块

SaltStack常用的模块

SaltStack之远程执行其它常用模块

SaltStack常用的模块