Saltstack-Salt常用模块及API

Salt提供了非常丰富的功能模块,涉及操作系统的基础功能、常用工具支持等,可以通过sys模块列出当前版本支持的模块。

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
salt  ' * '  sys. list_modules
781915 e2:
  -  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"
781915 e2:
                  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
781915 e2:
  / var / cache / salt / minion / localfiles / etc / hosts
 
salt  ' * '  cp. get_dir  salt: // path / to / dir /  / minion / dest     #file_rootssalt// 
     /’ base
781915 e2:
    
salt  ' * '  cp. get_file  salt: // path / to / file  / minion / dest    #file_roots
781915 e2:
    
salt  ' * '  cp. get_url  http: // www. baidu. com  / tmp / index. html      #URL
781915 e2:
     / 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
781915 e2:
     #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
781915 e2:
     new
salt  ' 789880e2 '  cron. rm_job  root  / usr / local / weekly      #rootcrontab/usr/local/weekly
781915 e2:
     removed

API调用:


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

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值