SaltStack常用的模块-3

SaltStack常用的模块

2.6 SaltStack常用模块之file

2.6.11 file.file_exists

判断指定文件是否存在

[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.69.202:
    total 12
    -rw-r--r--. 1 root root   22 Feb 23 16:51 a
    drwxr-xr-x. 4 root root  165 Feb 19 09:20 abc
    -rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
    -rw-r--r--. 1 root root   22 Feb 24 14:57 cc
[root@master ~]# salt '*' file.file_exists /root/a
192.168.69.202:
    True
[root@master ~]# salt '*' file.file_exists /root/abc
192.168.69.202:
    False   //返回False是因为abc是目录而非文件

2.6.12 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

2.6.13 file.get_gid

获取指定文件的gid

[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.69.202:
    -rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt '*' file.get_gid /root/a
192.168.69.202:
    0

2.6.14 file.get_group

获取指定文件的组名

[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.69.202:
    -rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt '*' file.get_group /root/a
192.168.69.202:
    root

2.6.15 file.get_hash

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

[root@master ~]# salt '*' cmd.run 'sha256sum /root/a'
192.168.69.202:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a  /root/a
[root@master ~]# salt '*' file.get_hash /root/a
192.168.69.202:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a

2.6.16 file.get_mode

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.69.202:
    -rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt '*' file.get_mode /root/a
192.168.69.202:
    0644

2.6.17 file.get_selinux_context

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

[root@master ~]# salt '*' cmd.run 'ls -Z /root/a'
192.168.69.202:
    -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /root/a
[root@master ~]# salt '*' file.get_selinux_context /root/a
192.168.69.202:
    unconfined_u:object_r:admin_home_t:s0

2.6.18 file.get_sum

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

  • md5
  • sha1
  • sha224
  • sha256 (default)
  • sha384
  • sha512
[root@master ~]# salt '*' cmd.run 'sha256sum /root/a'
192.168.69.202:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a  /root/a
[root@master ~]# salt '*' file.get_sum /root/a
192.168.69.202:
    11129dfb248c6bc5784c1d439877552aa34f3408f14dbb38572e802e4831b77a
[root@master ~]# salt '*' cmd.run 'md5sum /root/a'
192.168.69.202:
    671ded4ec86c82a8779c8df17823f810  /root/a
[root@master ~]# salt '*' file.get_sum /root/a md5
192.168.69.202:
    671ded4ec86c82a8779c8df17823f810

2.6.19 file.get_uid与file.get_user

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

[root@master ~]# salt '*' cmd.run 'ls -l /root/a'
192.168.69.202:
    -rw-r--r--. 1 root root 22 Feb 23 16:51 /root/a
[root@master ~]# salt '*' file.get_uid /root/a
192.168.69.202:
    0
[root@master ~]# salt '*' file.get_user /root/a
192.168.69.202:
    root

2.6.20 file.gid_to_group

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

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

2.6.21 file.group_to_gid

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

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

2.6.22 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.23 file.is_blkdev

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

[root@master ~]# salt '*' cmd.run 'ls -l /dev/sr0'
192.168.69.202:
    brw-rw----. 1 root cdrom 11, 0 Feb 23 17:13 /dev/sr0
[root@master ~]# salt '*' file.is_blkdev /dev/sr0
192.168.69.202:
    True

2.6.24 file.lsattr

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

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

2.6.25 file.mkdir

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

[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.69.202:
    total 16
    -rw-r--r--. 1 root root   26 Feb 24 16:16 a
    -rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Feb 24 16:16 cc
    -rw-r--r--. 1 root root   26 Feb 24 16:18 cca
[root@master ~]# salt '*' file.mkdir /root/abc
192.168.69.202:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root'
192.168.69.202:
    total 16
    -rw-r--r--. 1 root root   26 Feb 24 16:16 a
    drwxr-xr-x. 2 root root    6 Feb 24 17:05 abc
    -rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Feb 24 16:16 cc
    -rw-r--r--. 1 root root   26 Feb 24 16:18 cca
    
[root@master ~]# salt '*' file.mkdir /root/haha tom tom 400
192.168.69.202:
    True
[root@master ~]# salt '*' cmd.run 'ls -l /root/'
192.168.69.202:
    total 16
    -rw-r--r--. 1 root root   26 Feb 24 16:16 a
    drwxr-xr-x. 2 root root    6 Feb 24 17:05 abc
    -rw-------. 1 root root 1259 Feb 24 15:00 anaconda-ks.cfg
    -rw-r--r--. 1 root root   26 Feb 24 16:16 cc
    -rw-r--r--. 1 root root   26 Feb 24 16:18 cca
    dr--------. 2 tom  tom     6 Feb 24 17:07 haha
发布了81 篇原创文章 · 获赞 12 · 访问量 4043

猜你喜欢

转载自blog.csdn.net/qq_43141726/article/details/104137589