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
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> # 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> 拒绝所有的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.
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做列表匹配,例如:L@minion1.test.com,minion3.domain.com or bl*.domain.com
I --针对 Pillar 做单个匹配,例如:I@pdata:foobar
S --针对子网或是IP做匹配,例如:S@192.168.1.0/24 or S@192.168.1.100
R --针对客户端范围做匹配,例如:R@%foo.bar
编辑master配置文件,注意组名前有两个空格,后有一个空格
# vi /etc/salt/master
如下执行脚本:
# 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/testa
192.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/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
# salt '' file.find /root type=f name=\.bak> 192.168.10.249:
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:
True
22. 获取hostname
Get hostname
CLI Example:
# salt '*' network.get_hostname
192.168.10.249:
localhost.localdomain
23. 同步文件
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