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

Saltstack of automatic operation and maintenance (3) file used by common modules

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

1 、 file.access

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

[root@server01 salt] # salt'* 'file.access / etc/passwd fserver02: Trueserver03: True [root@server01 salt] # salt' * 'file.access / etc/passwd rserver03: Trueserver02: True [root@server01 salt] # salt' * 'file.access / etc/passwd wserver02: Trueserver03: True [root@server01 salt] # salt' * 'file.access / etc/passwd xserver02: Falseserver03: False [root@server01 salt] #

2 、 file.touch

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

[root@server01 salt] # salt'* 'file.touch / opt/salt-testserver03: Trueserver02: True [root@server01 salt] # salt' * 'cmd.run' stat / opt/salt-test'server03: File:'/ opt/salt-test' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 134776101 Links: 1 Access: (0644/-rw-r--r --) Uid: (0 / root) Gid: (0 / root) Access: 2017-06-14 01 Access 58 Access 22.354868060-0400 Modify: 2017-06-14 01 Blocks 58 Blocks 22.354868060-0400 Birth:-server02: File:'/ opt/salt-test' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 803h/2051d Inode: 134326635 Links: 1 Access: (0644 Change) Uid: (0 / root) Gid: (0 / root) Access: 2017-06-14 01 Modify: 22.380650895-0400 Birth:-[root@server01 salt] #

3 、 file.append

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

[root@server01 salt] # salt'* 'file.append / opt/salt-test "salt test file001" server02: Wrote 1 lines to "/ opt/salt-test" server03: Wrote 1 lines to "/ opt/salt-test" [root@server01 salt] # salt' * 'file.append / opt/salt-test "salt test file002" server03: Wrote 1 lines to "/ opt/salt-test" server02: Wrote 1 lines to "/ opt/salt-test" [root@server01 salt] # salt'* 'file.append / opt/salt-test "salt test file003" server02: Wrote 1 lines to "/ opt/salt-test" server03: Wrote 1 lines to "/ opt/salt-test" [root@server01 salt] #

View the appended result:

[root@server01 salt] # salt'* 'cmd.run' cat / opt/salt-test'server02: salt test file001 salt test file002 salt test file003server03: salt test file001 salt test file002 salt test file003 [root@server01 salt] #

4. File.basename and file.dirname

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

[root@server01 salt] # salt'* 'file.basename / opt/salt-testserver03: salt-testserver02: salt-test [root@server01 salt] # salt' * 'file.basename / usr/local/webserverserver03: webserverserver02: webserver [root@server01 salt] #

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

[root@server01 salt] # salt'* 'file.dirname / opt/salt-testserver03: / optserver02: / opt [root@server01 salt] # salt' * 'file.dirname / usr/local/webserverserver02: / usr/localserver03: / usr/local [root@server01 salt] #

5. File.chgrp and file.chown

File.chgrp: modifying an array of files

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

File.chgrp usage:

[root@server01 salt] # salt'* 'cmd.run' ls-al / opt/salt-test'server03:-rw-r--r-- 1 root root 54 Jun 14 02:01 / opt/salt-testserver02:-rw-r--r-- 1 root root 54 Jun 14 02:01 / opt/salt-test [root@server01 salt] # salt'* 'file.chgrp / opt/salt-test nginxserver03: Noneserver02: None [root@server01 salt] # salt' * Cmd.run'ls-al / opt/salt-test'server02:-rw-r--r-- 1 root nginx 54 Jun 14 02:01 / opt/salt-testserver03:-rw-r--r-- 1 root nginx 54 Jun 14 02:01 / opt/salt-test [root@server01 salt] #

File.chown usage:

[root@server01 salt] # salt'* 'cmd.run' ls-al / opt/salt-test'server02:-rw-r--r-- 1 root nginx 54 Jun 14 02:01 / opt/salt-testserver03:-rw-r--r-- 1 root nginx 54 Jun 14 02:01 / opt/salt-test [root@server01 salt] # salt'* 'file.chown / opt/salt-test nginx nginxserver02: Noneserver03: None [root@server01 salt] # salt' * 'cmd.run' ls-al / opt/salt-test'server03:-rw-r--r-- 1 nginx nginx 54 Jun 14 02:01 / opt/salt-testserver02:-rw-r--r-- 1 nginx nginx 54 Jun 14 02:01 / opt/salt-test [root@server01 salt] #

6 、 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 the 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@server01 salt] # salt'* 'file.copy / etc/hosts / opt/hostsserver03: Trueserver02: True [root@server01 salt] # salt' * 'cmd.run' ls-al / opt/hosts'server02:-rw-r--r-- 1 root root 323 Jun 14 02:18 / opt/hostsserver03:-rw-r--r-- 1 root root 323 Jun 14 02:18 / opt/hosts [root@server01 salt] # [root@server01 salt] # salt' * 'file.copy / var/spool/cron/ / tmp/ recurse=Trueserver03: Trueserver02: True [root@server01 salt] # salt' * 'cmd.run' ls-1 / tmp/'server03: total 4-rw-r--r-- 1 root root 52 Jun 14 02:21 rootserver02: total 4-rw-r--r-- 1 root root 52 Jun 14 02:21 root [root@server01 salt] #

[root@server01 salt] # salt'* 'file.copy / var/spool/cron/ / tmp/ recurse=True remove_existing=True

Server03:

ERROR: Could not copy'/ var/spool/cron/' to'/ tmp/'

Server02:

ERROR: Could not copy'/ var/spool/cron/' to'/ tmp/'

[root@server01 salt] #

Add remove_existing=True parameters to report an error, read some materials, do not know the corresponding explanation, do not know if anyone knows what went wrong, wait for instructions!

7 、 file.diskusage

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

[root@server01 salt] # salt'* 'file.diskusage / etc/passwdserver03: 1126server02: 1126 [root@server01 salt] # salt' * 'file.diskusage / etc/shadowserver03: 708server02: 708 [root@server01 salt] #

8 、 file.find

File,find: returns the file path of the specified search criteria, which is compatible with the find command in Linux and the parameter find command.

[root@server01 salt] # salt'* 'file.find / etc/ name=minionserver03:-/ etc/salt/minion-/ etc/salt/pki/minionserver02:-/ etc/salt/minion-/ etc/salt/pki/minion [root@server01 salt] #

9. File.get_gid, file.get_group, file.get_uid and file.get_user respectively represent the array ID that returns the specified file, the array of the specified file, the owner id of the specified file, and the genus of the specified file

[root@server01 salt] # salt'* 'file.get_gid / etc/shadowserver02: 0server03: 0 [root@server01 salt] # salt' * 'file.get_group / etc/shadowserver03: rootserver02: root [root@server01 salt] # salt' * 'file.get_uid / etc/shadowserver03: 0server02: 0 [root@server01 salt] # salt' * 'file.get_user / etc/shadowserver02: rootserver03: root [root@server01 salt] #

10 、 file.grep

File.grep: returns the lookup string in the specified file. Similar to the grep command under Linux, the parameters are compatible with the grep command.

[root@server01 salt] # salt'* 'file.grep / etc/passwd sshserver03:-pid: 28202 retcode: 0 stderr: stdout: sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinserver02:-pid: 13034 retcode: 0 stderr: Stdout: sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [root@server01 salt] # [root@server01 salt] # salt'* 'file.grep / etc/sysconfig/network-scripts/ifcfg-ens33 DNS "- I" server02:-pid: 13096 retcode: 0 stderr: stdout: PEERDNS=yes IPV6_PEERDNS=yes DNS1=114.114.114.114server03:-pid: 28286 retcode: 0 stderr: stdout: PEERDNS=yes IPV6_PEERDNS=yes DNS1=114.114.114.114 [root@server01 salt] #

11 、 file.makdirs

File.makedirs: to create a directory, you need to make sure that the path contained in the directory is available. Note that you must add "/" at the end of the path, otherwise it will be treated as a parent directory, such as passing in / tmp/pfile. Will be treated as / tmp/, while incoming / 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. If you pass in multiple parameter defaults to deal with the first parameter, you can create a multi-level directory in case the parent directory does not exist.

[root@server01 salt] # salt'* 'file.makedirs / tmp/pfileserver03: Directory' / tmp' already existsserver02: Directory'/ tmp' already exists [root@server01 salt] # salt'* 'file.makedirs / tmp/pfile/server02: Noneserver03: None [root@server01 salt] # salt' * 'cmd.run' ls-l / tmp/'server03: total 0 drwxr-xr-x 2 root root 6 Jun 14 04:43 pfileserver02: total 0 drwxr-xr- X 2 root root 6 Jun 14 04:43 pfile [root@server01 salt] # [root@server01 salt] # salt'* 'file.makedirs / tmp/test01/test02/test03/test04/server03: Noneserver02: None [root@server01 salt] # salt' * 'cmd.run' tree / tmp/'server03: / tmp/ |-- pfile `--test01`-- test02 `--test03`-- test04 5 directories 0 filesserver02: / tmp/ |-- pfile `--test01`-- test02 `--test03`-- test04 5 directories, 0 files [root@server01 salt] #

12 、 file.mkdir

File.mkdir: make sure a directory is available, and if so, create a directory. Unlike the file.makedirs above, the parameter can be ended without a "/" or created successfully. In addition, the module supports batch creation of multiple directories as well as multi-level directories

[root@server01 salt] # salt'* 'file.mkdir / tmp/saltdirserver03: Noneserver02: None [root@server01 salt] # salt' * 'cmd.run' ls-l / tmp'server03: total 0 drwxr-xr-x 2 root root 6 Jun 14 04:43 pfile drwxr-xr-x 2 root root 6 Jun 14 04:49 saltdir drwxr-xr-x 3 root root 20 Jun 14 04:44 test01server02: total 0 drwxr-xr-x 2 root root 6 Jun 14 04: 43 pfile drwxr-xr-x 2 root root 6 Jun 14 04:49 saltdir drwxr-xr-x 3 root root 20 Jun 14 04:44 test01 [root@server01 salt] # salt'* 'file.mkdir / tmp/saltdir01 / tmp/saltdir02 / tmp/saltdir03server02: Noneserver03: None [root@server01 salt] # salt' * 'file.mkdir / tmp/pfile/saltfileserver03: Noneserver02: None [root@server01 salt] # salt' * 'cmd.run' ls-l / tmp/saltdir*' Server02: / tmp/saltdir: total 0 / tmp/saltdir01: total 0server03: / tmp/saltdir: total 0 / tmp/saltdir01: total 0 [root@server01 salt] # salt'* 'cmd.run' ls-ld / tmp/pfile/saltfile'server03: drwxr-xr-x 2 root root 6 Jun 14 04:50 / tmp/pfile/saltfileserver02: drwxr-xr-x 2 root root 6 Jun 14 04:50 / tmp/pfile/saltfile [root@server01 salt] #

13 、 file.move

File.move: move a file or directory

[root@server01 salt] # salt'* 'file.move / tmp/pfile/saltfile / tmp/move-saltfileserver02:-comment:' / tmp/pfile/saltfile' moved to'/ tmp/move-saltfile' result: Trueserver03:-comment:'/ tmp/pfile/saltfile' moved to'/ tmp/move-saltfile' result: True [root@server01 salt] # salt'* 'cmd.run' ls-ld / tmp/move-saltfile'server03: drwxr-xr-x 2 root root 6 Jun 14 04:50 / tmp/move-saltfileserver02: drwxr-xr-x 2 root root 6 Jun 14 04:50 / tmp/move-saltfile [root@server01 salt] #

14 、 file.remove

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

[root@server01 salt] # salt'* 'file.remove / tmp/test01server02: Trueserver03: True [root@server01 salt] # salt' * 'cmd.run' ls-l / tmp/'server02: total 0 drwxr-xr-x 2 root root 6 Jun 14 04:50 move-saltfile drwxr-xr-x 2 root root 6 Jun 14 04:54 pfile drwxr-xr-x 2 root root 6 Jun 14 04:49 saltdir drwxr-xr-x 2 root root 6 Jun 14 04:50 saltdir01server03 : total 0 drwxr-xr-x 2 root root 6 Jun 14 04:50 move-saltfile drwxr-xr-x 2 root root 6 Jun 14 04:54 pfile drwxr-xr-x 2 root root 6 Jun 14 04:49 saltdir drwxr-xr-x 2 root root 6 Jun 14 04:50 saltdir01 [root@server01 salt] #

15 、 file.rename

File.rename: rename a file or directory

[root@server01 salt] # salt'* 'file.rename / tmp/saltdir / tmp/saltdir-renameserver03: Trueserver02: True [root@server01 salt] # salt' * 'cmd.run' ls-l / tmp/'server02: total 4 drwxr-xr-x 2 root root 6 Jun 14 04:50 move-saltfile drwxr-xr-x 2 root root 6 Jun 14 04:54 pfile-rw-r--r-- 1 root root 9 Jun 14 05:01 salt-test-file Drwxr-xr-x 2 root root 6 Jun 14 04:49 saltdir-rename drwxr-xr-x 2 root root 6 Jun 14 04:50 saltdir01server03: total 4 drwxr-xr-x 2 root root 6 Jun 14 04:50 move-saltfile drwxr-xr-x 2 root root 6 Jun 14 04:54 pfile-rw-r--r-- 1 root root 9 Jun 14 05:01 salt-test-file drwxr-xr-x 2 root root 6 Jun 14 04:49 saltdir-rename drwxr-xr- X 2 root root 6 Jun 14 04:50 saltdir01 [root@server01 salt] # salt'* 'file.rename / tmp/salt-test-file / tmp/salt-test-file-renameserver03: Trueserver02: True [root@server01 salt] # salt' * 'cmd.run' ls-l / tmp/'server03: total 4 drwxr-xr-x 2 root root 6 Jun 14 04:50 move-saltfile drwxr-xr-x 2 root root 6 Jun 14 04:54 pfile-rw-r- -root root-1 root root 9 Jun 14 05:01 salt-test-file-rename drwxr-xr-x 2 root root 6 Jun 14 04:49 saltdir-rename drwxr-xr-x 2 root root 6 Jun 14 04:50 saltdir01server02: total 4 drwxr-xr-x 2 root root 6 Jun 14 04:50 move-saltfile drwxr-xr-x 2 root root 6 Jun 14 04:54 pfile-rw-r--r-- 1 root root 9 Jun 14 05:01 salt-test-file-rename Drwxr-xr-x 2 root root 6 Jun 14 04:49 saltdir-rename drwxr-xr-x 2 root root 6 Jun 14 04:50 saltdir01 [root@server01 salt] #

16 、 file.stats

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

[root@server01 salt] # salt'* 'file.stats / etc/shadowserver03:-atime: 1497423661.56 ctime: 1497420465.01 gid: 0 group: root inode: 67109282 mode: 0 mtime: 1497420465.01 size: 708 target: / etc/shadow type: File uid: 0 user: rootserver02:-atime: 1497423661.24 ctime: 1497420465.01 gid: 0 group: root inode: 67109305 mode: 0 mtime: 1497420465.01 size: 708 target: / etc/shadow type: file Uid: 0 user: root [root@server01 salt] #

17 、 file.rmdir

File.rmdir: delete the specified empty directory. If the directory is not empty, the execution fails, that is, a failure is returned.

[root@server01 salt] # salt'* 'file.rmdir / tmp/pfileserver02: Trueserver03: True [root@server01 salt] # salt' * 'file.rmdir / tmp/saltdir01/server03: Directory not emptyserver02: Directory not empty [root@server01 salt] # salt' * 'file.rmdir / tmp/saltdir01server02: Directory not emptyserver03: Directory not empty [root@server01 salt] #

18 、 file.search

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

[root@server01 salt] # salt'* 'file.search / etc/passwd rootserver03: Trueserver02: True [root@server01 salt] # salt' * 'file.search / etc/passwd' ssh'server02: Trueserver03: True [root@server01 salt] #

19 、 file.readdir

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

[root@server01 salt] # salt'* 'file.readdir / tmp/saltdir01server03: -. -. Salt-test-file-renameserver02: -. -. -salt-test-file-rename [root@server01 salt] # salt'* 'cmd.run' ls-l / tmp/saltdir01/'server02: total 4-rw-r--r-- 1 root root 9 Jun 14 05:08 salt-test-file-renameserver03: total 4-rw-r--r-- 1 root root 9 Jun 14 05:08 salt-test-file-rename [root@server01 salt] #

20 、 file.managed

When there are file divisions in the system that are frequently modified on hundreds of machines, using saltstack's file.managed to manage file status will make it much easier for you. It allows you to complete all these changes with one click to reach the file state you want.

[root@salt-master base] # tree. ├── system │ ├── files │ │ └── hosts └── hosts.sls └── top.sls 2 directories, 3files [root@salt-master base] # cat top.sls base: # the environment specified here is base So the top.sls in the / srv/salt/base directory "*": # "*" means all hosts. Specify a single host to write "salt-minion1"-system.hosts # directly, which refers to those sls configuration files that are called. "Point" here is the directory rating # that is, system does not have a hosts.sls configuration file [root@salt-masterbase] # cat system/hosts.sls/etc/hosts: # this is the configuration ID and file storage location Is non-repeatable file.managed: # the file management module of "file.managed" salt is called here-source: salt://system/files/hosts # source is the specified file source "salt://" refers to the user group of file-mode: 644 # file permissions 644-user: root # file owner-group: root # file under the salt project

The status configuration of the above file is explained in detail:

[root@salt-master system] # salt 'salt-minion1' state.highstate # salt' execution Node 'execution module below is the execution result salt-minion1: # execution node-ID: / etc/hosts # configuration ID Function: file.managed # module Result: True # execution result True: successful Comment: File / etc/hosts updated # File execution operation [Update] Started: 14 Started 15File 55.120499 # start time of command execution Duration: 24.284 ms # length of execution Changes: # whether it is generated Change-diff:-- + + @ @-1 localhost localhost.localdomainlocalhost6 localhost6.localdomain 2 + 1 localhost localhost.localdomainlocalhost6 localhost6.localdomain 4 @ @ 127.0.0.1 + salt-master 10.0.0.11 # is added on this line with a "+" sign The "-" sign is a reduction of line + salt-minion1 10.0.0.21 Summary-Succeeded: 1 (changed=1) # executed successfully There is a file status change Failed: 0 # execution failed 0-Total states run: 1 # number of execution states

Salt command to manage files

Sls file writing of salt "*" file.managed / etc/zabbix/zabbix_agentd.conf salt://file/zabbix_agentd.conf root root 755salt / etc/zabbix/zabbix_agentd.conf: file.managed:- source: salt://file/zabbix_agentd.conf- user: root- group: root- mode: 755 directory management file.directory

/ etc/zabbix/zabbix_agentd.conf.d: file.directory:- user: root- group: root- mode: 755-makedirs: Ture # if this directory user does not exist automatic creation-recurse: # you can use this-user-group-mode if you want to force recursion of attribution array permissions to files in the folder

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: 0

*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