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 
       }       
       #结果以一个标准的Python字典形式的字符串返回,可以通过eval()函数转换成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     
       #将测试的gzip文件拷贝给被控端minion 
      
     
     
     
     
      
      
     
     
     
     
      
       
       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/hosts文件复制到被控主机本地的salt 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_roots指定位置下的目录复制到被控主机,salt:// 第一个‘


/’为 配置文件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      #查看指定被控主机、root用户的crontab清单


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      #删除指定的被控主机、root用户crontab的/usr/local/weekly任务作业


781915 e2:


removed





API调用:

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