打赏

星辰大海ゞ

That which does not kill us makes us stronger!

导航

salt常用模块及API

saltstack提供了非常丰富的功能模块,涉及操作系统的基础功能,常用工具支持等,更多模块信息见官网模块介绍:https://docs.saltstack.com/en/latest/ref/modules/all/index.html 当然,也可以通过sys模块列出当前版本支持的模块。命令如下:

#salt '*' sys.list_modules

API的原理是通过调用master client模块,实例化一个LocalClient对象,再调用cmd()方法来实现的,以下是API实现test.ping的示例:

import salt.client

client = salt.client.LocalClient()

ret = client.cmd('*','test.ping')

print ret

其结果以一个标准的Python字典形式的字符串返回,可以通过eval()函数转换成Python的字典类型,方便后续的业务逻辑处理,程序运行结果如下:

{'hudson': True}

-------------     

另一例子:

>>> import salt.client
>>> local = salt.client.LocalClient()
>>> local.cmd('*', 'cmd.run', ['whoami'])

其它模块如:

salt '*' archive.gunzip /tmp/sourcefile.txt.gz   #支持gunzip、gzip、rar、tar、unrar、unzip等。

salt '*' archive.gzip /tmp/sourcefile.txt

API调用:client.cmd('*','archive.gunzip',['/tmp/sourcefile.txt.gz'])

 

salt '*' cmd.script salt://script/test.sh #该命令会做两个动作:首先同步test.sh到minion的cache目录(如同步到/var/cache/salt/minion/files/base/script/test.sh);其次运行该test.sh脚本

 

#将指定被控主机的/etc/hosts文件复制到被控主机本地的salt cache目录(/var/cache/salt/minion/localfiles/)

salt '*' cp.cache_local_file /etc/hosts

#将主服务器file_roots指定位置下的目录复制到被控主机

salt '*' cp.get_dir salt://path/to/dir/ /minion/dest

#将主服务器file_roots指定位置下的文件复制到被控主机

salt '*' cp.get_file salt://path/to/file /minion/dest

#下载URL内容到被控主机指定位置

salt '*' cp.get_url http://www.slashdot.org /tmp/index.html

API调用:client.cmd('hudson','cp.get_file',['salt://path/to/file' , '/minion/dest'])

 

salt '*'

 

posted on 2015-12-29 11:39  星辰大海ゞ  阅读(887)  评论(0编辑  收藏  举报