Saltstack-Salt常用模块及API

来源:互联网 发布:java 对修改文件权限 编辑:程序博客网 时间:2024/05/22 16:01

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}      #Pythoneval()Python便


(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'   #minion/opt/test
781915e2:
    
scp test.txt.gz root@kurol:/opt/test    #gzipminion
salt '781915e2' archive.gunzip /opt/test/test.txt.gz    #/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
7
[root@server ~]salt '*' cmd.run "free -m"
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     #/etc/hostssalt cache/var/cache/salt/minion/localfiles
781915e2:
 /var/cache/salt/minion/localfiles/etc/hosts
 
salt '*' cp.get_dir salt://path/to/dir/ /minion/dest    #file_rootssalt// 
    /’ base
781915e2:
    
salt '*' cp.get_file salt://path/to/file /minion/dest   #file_roots
781915e2:
    
salt '*' cp.get_url http://www.baidu.com /tmp/index.html     #URL
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     #rootcrontab
781915e2:
    #secu-tcs-agent monitor, install at Sat Mar 18 15:55:40 CST 2017
    * * * * * /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  #root/usr/local/weekly
781915e2:
    new
salt '789880e2' cron.rm_job root /usr/local/weekly     #rootcrontab/usr/local/weekly
781915e2:
    removed

API调用:

1
client.cmd('*','cron.set_job,['root','*','*','*','*','*','/usr/echo'])

 

 

 

 

 

 

 

 

 

 

 

原创粉丝点击