Salt提供了非常丰富的功能模块,涉及操作系统的基础功能、常用工具支持等,可以通过sys模块列出当前版本支持的模块。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
salt '*' sys.list_modules
781915e2:
- acl
- aliases
- alternatives
- apache
- archive
- artifactory
- at
- blockdev
- btrfs
- buildout
- cloud
- cmd
......
|
API的原理是通过调用master client模块,实例化一个LocalClient对象,再调用cmd()方法来实现的。
API实现test.ping示例:
1
2
3
4
5
|
>>> import salt.client
>>> client = salt.client.LocalClient()
>>> ret = client.cmd( '*' , 'test.ping' )
>>> print ret
{ '781915e2' : True }
|
(1)Archive模块
功能:实现系统层面的压缩包调用,支持gunzip、gzip、rar、tar、unrar、unzip等。
示例:
1
2
3
4
5
6
7
8
9
10
|
salt '781915e2' cmd.run 'mkdir /opt/test'
781915e2:
scp test .txt.gz root@kurol: /opt/test
salt '781915e2' archive.gunzip /opt/test/test .txt.gz
781915e2:
salt '781915e2' archive. gzip /opt/test/test .txt
781915e2:
|
API调用:
1
2
3
4
|
>>> import salt.client
>>> client = salt.client.LocalClient()
>>> client.cmd( '*' , 'archive.gunzip' ,[ '/opt/test/test.txt.gz' ])
{ '781915e2' : []}
|
(2)cmd模块
功能:实现远程的命令行调用执行(默认具备root操作权限,使用时需评估风险)
示例:
1
2
3
4
5
6
|
[root@server ~]
781915e2:
total used free shared buffers cached
Mem: 996 834 162 0 121 252
-/+ buffers /cache : 460 536
Swap: 0 0 0
|
API调用:
1
|
client.cmd( '*' , 'cmd.run' ,['free - m])
|
(3)cp模块
功能:实现远程文件、目录的复制,以及下载URL文件等操作。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
salt '*' cp .cache_local_file /etc/hosts
781915e2:
/var/cache/salt/minion/localfiles/etc/hosts
salt '*' cp .get_dir salt: //path/to/dir/ /minion/dest
781915e2:
salt '*' cp .get_file salt: //path/to/file /minion/dest
781915e2:
salt '*' cp .get_url http: //www .baidu.com /tmp/index .html
781915e2:
/tmp/index .html
|
API调用:
1
|
client.cmd( '*' , 'cp.get_file' ,[ 'salt://path/to/file ' , ' /minion/dest' ])
|
(4)cron模块
功能:实现被控主机的crontab操作
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
salt '*' cron.raw_cron root
781915e2:
* * * * * /usr/local/sa/agent/secu-tcs-agent-mon-safe.sh /usr/local/sa/agent > /dev/null 2>&1
*/1 * * * * /usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &
*/20 * * * * /usr/sbin/ntpdate ntpupdate.tencentyun.com >/dev/null &
30 2 * * * /www/server/panel/certbot-auto renew >> /www/server/panel/logs/certbot.log
salt '*' cron.set_job root '*' '*' '*' '*' 1 /usr/local/weekly
781915e2:
new
salt '789880e2' cron.rm_job root /usr/local/weekly
781915e2:
removed
|
API调用:
1
|
client.cmd( '*' , 'cron.set_job,[' root ',' * ',' * ',' * ',' * ',' * ',' / usr / echo'])
|
本文转自谢育政 51CTO博客,原文链接:http://blog.51cto.com/kurolz/1934033,如需转载请自行联系原作者