您好,登錄后才能下訂單哦!
1、file.access
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
/etc/zabbix/zabbix_agentd.conf.d: file.directory: - user: root - group: root - mode: 755 - makedirs: Ture #如果此目錄用戶不存在自動創建 - recurse: #如果想強制將屬注數組權限遞歸到文件夾內文件可以使用這個 - user - group - mode
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。