🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
,1、file.access 测试salt进程对指定文件是否有访问权限 [admin@master ~ ]$ sudo salt -L 'node2.51yuki.cn,node3.51yuki.cn' file.access /etc/passwd r node2.51yuki.cn: True node3.51yuki.cn: True [admin@master ~ ]$ sudo salt -L 'node2.51yuki.cn,node3.51yuki.cn' file.access /etc/passwd w node3.51yuki.cn: True node2.51yuki.cn: True [admin@master ~ ]$ sudo salt -L 'node2.51yuki.cn,node3.51yuki.cn' file.access /etc/passwd x node3.51yuki.cn: False node2.51yuki.cn: False 2、file.touch:如果文件不存在创建文件,相当于touch file,如果存在就更新访问时间或者修改时间 [admin@master ~ ]$ sudo salt '*' file.touch /tmp/aa.txt node2.51yuki.cn: True node3.51yuki.cn: True 3、file.append:向文件末尾追加内容 [admin@master ~ ]$ sudo salt '*' file.append /tmp/aa.txt "this is file" node2.51yuki.cn: Wrote 1 lines to "/tmp/aa.txt" node3.51yuki.cn: Wrote 1 lines to "/tmp/aa.txt" [admin@master ~ ]$ sudo salt '*' file.append /tmp/aa.txt "this is one\nthis is two" node2.51yuki.cn: Wrote 1 lines to "/tmp/aa.txt" node3.51yuki.cn: Wrote 1 lines to "/tmp/aa.txt" [admin@master ~ ]$ sudo salt '*' cmd.run 'cat /tmp/aa.txt' node3.51yuki.cn: this is file this is one\nthis is two node2.51yuki.cn: this is file this is one\nthis is two (发现\n不会换行) 4、file.basename:返回给定路径的最后一部分 [admin@master ~ ]$ sudo salt '*' file.basename /etc/sysconfig/network node3.51yuki.cn: network node2.51yuki.cn: network file.dirname:返回指定路径的目录部分 [admin@master ~ ]$ sudo salt '*' file.dirname /etc/sysconfig/network node2.51yuki.cn: /etc/sysconfig node3.51yuki.cn: /etc/sysconfig 5、file.chown:修改文件的属主和数组 [admin@master ~ ]$ sudo salt '*' cmd.run 'ls -l /tmp/aa.txt' node2.51yuki.cn: -rw-r--r--. 1 root root 38 Jan 20 18:00 /tmp/aa.txt node3.51yuki.cn: -rw-r--r--. 1 root root 38 Jan 20 18:00 /tmp/aa.txt [admin@master ~ ]$ sudo salt '*' file.chown /tmp/aa.txt admin admin node2.51yuki.cn: None node3.51yuki.cn: None [admin@master ~ ]$ sudo salt '*' cmd.run 'ls -l /tmp/aa.txt' node3.51yuki.cn: -rw-r--r--. 1 admin admin 38 Jan 20 18:00 /tmp/aa.txt node2.51yuki.cn: -rw-r--r--. 1 admin admin 38 Jan 20 18:00 /tmp/aa.txt 6、file.copy:从源目录拷贝文件到目标目录。如果要拷贝目录,需要添加recurse标签,默认情况下会覆盖目标目录中的相同路径的文件,并保留其他文件。remove_existing选项会提前移除目标目录中的所有文件,然后再从源路径拷贝文件到目标路径 [admin@master ~ ]$ sudo salt '*' file.copy /server/scripts/ /tmp/ recurse=True node3.51yuki.cn: True node2.51yuki.cn: True [admin@master ~ ]$ sudo salt '*' cmd.run 'ls -l /tmp' node3.51yuki.cn: total 8 -rw-r--r--. 1 admin admin 0 Jan 20 18:18 aa.txt -rw-r--r--. 1 root root 99 Jan 20 18:08 admin -rw-r--r--. 1 root root 0 Jan 20 18:18 bb.txt -rwx------. 1 root root 836 Jan 13 11:48 ks-script-sVgk05 drwx------. 3 root root 17 Jan 14 21:15 systemd-private-4881e6439b494b34b282c1025d358e5d-httpd.service-JHfYxU -rw-------. 1 root root 0 Jan 13 11:33 yum.log node2.51yuki.cn: total 8 -rw-r--r--. 1 admin admin 0 Jan 20 18:18 aa.txt -rw-r--r--. 1 root root 170 Jan 20 18:08 admin -rw-r--r--. 1 root root 0 Jan 20 18:18 bb.txt -rwx------. 1 root root 836 Jan 10 17:35 ks-script-bGSs8n drwx------. 3 root root 17 Jan 20 08:38 systemd-private-a2123c561d284a37960b7e710477431a-httpd.service-Ry1lvq -rw-------. 1 root root 0 Jan 10 17:23 yum.log [admin@master ~ ]$ (把目标服务器上/server/scripts目录里的文件拷贝到tmp目录下,如果是目录,recurse要配置成True) 7、file,find:返回指定搜索条件的文件路径,与Linux中的find命令,参数也兼容find命令 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 CLI Examples: salt '*' file.find / type=f name=\*.bak size=+10m 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 8、file.mkdir:确认一个目录是否可用,如果可以用,就创建目录 [admin@master ~ ]$ sudo salt '*' file.mkdir '/tmp/test' node2.51yuki.cn: True node3.51yuki.cn: True [admin@master ~ ]$ sudo salt '*' cmd.run 'ls -ld /tmp/test' node3.51yuki.cn: drwxr-xr-x. 2 root root 6 Jan 20 18:40 /tmp/test node2.51yuki.cn: drwxr-xr-x. 2 root root 6 Jan 20 18:40 /tmp/test (file模块还有很多很多很多函数,我们可以通过salt '*' sys.list_functions file查看)