Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

File related modules of saltstack module

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1 、 file.access

File.access: test whether the salt process has the corresponding access to the specified file.

[root@salt-master ~] # salt'* 'file.access / etc/passwd fsalt-minion02.contoso.com: Truesalt-minion01.contoso.com: True [root@salt-master ~] # salt' * 'file.access / etc/passwd rsalt-minion02.contoso.com: Truesalt-minion01.contoso.com: True [root@salt-master ~] # salt' * 'file.access / etc/passwd wsalt-minion01.contoso.com: Truesalt-minion02.contoso.com: True [root@salt-master ~] # salt'* 'file.access / etc/passwd xsalt-minion02.contoso.com: Falsesalt-minion01.contoso.com: False

2 、 file.touch

File.touch: if the file does not exist, it is equivalent to touch file. If the file exists, update the access time or modification time.

[root@salt-master ~] # salt'* 'file.touch / tmp/salt-testsalt-minion02.contoso.com: Truesalt-minion01.contoso.com: True

3 、 file.append

File.append: appends text to the end of the file.

[root@salt-master ~] # salt'* 'file.append / tmp/sal-test "Hello World. "salt-minion01.contoso.com: Wrote 1 lines to" / tmp/sal-test "salt-minion02.contoso.com: Wrote 1 lines to" / tmp/sal-test "[root@salt-master ~] # salt'* 'file.append / tmp/sal-test args='Hello=Hi'salt-minion01.contoso.com: Wrote 1 lines to" / tmp/sal-test "salt-minion02.contoso.com: Wrote 1 lines to" / tmp/sal-test "[root@ Salt-master ~] # salt'* 'file.append / tmp/sal-test args= "[' true=1'" 'false=0'] "salt-minion01.contoso.com: Wrote 2 lines to" / tmp/sal-test "salt-minion02.contoso.com: Wrote 2 lines to" / tmp/sal-test "

4 、 file.basename

File.basename: returns the last part of the given path.

[root@salt-master ~] # salt'* 'file.basename / tmp/sal-testsalt-minion02.contoso.com: sal-testsalt-minion01.contoso.com: sal-test

5 、 file.chgrp

File.chgrp: modify the group of files.

[root@salt-master ~] # salt'* 'file.chgrp / tmp/testfile nginxsalt-minion01.contoso.com: Nonesalt-minion02.contoso.com: None [root@salt-master ~] # salt' * 'cmd.run "ls-l / tmp/testfile" salt-minion01.contoso.com:-rw-r--r-- 1 root nginx 0 Jun 4 01:27 / tmp/testfilesalt-minion02.contoso.com:-rw-r--r-- 1 root nginx 0 Jun 4 01:27 / tmp/testfile

6 、 file.chown

File.chown: modify the owner and group of the file.

[root@salt-master ~] # salt'* 'file.chown / tmp/testfile nginx nginxsalt-minion01.contoso.com: Nonesalt-minion02.contoso.com: None [root@salt-master ~] # salt' * 'cmd.run "ls-l / tmp/testfile" salt-minion02.contoso.com:-rw-r--r-- 1 nginx nginx 0 Jun 4 01:27 / tmp/testfilesalt-minion01.contoso.com:-rw-r--r-- 1 nginx nginx 0 Jun 4 01:27 / tmp/testfile

7 、 file.copy

File.copy: copy files from the source directory to the destination directory. If you want to copy a directory, you need to add a recurse tag, which by default overwrites files in the same path in the target directory and retains other files. The remove_existing option removes all files in the destination directory ahead of time, and then copies the files from the source path to the destination path.

[root@salt-master ~] # salt'* 'file.copy / etc/hosts / tmp/hostssalt-minion01.contoso.com: Truesalt-minion02.contoso.com: True [root@salt-master ~] # salt' * 'cmd.run' ls-l / tmp/hosts'salt-minion01.contoso.com:-rw-r--r-- 1 root root 327 Jun 4 01:41 / tmp/hostssalt-minion02.contoso.com:-rw-r--r-- 1 Root root 330Jun 4 01:41 / tmp/hosts [root@salt-master ~] # salt'* 'file.copy / var/spool/cron/ / tmp/ recurse=Truesalt-minion01.contoso.com: Truesalt-minion02.contoso.com: True [root@salt-master ~] # salt' * 'cmd.run' ls-l / tmp/root'salt-minion02.contoso.com:-rw-r--r-- 1 root root 89 Jun 4 01:46 / tmp/rootsalt-minion01. Contoso.com:-rw-r--r-- 1 root root 73 Jun 4 01:46 / tmp/root [root@salt-master ~] # salt'* 'file.copy / var/spool/cron/ / tmp/ recurse=True remove_existing=Truesalt-minion02.contoso.com: Truesalt-minion01.contoso.com: True [root@salt-master] # salt' * 'cmd.run' ls-1 / tmp'salt-minion01.contoso.com: total 4-rw-r -- root root-1 root root 73 Mar 11 21:35 rootsalt-minion02.contoso.com: total 4-rw- 1 root root 89 May 25 13:32 root

8 、 file.dirname

File.dirname: returns the directory portion of the specified path.

[root@salt-master ~] # salt'* 'file.dirname / etc/passwdsalt-minion01.contoso.com: / etcsalt-minion02.contoso.com: / etc

9 、 file.diskusage

File.diskusage: recursively calculates the disk space occupied by the specified directory and returns the calculated value in bytes.

[root@salt-master ~] # salt'* 'file.diskusage / root/install.logsalt-minion01.contoso.com: 30131salt-minion02.contoso.com: 30131

10 、 file.file_exists

File.file_exists: test whether the target path is a valid file, and the return value is True or False.

[root@salt-master ~] # salt'* 'file.file_exists / etc/passwdsalt-minion02.contoso.com: Truesalt-minion01.contoso.com: True

11 、 file.find

File.find: returns the file path returned by the specified search criteria, which is equivalent to the find command in Linux, and the parameters are also compatible with the find command.

[root@salt-master ~] # salt 'salt-minion01.contoso.com' file.find / var name=minionsalt-minion01.contoso.com:-/ var/cache/salt/minion-/ var/log/salt/minion-/ var/run/salt/minion [root@salt-master ~] # salt' salt-minion01.contoso.com' file.find / var name=minion size=-1m salt-minion01.contoso.com:-/ var/cache/salt/minion- / var/log/salt/minion-/ var/run/salt/minion [root@salt-master ~] # salt 'salt-minion01.contoso.com' file.find / var name=minion size=-1m print=path Size,mtimesalt-minion01.contoso.com: | _-/ var/cache/salt/minion-4096-1495711378 | _-/ var/log/salt/minion-1123-1496551874 | _-/ var/run/salt/minion-4096-1496544267

12 、 file.get_gid

File.get_gid: returns the id of the group of the given file.

[root@salt-master ~] # salt'* 'file.get_gid / etc/passwdsalt-minion02.contoso.com: 0salt-minion01.contoso.com: 0

13 、 file.get_group

File.get_group: returns the subordinate group of the given file.

[root@salt-master ~] # salt'* 'file.get_group / etc/passwdsalt-minion02.contoso.com: rootsalt-minion01.contoso.com: root

14 、 file.get_uid

File.get_uid: returns the id of the owner of the given file.

[root@salt-master ~] # salt'* 'file.get_uid / etc/passwdsalt-minion01.contoso.com: 0salt-minion02.contoso.com: 0

15 、 file.get_user

File.get_user: returns the owner of the given file.

[root@salt-master ~] # salt'* 'file.get_user / etc/passwdsalt-minion02.contoso.com: rootsalt-minion01.contoso.com: root

16 、 file.grep

File.grep: finds a string from the specified file, which is equivalent to the grep command in Linux, and the parameters are also compatible with the grep command.

[root@salt-master ~] # salt 'salt-minion01.contoso.com' file.grep / etc/passwd nobodysalt-minion01.contoso.com:-pid: 1858 retcode: 0 stderr: stdout: nobody:x:99:99:Nobody:/:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin [root@salt -master ~] # salt'* 'file.grep / etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr "- I" salt-minion01.contoso.com:-pid: 1896 retcode: 0 stderr: stdout: IPADDR=192.168.49.101salt-minion02.contoso.com:-pid: 1770 retcode: 0 stderr: stdout: IPADDR=192.168.49.102

17 、 file.makedirs

File.makedirs: to create a directory, you need to make sure that the path contained in the directory is available. Note that the path must add "/" at the end, otherwise it will be treated as a parent directory, for example, if / tmp/pfile is passed in, it will be treated as / tmp/, while / tmp/pfile/ will be treated as / tmp/pfile/. In addition, although the module name contains dirs, it is not possible to create multiple directories in batches, and if you pass in multiple parameters, only the first parameter is processed by default. However, you can create multi-level directories, even if the parent directory does not exist.

[root@salt-master ~] # salt'* 'file.makedirs / tmp/pfilesalt-minion01.contoso.com: Directory' / tmp' already existssalt-minion02.contoso.com: Directory'/ tmp' already exists [root@salt-master ~] # salt'* 'file.makedirs / tmp/pfile/salt-minion02.contoso.com: Nonesalt-minion01.contoso.com: None [root@salt-master ~] # salt' * 'cmd.run' ls-l / tmp/'salt- Minion01.contoso.com: total 8 drwxr-xr-x 2 root root 4096 Jun 4 10:48 pfile-rw-r--r-- 1 root root 73 Mar 11 21:35 rootsalt-minion02.contoso.com: total 8 drwxr-xr-x 2 root root 4096 Jun 4 10:48 pfile-rw- 1 root root 89 May 25 13:32 root [root@salt-master ~] # salt'* file.makedirs / tmp/dic1/dic2/ Dic3/salt-minion02.contoso.com: Nonesalt-minion01.contoso.com: None [root@salt-master ~] # salt'* 'cmd.run' tree / tmp'salt-minion01.contoso.com: / tmp |-- dic1 | `--dic2 |`-- dic3 |-- pfile |-- root `--salt 5 directories 1 filesalt-minion02.contoso.com: / tmp |-- dic1 | `--dic2 |`-- dic3 |-- pfile |-- root `--salt 5 directories, 1 file

18 、 file.mkdir

File.mkdir: confirm that a directory is available, that is, create the specified directory. Unlike file.makedirs, parameters can be created successfully without a "/" at the end. In addition, the module supports batch creation of multiple directories as well as multi-level directories.

[root@salt-master ~] # salt'* 'file.mkdir / tmp/saltsalt-minion02.contoso.com: Nonesalt-minion01.contoso.com: None [root@salt-master ~] # salt' * 'cmd.run' ls-l / tmp'salt-minion01.contoso.com: total 12 drwxr-xr-x 2 root root 4096 Jun 4 10:48 pfile-rw-r--r-- 1 root root 73 Mar 11 21:35 root drwxr-xr-x 2 Root root 4096 Jun 4 11:02 salt [root@salt-master ~] # salt'* 'file.mkdir / tmp/salt1/slat2/salt3salt-minion02.contoso.com: Nonesalt-minion01.contoso.com: None [root@salt-master ~] # salt' * 'file.mkdir / tmp/test1 / tmp/test2 / tmp/test3salt-minion02.contoso.com: Nonesalt-minion01.contoso.com: None [root@salt-master ~] # salt' * 'file.mkdir / tmp/ Pfile/testsalt-minion01.contoso.com: Nonesalt-minion02.contoso.com: None

19 、 file.move

File.move: move a file or directory.

[root@salt-master] # salt'* 'file.move / tmp/pfile/test / tmp/salt01/salt-minion01.contoso.com:-comment:' / tmp/pfile/test' moved to'/ tmp/salt01/' result: Truesalt-minion02.contoso.com:-comment:'/ tmp/pfile/test' moved to'/ Tmp/salt01/' result: True

20 、 file.remove

File.remove: delete files. Note: this module can only accept one parameter at a time.

[root@salt-master ~] # salt'* 'file.remove / tmp/dic1/salt-minion02.contoso.com: Truesalt-minion01.contoso.com: True [root@salt-master ~] # salt' * 'file.remove / tmp/test1/salt-minion02.contoso.com: Truesalt-minion01.contoso.com: True

21 、 file.rename

File.rename: rename a file or directory.

[root@salt-master ~] # salt'* 'file.rename / tmp/salt1 / tmp/salt01salt-minion01.contoso.com: Truesalt-minion02.contoso.com: True

22 、 file.stats

File.stats: returns a dictionary containing the status of the specified file.

[root@salt-master ~] # salt 'salt-minion01.contoso.com' file.stats / etc/passwdsalt-minion01.contoso.com:-atime: 1496464651.48 ctime: 1489237646.83 gid: 0 group: root inode: 394628 mode: 0644 mtime: 1489237646.82 size: 1465 target: / etc/passwd type: file uid: 0 user: root

23 、 file.rmdir

File.rmdir: delete the specified directory and return a failure if the directory is not empty.

[root@salt-master ~] # salt'* 'file.rmdir / tmp/salt01salt-minion02.contoso.com: Directory not emptysalt-minion01.contoso.com: Directory not empty [root@salt-master ~] # salt' * 'file.rmdir / tmp/saltsalt-minion02.contoso.com: Truesalt-minion01.contoso.com: True

24 、 file.search

File.search: searches for whether the pattern parameter appears in the specified file.

[root@salt-master ~] # salt'* 'file.search / etc/passwd' nginx'salt-minion02.contoso.com: Truesalt-minion01.contoso.com: True [root@salt-master ~] # salt'* 'file.search / etc/passwd' test'salt-minion02.contoso.com: Falsesalt-minion01.contoso.com: False

25 、 file.readdir

File.readdir: returns a list containing the contents of a directory.

[root@salt-master] # salt'* 'file.readdir / tmp/pfilesalt-minion02.contoso.com: -. -. Testsalt-minion01.contoso.com: -. -. -test

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 257

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report