自动化运维之saltstack常用模块使用之file
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动化运维之saltstack常用模块使用之file相关的知识,希望对你有一定的参考价值。
1、file.access
file.access:测试salt进程对指定文件是否有访问权限
[[email protected] salt]# salt ‘*‘ file.access /etc/passwd f server02: True server03: True [[email protected] salt]# salt ‘*‘ file.access /etc/passwd r server03: True server02: True [[email protected] salt]# salt ‘*‘ file.access /etc/passwd w server02: True server03: True [[email protected] salt]# salt ‘*‘ file.access /etc/passwd x server02: False server03: False [[email protected] salt]#
2、file.touch
file.touch:如果文件不存在创建文件,相当于touch file,如果存在就更新访问时间或者修改时间
[[email protected] salt]# salt ‘*‘ file.touch /opt/salt-test server03: True server02: True [[email protected] 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:58:22.354868060 -0400 Modify: 2017-06-14 01:58:22.354868060 -0400 Change: 2017-06-14 01:58: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/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2017-06-14 01:58:22.380650895 -0400 Modify: 2017-06-14 01:58:22.380650895 -0400 Change: 2017-06-14 01:58:22.380650895 -0400 Birth: - [[email protected] salt]#
3、file.append
file.append:向文件末尾追加内容
[[email protected] 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" [[email protected] 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" [[email protected] 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" [[email protected] salt]#
查看追加结果:
[[email protected] salt]# salt ‘*‘ cmd.run ‘cat /opt/salt-test‘ server02: salt test file001 salt test file002 salt test file003 server03: salt test file001 salt test file002 salt test file003 [[email protected] salt]#
4、file.basename 和file.dirname
file.basename:返回给定路径的最后一部分
[[email protected] salt]# salt ‘*‘ file.basename /opt/salt-test server03: salt-test server02: salt-test [[email protected] salt]# salt ‘*‘ file.basename /usr/local/webserver server03: webserver server02: webserver [[email protected] salt]#
file.dirname:返回指定路径的目录部分
[[email protected] salt]# salt ‘*‘ file.dirname /opt/salt-test server03: /opt server02: /opt [[email protected] salt]# salt ‘*‘ file.dirname /usr/local/webserver server02: /usr/local server03: /usr/local [[email protected] salt]#
5、file.chgrp和file.chown
file.chgrp:修改文件的数组
file.chown:修改文件的属主和数组
file.chgrp用法:
[[email protected] salt]# salt ‘*‘ cmd.run ‘ls -al /opt/salt-test‘ server03: -rw-r--r-- 1 root root 54 Jun 14 02:01 /opt/salt-test server02: -rw-r--r-- 1 root root 54 Jun 14 02:01 /opt/salt-test [[email protected] salt]# salt ‘*‘ file.chgrp /opt/salt-test nginx server03: None server02: None [[email protected] salt]# salt ‘*‘ cmd.run ‘ls -al /opt/salt-test‘ server02: -rw-r--r-- 1 root nginx 54 Jun 14 02:01 /opt/salt-test server03: -rw-r--r-- 1 root nginx 54 Jun 14 02:01 /opt/salt-test [[email protected] salt]#
file.chown用法:
[[email protected] salt]# salt ‘*‘ cmd.run ‘ls -al /opt/salt-test‘ server02: -rw-r--r-- 1 root nginx 54 Jun 14 02:01 /opt/salt-test server03: -rw-r--r-- 1 root nginx 54 Jun 14 02:01 /opt/salt-test [[email protected] salt]# salt ‘*‘ file.chown /opt/salt-test nginx nginx server02: None server03: None [[email protected] salt]# salt ‘*‘ cmd.run ‘ls -al /opt/salt-test‘ server03: -rw-r--r-- 1 nginx nginx 54 Jun 14 02:01 /opt/salt-test server02: -rw-r--r-- 1 nginx nginx 54 Jun 14 02:01 /opt/salt-test [[email protected] salt]#
6、file.copy
file.copy:从源目录拷贝文件到目标目录。如果要拷贝目录,需要添加recurse标签,默认情况下会覆盖目标目录中的相同路径的文件,并保留其他文件。remove_existing选项会提前移除目标目录中的所有文件,然后再从源路径拷贝文件到目标路径
[[email protected] salt]# salt ‘*‘ file.copy /etc/hosts /opt/hosts server03: True server02: True [[email protected] salt]# salt ‘*‘ cmd.run ‘ls -al /opt/hosts‘ server02: -rw-r--r-- 1 root root 323 Jun 14 02:18 /opt/hosts server03: -rw-r--r-- 1 root root 323 Jun 14 02:18 /opt/hosts [[email protected] salt]# [[email protected] salt]# salt ‘*‘ file.copy /var/spool/cron/ /tmp/ recurse=True server03: True server02: True [[email protected] salt]# salt ‘*‘ cmd.run ‘ls -l /tmp/‘ server03: total 4 -rw-r--r-- 1 root root 52 Jun 14 02:21 root server02: total 4 -rw-r--r-- 1 root root 52 Jun 14 02:21 root [[email protected] salt]#
[[email protected] 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/‘
[[email protected] salt]#
添加remove_existing=True参数报错,翻阅了一些资料,没知道对应的解释,不知道有没有人知道到底哪里出问题了,坐等指点!!!!
7、file.diskusage
file.diskusage:递归计算指定目录所占的磁盘空间并以字节为单位返回计算出的值
[[email protected] salt]# salt ‘*‘ file.diskusage /etc/passwd server03: 1126 server02: 1126 [[email protected] salt]# salt ‘*‘ file.diskusage /etc/shadow server03: 708 server02: 708 [[email protected] salt]#
8、file.find
file,find:返回指定搜索条件的文件路径,与Linux中的find命令,参数也兼容find命令
[[email protected] salt]# salt ‘*‘ file.find /etc/ name=minion server03: - /etc/salt/minion - /etc/salt/pki/minion server02: - /etc/salt/minion - /etc/salt/pki/minion [[email protected] salt]#
9、file.get_gid、file.get_group、file.get_uid、file.get_user分别表示返回指定文件的数组ID,返回指定文件的数组,返回指定文件的属主id、返回指定文件的属
[[email protected] salt]# salt ‘*‘ file.get_gid /etc/shadow server02: 0 server03: 0 [[email protected] salt]# salt ‘*‘ file.get_group /etc/shadow server03: root server02: root [[email protected] salt]# salt ‘*‘ file.get_uid /etc/shadow server03: 0 server02: 0 [[email protected] salt]# salt ‘*‘ file.get_user /etc/shadow server02: root server03: root [[email protected] salt]#
10、file.grep
file.grep:返回指定文件中查找字符串,跟Linux下grep命令类似,参数可以兼容grep命令
[[email protected] salt]# salt ‘*‘ file.grep /etc/passwd ssh server03: ---------- pid: 28202 retcode: 0 stderr: stdout: sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin server02: ---------- pid: 13034 retcode: 0 stderr: stdout: sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [[email protected] salt]# [[email protected] 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.114 server03: ---------- pid: 28286 retcode: 0 stderr: stdout: PEERDNS=yes IPV6_PEERDNS=yes DNS1=114.114.114.114 [[email protected] salt]#
11、file.makdirs
file.makedirs:创建目录,需要确认目录所包含的路径是否可用。注意,路径末尾一定要加"/",否则就会被当做父目录,比如传入/tmp/pfile。就会被当做/tmp/处理,而传入/tmp/pfile/则会被当做/tmp/pfile/处理。另外,虽然该模块名称包含的dirs,但是其实无法批量创建多个目录,如果传入多个参数默认值处理第一个参数,但是可以创建多级目录,及时上级目录不存在。
[[email protected] salt]# salt ‘*‘ file.makedirs /tmp/pfile server03: Directory ‘/tmp‘ already exists server02: Directory ‘/tmp‘ already exists [[email protected] salt]# salt ‘*‘ file.makedirs /tmp/pfile/ server02: None server03: None [[email protected] salt]# salt ‘*‘ cmd.run ‘ls -l /tmp/‘ server03: total 0 drwxr-xr-x 2 root root 6 Jun 14 04:43 pfile server02: total 0 drwxr-xr-x 2 root root 6 Jun 14 04:43 pfile [[email protected] salt]# [[email protected] salt]# salt ‘*‘ file.makedirs /tmp/test01/test02/test03/test04/ server03: None server02: None [[email protected] salt]# salt ‘*‘ cmd.run ‘tree /tmp/‘ server03: /tmp/ |-- pfile `-- test01 `-- test02 `-- test03 `-- test04 5 directories, 0 files server02: /tmp/ |-- pfile `-- test01 `-- test02 `-- test03 `-- test04 5 directories, 0 files [[email protected] salt]#
12、file.mkdir
file.mkdir:确认一个目录是否可用,如果可以用,就创建目录,与上面的file.makedirs不同的是,参数的末尾可以不带"/",也可以创建成功。另外,该模块支持批量创建多个目录,也支持创建多级目录
[[email protected] salt]# salt ‘*‘ file.mkdir /tmp/saltdir server03: None server02: None [[email protected] 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 test01 server02: 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 [[email protected] salt]# salt ‘*‘ file.mkdir /tmp/saltdir01 /tmp/saltdir02 /tmp/saltdir03 server02: None server03: None [[email protected] salt]# salt ‘*‘ file.mkdir /tmp/pfile/saltfile server03: None server02: None [[email protected] salt]# salt ‘*‘ cmd.run ‘ls -l /tmp/saltdir*‘ server02: /tmp/saltdir: total 0 /tmp/saltdir01: total 0 server03: /tmp/saltdir: total 0 /tmp/saltdir01: total 0 [[email protected] salt]# salt ‘*‘ cmd.run ‘ls -ld /tmp/pfile/saltfile‘ server03: drwxr-xr-x 2 root root 6 Jun 14 04:50 /tmp/pfile/saltfile server02: drwxr-xr-x 2 root root 6 Jun 14 04:50 /tmp/pfile/saltfile [[email protected] salt]#
13、file.move
file.move:移动一个文件或者目录
[[email protected] salt]# salt ‘*‘ file.move /tmp/pfile/saltfile /tmp/move-saltfile server02: ---------- comment: ‘/tmp/pfile/saltfile‘ moved to ‘/tmp/move-saltfile‘ result: True server03: ---------- comment: ‘/tmp/pfile/saltfile‘ moved to ‘/tmp/move-saltfile‘ result: True [[email protected] salt]# salt ‘*‘ cmd.run ‘ls -ld /tmp/move-saltfile‘ server03: drwxr-xr-x 2 root root 6 Jun 14 04:50 /tmp/move-saltfile server02: drwxr-xr-x 2 root root 6 Jun 14 04:50 /tmp/move-saltfile [[email protected] salt]#
14、file.remove
file.remove:删除文件。注意:该模块一次只能接受一个参数。
[[email protected] salt]# salt ‘*‘ file.remove /tmp/test01 server02: True server03: True [[email protected] 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 saltdir01 server03: 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 [[email protected] salt]#
15、file.rename
file.rename:重命名一个文件或者目录
[[email protected] salt]# salt ‘*‘ file.rename /tmp/saltdir /tmp/saltdir-rename server03: True server02: True [[email protected] 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 saltdir01 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--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 [[email protected] salt]# salt ‘*‘ file.rename /tmp/salt-test-file /tmp/salt-test-file-rename server03: True server02: True [[email protected] 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--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 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-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 [[email protected] salt]#
16、file.stats
file.stats:返回包含指定文件状态的词典
[[email protected] salt]# salt ‘*‘ file.stats /etc/shadow server03: ---------- 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: root server02: ---------- 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 [[email protected] salt]#
17、file.rmdir
file.rmdir:删除指定空目录,如果目录不为空,则执行失败,即返回失败。
[[email protected] salt]# salt ‘*‘ file.rmdir /tmp/pfile server02: True server03: True [[email protected] salt]# salt ‘*‘ file.rmdir /tmp/saltdir01/ server03: Directory not empty server02: Directory not empty [[email protected] salt]# salt ‘*‘ file.rmdir /tmp/saltdir01 server02: Directory not empty server03: Directory not empty [[email protected] salt]#
18、file.search
file.search:搜索pattern参数是否出现在指定的文件中
[[email protected] salt]# salt ‘*‘ file.search /etc/passwd root server03: True server02: True [[email protected] salt]# salt ‘*‘ file.search /etc/passwd ‘ssh‘ server02: True server03: True [[email protected] salt]#
19、file.readdir
file.readdir:返回包含一个目录的内容列表
[[email protected] salt]# salt ‘*‘ file.readdir /tmp/saltdir01 server03: - . - .. - salt-test-file-rename server02: - . - .. - salt-test-file-rename [[email protected] 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-rename server03: total 4 -rw-r--r-- 1 root root 9 Jun 14 05:08 salt-test-file-rename [[email protected] salt]#
本文出自 “平平淡淡才是真” 博客,请务必保留此出处http://ucode.blog.51cto.com/10837891/1936744
以上是关于自动化运维之saltstack常用模块使用之file的主要内容,如果未能解决你的问题,请参考以下文章