1.查看版本
# salt --version
salt 2015.5.8 (Lithium)
[root@localhost salt]# salt --version-report
Usage: salt [options] '<target>' <function> [arguments]
salt: error: no such option: --version-report
# salt --versions-report
Salt: 2015.5.8
Python: 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
Jinja2: unknown
M2Crypto: 0.20.2
msgpack-python: 0.4.6
msgpack-pure: Not Installed
pycrypto: 2.0.1
libnacl: Not Installed
PyYAML: 3.10
ioflo: Not Installed
PyZMQ: 14.3.1
RAET: Not Installed
ZMQ: 3.2.5
Mako: 0.3.4
Tornado: Not Installed
timelib: Not Installed
dateutil: Not Installed
2. salt-key
查看所的有minion
# salt-key –L
Accepted Keys:
192.168.10.110
192.168.43.218
Denied Keys:
Unaccepted Keys:
192.168.10.110
Rejected Keys:
接受指定的minion
# salt-key –a id
# salt-key -a 192.168.10.110
The following keys are going to be accepted:
Unaccepted Keys:
192.168.10.110
Proceed? [n/Y] Y
接受所有的minion
# salt-key -A
刪除指定的minion
# salt-key –d id
# salt-key -d 192.168.10.110
The following keys are going to be deleted:
Accepted Keys:
192.168.10.110
Proceed? [N/y] y
Key for minion 192.168.10.110 deleted.
刪除所有的minion
# salt-key -D
The following keys are going to be deleted:
Accepted Keys:
192.168.10.110
192.168.43.218
Proceed? [N/y] y
Key for minion 192.168.10.110 deleted.
Key for minion 192.168.43.218 deleted.
拒絕指定的minion
# salt-key –r id
拒絕所有的minion
# salt-key -R
The following keys are going to be rejected:
Unaccepted Keys:
192.168.10.110
Proceed? [n/Y] Y
Key for minion 192.168.10.110 rejected.
3. minion狀態管理
查看存活的minion
# salt-run manage.up
- 192.168.10.249
查看死掉的minion
#salt-run manage.down
查看down掉的minion,並將其刪除
# salt-run manage.down removekeys=True
查看minion的相關狀態
# salt-run manage.status
# salt-run manage.status
down:
up:
- 192.168.10.249
查看slat的所有master和minion的版本信息
# salt-run manage.versions
# salt-run manage.versions
Master:
2015.5.8
2016.Up to date:
----------
192.168.10.249:
2015.5.8
4. 將minion分組
分組規則:
G --針對Grains做單個匹配,例如:G@os:Ubuntu
E --針對minion針對正則表達式匹婚配,例如:E@webd+.(dev|qa|prod).loc
P --針對Grains做正則表達式匹配,例如:P@os:(RedHat|Fedora|CentOS)
L --針對minion做列表匹配,例如:[email protected],minion3.domain.com or bl*.domain.com
I --針對 Pillar 做單個匹配,例如:I@pdata:foobar
S --針對子網或是IP做匹配,例如:[email protected]/24 or [email protected]
R --針對客戶端範圍做匹配,例如:R@%foo.bar
編輯master配置文件,注意組名前有兩個空格,後有一個空格
# vi /etc/salt/master
nodegroups:
web: '[email protected],192.168.10.111'
# salt -N ‘web’ test.ping
192.168.10.110:
True
首先要保證組員在master的列表中,否則對分組進行操作時會不成功:
# salt -N group1 test.ping
No minions matched the target. No command was sent, no jid was assigned.
ERROR: No return received
5. salt文件管理
編輯master主配置文件,做如下配置:
file_roots:
base:
- /srv/salt
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
可以將文件及shell放在/srv/salt等文件夾下,執行其他命令調用方法:salt://dev/install.sh
如下執行腳本:
# salt '192.168.10.110' cmd.script salt://test.sh
6. 壓縮和解壓縮:archive.tar
通過tar壓縮或解壓文件。
options:Options to pass to the tar command
tarfile:The filename of the tar archive to pack/unpack
sources:Comma delimited list of files to pack into the tarfile. Can also be passed as a Python list.
dest:The destination directory into which to unpack the tarfile
cwd : None,The directory in which the tar command should be executed. If not specified, will default to the home directory of the user under which the salt minion process is running.
template : None
Can be set to 'jinja' or another supported template engine to render the command arguments before execution:
salt '' archive.tar cjvf /tmp/salt.tar.bz2 {{grains.saltpath}} template=jinja
CLI Examples:
# Create a tarfile
salt '' archive.tar cjvf /tmp/tarfile.tar.bz2 /tmp/file_1,/tmp/file_2
# Unpack a tarfile
salt '*' archive.tar xf foo.tar dest=/target/directory
- 執行壓縮命令:
# salt '*' archive.tar zcvf /root/test.tar.gz /root/python,/root/testa192.168.10.249:
- tar: Removing leading `/' from member names
- /root/python/
- /root/python/p2
- /root/python/p1
- /root/testa
通過該方法壓縮的文件,解壓後帶有全路徑,可通過cwd指定執行的目錄.
# salt '*' archive.tar zcvf /root/test/test.tar.gz python,testa cwd=/root
192.168.10.249:
- python/
- python/p2
- python/p1
- testa
- 執行解壓縮命令:
# salt '*' archive.tar zxvf /root/test/test.tar.gz dest=/root/test192.168.10.249:
- python/
- python/p2
- python/p1
- testa
7. 檢測:test.ping
測試主機的存活狀態
# salt ‘*’ test.ping
# salt '192.168.10.110' test.ping
192.168.10.110:
True
8. 執行shell:cmd.run
在minion上執行shell命令。
# salt '192.168.10.110' cmd.run 'whoami'
192.168.10.110:
root
9. 執行腳本文件:cmd. script
在文件管理中放入相應的腳本文件
# cat /srv/salt/test.sh
#!/bin/bash
echo 'This is a test.'
在minion上執行該腳本:
# salt '192.168.10.110' cmd.script salt://test.sh
192.168.10.110:
----------
pid:
23866
retcode:
0
stderr:
stdout:
This is a test.
10. 查看磁盤利用率:disk.usage
Return usage information for volumes mounted on this minion
CLI Example:
salt '*' disk.usage
11. 更改文件的所屬權
更改文件的所屬權.
file.chown(path, user, group)
Chown a file, pass the file the desired user and group
path:path to the file or directory
user:user owner
group:group owner
CLI Example:
salt '*' file.chown /etc/passwd root root
12. 複製文件
file.copy(src, dst, recurse=False, remove_existing=False)
將一個目錄或文件夾複製到另一個地方
如果複製一個文件夾必須加上recurse=True參數。默認爲複製目的文件夾。
remove_existing=True,首先將目的文件夾裏的內容全部刪除,再進行復制。該參數可以保證源和目的完全一樣。
CLI Example:
salt '*' file.copy /path/to/src /path/to/dst
salt '*' file.copy /path/to/src_dir /path/to/dst_dir recurse=True
salt '*' file.copy /path/to/src_dir /path/to/dst_dir recurse=True remove_existing=True
13. 判斷文件是否存在
判斷某個文件是否存在,返回true/false。
CLI Example:
salt '*' file.file_exists /etc/passwd
14. 判斷文件夾是否存在
判斷一個文件夾是否存在,返回true/false。
# salt '*' file.directory_exists /root/a
192.168.10.249:
True
# salt '*' file.directory_exists /root/ab
192.168.10.249:
False
15. 查看目錄大小
返回目錄大小(字節)。
CLI Example:
# salt '*' file.diskusage /root
192.168.10.249:
252580128
16. 查找文件
salt '*' file.find / type=f name=*.bak size=+10m
salt '*' file.find /var mtime=+30d size=+10m print=path,size,mtime
salt '*' file.find /var/log name=*.[0-9] mtime=+30d size=+10m delete
-
1.查看大小大於10k,後綴爲.bak的文件
# salt '*' file.find /root type=f name=*.bak size=+10k192.168.10.249:
- /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak -
- 查看大小大於10k,後綴爲.bak的文件,返回path,size,mtime
# salt '*' file.find /root type=f name=*.bak size=+10k print=path,size,mtime192.168.10.249:
- /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak
- 16485
- 1453970790
- 查看大小大於10k,後綴爲.bak的文件,返回path,size,mtime
-
- 查看大小大於10k,後綴爲.bak的文件,並刪除
# salt '*' file.find /root type=f name=*.bak size=+10k delete192.168.10.249:
- /root/sabawar/assets/tinymce/jscripts/tiny_mce/plugins/media/js/media.js.bak
# salt '' file.find /root type=f name=\.bak size=+10k
192.168.10.249:
- 查看大小大於10k,後綴爲.bak的文件,並刪除
17. 過濾文件內容
過濾某個文件的內容。同linux的grep。
file.grep(path, pattern, *args)
Note:This function's return value is slated for refinement in future versions of Salt
path:A file path
pattern:A string. For example: test a[0-5]
args:grep options. For example: " -v" " -i -B2"
CLI Example:
salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr " -i"
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr " -i -B2"
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr " -i -l"
18. 替換文件內容
# cat a
a
b
c
# salt '*' file.replace /root/test/a pattern='a' repl='aa'
192.168.10.249:
---
+++
@@ -1,3 +1,3 @@
-a
+aa
b
c
# cat a
aa
b
c
19. 更改文件的權限
設置文件(夾)的權限
salt '*' file.set_mode /etc/passwd 0644
20. 添加hosts文件
添加一個host對,如果存在將對應的值添加進去,不存在則創建。
# salt '*' hosts.add_host 192.168.10.249 test.com
192.168.10.249:
True
21. 檢測端口
在minion檢測某個域名或主機的端口是否打開.
CLI Example:
salt '*' network.connect archlinux.org 80
salt '*' network.connect archlinux.org 80 timeout=3
salt '*' network.connect archlinux.org 80 timeout=3 family=ipv4
salt '*' network.connect google-public-dns-a.google.com port=53 proto=udp timeout=3
# salt '*' network.connect 192.168.10.249 22
192.168.10.249:
----------
comment:
Successfully connected to 192.168.10.249 (192.168.10.249) on tcp port 22
result:
True22. 獲取hostname
Get hostname
CLI Example:
# salt '*' network.get_hostname
192.168.10.249:
localhost.localdomain23. 同步文件
CLI Example:
salt '' rsync.rsync {src} {dst} {delete=True} {update=True} {passwordfile=/etc/pass.crt} {exclude=xx}
salt '' rsync.rsync {src} {dst} {delete=True} {excludefrom=/xx.ini}
# salt '*' rsync.rsync /root/test /test
192.168.10.249:
----------
pid:
5572
retcode:
0
stderr:
stdout:
sending incremental file list
test/
test/a
test/a.bak
test/telnet.rpm
test/soft/
test/soft/telnet.rpm
sent 108711 bytes received 96 bytes 72538.00 bytes/sec
total size is 118101 speedup is 1.09