自动化运维之saltstack(三)常用模块使用之file
1、file.access
创新互联建站主营博望网站建设的网络公司,主营网站建设方案,app软件定制开发,博望h5小程序制作搭建,博望网站营销推广欢迎博望等地区企业咨询
file.access:测试salt进程对指定文件是否有访问权限
[root@server01 salt]# salt '*' file.access /etc/passwd f server02: True server03: True [root@server01 salt]# salt '*' file.access /etc/passwd r server03: True server02: True [root@server01 salt]# salt '*' file.access /etc/passwd w server02: True server03: True [root@server01 salt]# salt '*' file.access /etc/passwd x server02: False server03: False [root@server01 salt]#
2、file.touch
file.touch:如果文件不存在创建文件,相当于touch file,如果存在就更新访问时间或者修改时间
[root@server01 salt]# salt '*' file.touch /opt/salt-test server03: True server02: 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: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: - [root@server01 salt]#
3、file.append
file.append:向文件末尾追加内容
[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]#
查看追加结果:
[root@server01 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 [root@server01 salt]#
4、file.basename 和file.dirname
file.basename:返回给定路径的最后一部分
[root@server01 salt]# salt '*' file.basename /opt/salt-test server03: salt-test server02: salt-test [root@server01 salt]# salt '*' file.basename /usr/local/webserver server03: webserver server02: webserver [root@server01 salt]#
file.dirname:返回指定路径的目录部分
[root@server01 salt]# salt '*' file.dirname /opt/salt-test server03: /opt server02: /opt [root@server01 salt]# salt '*' file.dirname /usr/local/webserver server02: /usr/local server03: /usr/local [root@server01 salt]#
5、file.chgrp和file.chown
file.chgrp:修改文件的数组
file.chown:修改文件的属主和数组
file.chgrp用法:
[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-test server02: -rw-r--r-- 1 root root 54 Jun 14 02:01 /opt/salt-test [root@server01 salt]# salt '*' file.chgrp /opt/salt-test nginx server03: None server02: 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-test server03: -rw-r--r-- 1 root nginx 54 Jun 14 02:01 /opt/salt-test [root@server01 salt]#
file.chown用法:
[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-test server03: -rw-r--r-- 1 root nginx 54 Jun 14 02:01 /opt/salt-test [root@server01 salt]# salt '*' file.chown /opt/salt-test nginx nginx server02: None server03: 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-test server02: -rw-r--r-- 1 nginx nginx 54 Jun 14 02:01 /opt/salt-test [root@server01 salt]#
6、file.copy
file.copy:从源目录拷贝文件到目标目录。如果要拷贝目录,需要添加recurse标签,默认情况下会覆盖目标目录中的相同路径的文件,并保留其他文件。remove_existing选项会提前移除目标目录中的所有文件,然后再从源路径拷贝文件到目标路径
[root@server01 salt]# salt '*' file.copy /etc/hosts /opt/hosts server03: True server02: True [root@server01 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 [root@server01 salt]# [root@server01 salt]# salt '*' file.copy /var/spool/cron/ /tmp/ recurse=True server03: True server02: True [root@server01 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 [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]#
添加remove_existing=True参数报错,翻阅了一些资料,没知道对应的解释,不知道有没有人知道到底哪里出问题了,坐等指点!!!!
7、file.diskusage
file.diskusage:递归计算指定目录所占的磁盘空间并以字节为单位返回计算出的值
[root@server01 salt]# salt '*' file.diskusage /etc/passwd server03: 1126 server02: 1126 [root@server01 salt]# salt '*' file.diskusage /etc/shadow server03: 708 server02: 708 [root@server01 salt]#
8、file.find
file,find:返回指定搜索条件的文件路径,与Linux中的find命令,参数也兼容find命令
[root@server01 salt]# salt '*' file.find /etc/ name=minion server03: - /etc/salt/minion - /etc/salt/pki/minion server02: - /etc/salt/minion - /etc/salt/pki/minion [root@server01 salt]#
9、file.get_gid、file.get_group、file.get_uid、file.get_user分别表示返回指定文件的数组ID,返回指定文件的数组,返回指定文件的属主id、返回指定文件的属
[root@server01 salt]# salt '*' file.get_gid /etc/shadow server02: 0 server03: 0 [root@server01 salt]# salt '*' file.get_group /etc/shadow server03: root server02: root [root@server01 salt]# salt '*' file.get_uid /etc/shadow server03: 0 server02: 0 [root@server01 salt]# salt '*' file.get_user /etc/shadow server02: root server03: root [root@server01 salt]#
10、file.grep
file.grep:返回指定文件中查找字符串,跟Linux下grep命令类似,参数可以兼容grep命令
[root@server01 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 [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.114 server03: ---------- pid: 28286 retcode: 0 stderr: stdout: PEERDNS=yes IPV6_PEERDNS=yes DNS1=114.114.114.114 [root@server01 salt]#
11、file.makdirs
file.makedirs:创建目录,需要确认目录所包含的路径是否可用。注意,路径末尾一定要加"/",否则就会被当做父目录,比如传入/tmp/pfile。就会被当做/tmp/处理,而传入/tmp/pfile/则会被当做/tmp/pfile/处理。另外,虽然该模块名称包含的dirs,但是其实无法批量创建多个目录,如果传入多个参数默认值处理第一个参数,但是可以创建多级目录,及时上级目录不存在。
[root@server01 salt]# salt '*' file.makedirs /tmp/pfile server03: Directory '/tmp' already exists server02: Directory '/tmp' already exists [root@server01 salt]# salt '*' file.makedirs /tmp/pfile/ server02: None server03: 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 server02: 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: None server02: None [root@server01 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 [root@server01 salt]#
12、file.mkdir
file.mkdir:确认一个目录是否可用,如果可以用,就创建目录,与上面的file.makedirs不同的是,参数的末尾可以不带"/",也可以创建成功。另外,该模块支持批量创建多个目录,也支持创建多级目录
[root@server01 salt]# salt '*' file.mkdir /tmp/saltdir server03: None server02: 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 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 [root@server01 salt]# salt '*' file.mkdir /tmp/saltdir01 /tmp/saltdir02 /tmp/saltdir03 server02: None server03: None [root@server01 salt]# salt '*' file.mkdir /tmp/pfile/saltfile server03: None server02: None [root@server01 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 [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/saltfile server02: drwxr-xr-x 2 root root 6 Jun 14 04:50 /tmp/pfile/saltfile [root@server01 salt]#
13、file.move
file.move:移动一个文件或者目录
[root@server01 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 [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-saltfile server02: drwxr-xr-x 2 root root 6 Jun 14 04:50 /tmp/move-saltfile [root@server01 salt]#
14、file.remove
file.remove:删除文件。注意:该模块一次只能接受一个参数。
[root@server01 salt]# salt '*' file.remove /tmp/test01 server02: True server03: 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 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 [root@server01 salt]#
15、file.rename
file.rename:重命名一个文件或者目录
[root@server01 salt]# salt '*' file.rename /tmp/saltdir /tmp/saltdir-rename server03: True server02: 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 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 [root@server01 salt]# salt '*' file.rename /tmp/salt-test-file /tmp/salt-test-file-rename server03: True server02: 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--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 [root@server01 salt]#
16、file.stats
file.stats:返回包含指定文件状态的词典
[root@server01 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 [root@server01 salt]#
17、file.rmdir
file.rmdir:删除指定空目录,如果目录不为空,则执行失败,即返回失败。
[root@server01 salt]# salt '*' file.rmdir /tmp/pfile server02: True server03: True [root@server01 salt]# salt '*' file.rmdir /tmp/saltdir01/ server03: Directory not empty server02: Directory not empty [root@server01 salt]# salt '*' file.rmdir /tmp/saltdir01 server02: Directory not empty server03: Directory not empty [root@server01 salt]#
18、file.search
file.search:搜索pattern参数是否出现在指定的文件中
[root@server01 salt]# salt '*' file.search /etc/passwd root server03: True server02: True [root@server01 salt]# salt '*' file.search /etc/passwd 'ssh' server02: True server03: True [root@server01 salt]#
19、file.readdir
file.readdir:返回包含一个目录的内容列表
[root@server01 salt]# salt '*' file.readdir /tmp/saltdir01 server03: - . - .. - salt-test-file-rename server02: - . - .. - 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-rename server03: total 4 -rw-r--r-- 1 root root 9 Jun 14 05:08 salt-test-file-rename [root@server01 salt]#
20、file.managed
当在系统中有些文件分部在成百上千台机器经常修改时,使用saltstack的file.managed管理文件状态就会让你方便很多。能让你一键完成这所有修改达到你想要的文件状态,
[root@salt-master base]# tree . ├── system │ ├── files │ │ └── hosts │ └── hosts.sls └── top.sls 2 directories, 3 files [root@salt-master base]# cat top.sls base: #这里指定的环境是base,所以这个top.sls在/srv/salt/base目录下 "*": # “*”是所有主机的意思,指定单个主机直接写“salt-minion1” - system.hosts #这里指调用了那些sls配置文件,“点”在这里是目录分级 #也就是system没有可以下的hosts.sls配置文件 [root@salt-master base]# cat system/hosts.sls /etc/hosts: #这个是配置ID和文件存放位置,是不可重复的 file.managed: #这里调用了“file.managed”salt的文件管理模块 - source: salt://system/files/hosts #source是指定文件源,"salt://"是指salt项目下文件 - mode: 644 #文件权限644 - user: root #文件用户属主 - group: root #文件的用户组
上面文件状态配置执行详解:
[root@salt-master system] # salt 'salt-minion1' state.highstate #salt '执行节点' 执行模块
下面是执行结果
salt-minion1: #执行节点 ---------- ID: /etc/hosts #配置ID Function: file.managed #模块 Result: True #执行结果True为成功 Comment: File /etc/hosts updated #文件执行操作【更新】 Started: 14:15:55.120499 #执行命令开始时间 Duration: 24.284 ms #执行的时长 Changes: #是否产生更改 ---------- diff: --- +++ @@ -1,2 +1,4 @@ 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomainlocalhost6 localhost6.localdomain +salt-master 10.0.0.11 #在这个带有“+”号的行是增加的,"-"号为减少行 +salt-minion1 10.0.0.21 Summary ------------ Succeeded: 1 (changed=1) #执行成功1个,有一个文件状态发生更改 Failed: 0 #执行失败0个 ------------ Total states run: 1 #执行状态个数
salt命令管理文件
salt “*”
file
.managed
/etc/zabbix/zabbix_agentd
.conf salt:
//file/zabbix_agentd
.conf root root 755
salt的sls文件写法
/etc/zabbix/zabbix_agentd.conf: file.managed: - source: salt://file/zabbix_agentd.conf - user: root - group: root - mode: 755
目录管理file.directory
/etc/zabbix/zabbix_agentd.conf.d: file.directory: - user: root - group: root - mode: 755 - makedirs: Ture #如果此目录用户不存在自动创建 - recurse: #如果想强制将属注数组权限递归到文件夹内文件可以使用这个 - user - group - mode
网站标题:自动化运维之saltstack(三)常用模块使用之file
转载源于:http://ybzwz.com/article/iihjje.html