天天看點

Saltstack-Salt常用子產品及API

Salt提供了非常豐富的功能子產品,涉及作業系統的基礎功能、常用工具支援等,可以通過sys子產品列出目前版本支援的子產品。

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示例:

>>> 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等。

示例:

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調用:

>>> import salt.client
>>> client = salt.client.LocalClient()
>>> client.cmd('*','archive.gunzip',['/opt/test/test.txt.gz'])
{'781915e2': []}      

 (2)cmd子產品

功能:實作遠端的指令行調用執行(預設具備root操作權限,使用時需評估風險)

[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調用:

client.cmd('*','cmd.run',['free -m])      

(3)cp子產品

功能:實作遠端檔案、目錄的複制,以及下載下傳URL檔案等操作。

salt '*' cp.cache_local_file /etc/hosts     #将指定被控主機的/etc/hosts檔案複制到被控主機本地的salt 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_roots指定位置下的目錄複制到被控主機,salt:// 第一個‘/’為 配置檔案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      
client.cmd('*','cp.get_file',['salt://path/to/file ',' /minion/dest'])      

(4)cron子產品

功能:實作被控主機的crontab操作

salt '*' cron.raw_cron root     #檢視指定被控主機、root使用者的crontab清單
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     #删除指定的被控主機、root使用者crontab的/usr/local/weekly任務作業
781915e2:
    removed      
client.cmd('*','cron.set_job,['root','*','*','*','*','*','/usr/echo'])      

繼續閱讀