Saltstack API以及對應的Python模板

來源:互聯網
上載者:User

標籤:saltstack api以及對應的python模板


來源:《Python自動化營運開發》

測試:import salt.clientclient = salt.client.LocalClient()ret = client.cmd(‘*‘, ‘test.ping‘)print ret(1)Archive模組1)功能:實現系統層面的壓縮包調用,支援gunzip、gzip、rar、tar、unrar、unzip等。2)樣本:#採用gzunzip解壓/tmp/sourcefile.txt.gz包salt ‘*‘ archive.gunzip /tmp/sourcefile.txt.gz#採用gzip壓縮/tmp/sourcefile.txt檔案salt ‘*‘ archive.gzip /tmp/sourcefile.txt3)API調用:client.cmd(‘*‘, ‘ archive.gunzip‘,[‘/tmp/sourcefile.txt.gz ‘])(2)cmd模組1)功能:實現遠端命令列調用執行(預設具備root操作許可權,使用時需評估風險)。2)樣本:#擷取所有被控主機的記憶體使用量情況salt ‘*‘ cmd.run "free -m"#在SN2013-08-021主機運行test.sh指令碼,其中script/test.sh存放在file_roots指定的目錄,#該命令會做兩個動作:首先同步test.sh到minion的cache目錄(如同步到/var/cache/salt/#minion/files/base/script/test.sh);其次運行該指令碼‘SN2013-08-021‘ cmd.script salt://script/test.sh3)API調用:client.cmd(‘SN2013-08-021‘, ‘cmd.run‘,[‘free -m‘])(3)cp模組1)功能:實現遠程檔案、目錄的複製,以及下載URL檔案等操作。2)樣本:#將指定被控主機的/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.html3)API調用:client.cmd(‘SN2013-08-021‘, ‘cp.get_file‘,[‘ salt://path/to/file ‘,‘/minion/dest‘])(4)cron模組1)功能:實現被控主機的crontab操作。2)樣本:#查看指定被控主機、root使用者的crontab清單salt ‘SN2013-08-022‘ cron.raw_cron root#為指定的被控主機、root使用者添加/usr/local/weekly任務作業salt ‘SN2013-08-022‘ cron.set_job root ‘*‘ ‘*‘ ‘*‘ ‘*‘ 1 /usr/local/weekly#刪除指定的被控主機、root使用者crontab的/usr/local/weekly任務作業salt ‘SN2013-08-022‘ cron.rm_job root /usr/local/weekly3)API調用:client.cmd(‘SN2013-08-021‘, ‘cron.set_job‘,[‘root‘,‘*‘,‘*‘,‘*‘,‘*‘,‘*‘,‘/usr/echo‘])(5)dnsutil模組1)功能:實現被控主機通用DNS相關操作。2)樣本:#添加指定被控主機hosts的主機配置項salt ‘*‘ dnsutil.hosts_append /etc/hosts 127.0.0.1 ad1.yuk.com,ad2.yuk.com#刪除指定被控主機hosts的主機配置項salt ‘*‘ dnsutil.hosts_remove /etc/hosts ad1.yuk.com3)API調用:client.cmd(‘*‘, ‘dnsutil.hosts_append‘,[‘/etc/hosts‘,‘127.0.0.1‘,‘ad1.yuk.co‘])(6)file模組1)功能:被控主機檔案常見操作,包括檔案讀寫、許可權、尋找、校正等。2)樣本:#校正所有被控主機/etc/fstab檔案的md5是否為6254e84e2f6ffa54e0c8d9cb230f5505,一致則返回Truesalt ‘*‘ file.check_hash /etc/fstab md5=6254e84e2f6ffa54e0c8d9cb230f5505#校正所有被控主機檔案的加密資訊、支援md5、sha1、sha224、sha256、sha384、sha512密碼編譯演算法salt ‘*‘ file.get_sum /etc/passwd md5#修改所有被控主機/etc/passwd檔案的屬組、使用者權限,等價於chown root:root /etc/passwdsalt ‘*‘ file.chown /etc/passwd root root#複製所有被控主機本地/path/to/src檔案到本地的/path/to/dst檔案salt ‘*‘ file.copy /path/to/src /path/to/dst#檢查所有被控主機/etc目錄是否存在,存在則返回True,檢查檔案是否存在使用file.file_exists方法salt ‘*‘ file.directory_exists /etc#擷取所有被控主機/etc/passwd的stats資訊salt ‘*‘ file.stats /etc/passwd#擷取所有被控主機/etc/passwd的許可權mode,如755、644salt ‘*‘ file.get_mode /etc/passwd#修改所有被控主機/etc/passwd的許可權mode為0644salt ‘*‘ file.set_mode /etc/passwd 0644#在所有被控主機建立/opt/test目錄salt ‘*‘ file.mkdir /opt/test#將所有被控主機/etc/httpd/httpd.conf檔案的LogLevel參數的warn值修改成infosalt ‘*‘ file.sed /etc/httpd/httpd.conf ‘LogLevel warn‘ ‘LogLevel info‘#給所有被控主機的/tmp/test/test.conf檔案追加內容"maxclient 100"salt ‘*‘ file.append /tmp/test/test.conf "maxclient 100"#刪除所有被控主機的/tmp/foo檔案salt ‘*‘ file.remove /tmp/foo3)API調用:client.cmd(‘*‘, ‘ file.remove ‘,[‘/tmp/foo‘])(7)iptables模組1)功能:被控主機iptables支援。2)樣本:#在所有被控端主機追加(append)、插入(insert)iptables規則,其中INPUT為輸入鏈salt ‘*‘ iptables.append filter INPUT rule=‘-m state --state RELATED,ESTABLISHED -j ACCEPT‘salt ‘*‘ iptables.insert filter INPUT position=3 rule=‘-m state --stateRELATED,ESTABLISHED -j ACCEPT‘#在所有被控端主機刪除指定鏈編號為3(position=3)或指定存在的規則salt ‘*‘ iptables.delete filter INPUT position=3salt ‘*‘ iptables.delete filter INPUT rule=‘-m state --state RELATED,ESTABLISHED -j ACCEPT‘#儲存所有被控端主機規則到本地硬碟(/etc/sysconfig/iptables)salt ‘*‘ iptables.save /etc/sysconfig/iptables3)API調用:client.cmd(‘SN2013-08-022‘, ‘iptables.append‘,[‘filter‘,‘INPUT‘,‘rule=\‘-p tcp --sport 80 -j ACCEPT\‘‘])(8)netwrok模組1)功能:返回被控主機網路資訊。2)樣本:#在指定被控主機‘SN2013-08-022‘擷取dig、ping、traceroute目錄網域名稱資訊salt ‘SN2013-08-022‘ network.dig www.qq.comsalt ‘SN2013-08-022‘ network.ping www.qq.comsalt ‘SN2013-08-022‘ network.traceroute www.qq.com#擷取指定被控主機‘SN2013-08-022‘的MAC地址salt ‘SN2013-08-022‘ network.hwaddr eth0#檢測指定被控主機‘SN2013-08-022‘是否屬於10.0.0.0/16子網範圍,屬於則返回Truesalt ‘SN2013-08-022‘ network.in_subnet 10.0.0.0/16#擷取指定被控主機‘SN2013-08-022‘的網卡配置資訊salt ‘SN2013-08-022‘ network.interfaces#擷取指定被控主機‘SN2013-08-022‘的IP地址配置資訊salt ‘SN2013-08-022‘ network.ip_addrs#擷取指定被控主機‘SN2013-08-022‘的子網資訊salt ‘SN2013-08-022‘ network.subnets3)API調用:client.cmd(‘SN2013-08-022‘, ‘network.ip_addrs‘)(9)pkg包管理模組1)功能:被控主機程式包管理,如yum、apt-get等。2)樣本:#為所有被控主機安裝PHP環境,根據不同系統發行版調用不同安裝工具進行部署,如redhat平台的yum,等價於yum -y install phpsalt ‘*‘ pkg.install php#卸載所有被控主機的PHP環境salt ‘*‘ pkg.remove php#升級所有被控主機的軟體包salt ‘*‘ pkg.upgrade3)API調用:client.cmd(‘SN2013-08-022‘, ‘pkg.remove‘,[‘php‘])(10)Service服務模組1)功能:被控主機程式包服務管理。2)樣本:#開啟(enable)、禁用(disable)nginx開機自啟動服務salt ‘*‘ service.enable nginxsalt ‘*‘ service.disable nginx#針對nginx服務的reload、restart、start、stop、status操作salt ‘*‘ service.reload nginxsalt ‘*‘ service.restart nginxsalt ‘*‘ service.start nginxsalt ‘*‘ service.stop nginxsalt ‘*‘ service.status nginx3)API調用:client.cmd(‘SN2013-08-022‘, ‘service.stop‘,[‘nginx‘])


本文出自 “奮鬥吧” 部落格,請務必保留此出處http://lvnian.blog.51cto.com/7155281/1873949

Saltstack API以及對應的Python模板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.