Saltstack 基礎模組記錄二

來源:互聯網
上載者:User

標籤:saltstack 模組

一、

Archive模組

功能:實現系統層面的壓縮包調用,支援gzip、gunzip、rar、tar、unrar、unzip等

樣本:

1、採用gunzip解壓sourcefile.txt.gz包

salt ‘*‘ archive.gunzip sourcefile.txt.gz

2、採用gzip壓縮sourcefile.txt檔案

salt ‘*‘ archive.gzip sourcefile.txt

3、API調用:

client.cmd(‘*‘,‘archive.gunzip‘,[‘sourcefile.txt.gz‘])


二、

cmd模組

功能:實現遠端命令列調用執行(預設具備root操作許可權,使用時需評估風險)

樣本:

1、擷取所欲被控主機的記憶體使用量情況

salt ‘*‘ cmd.run ‘free -m‘


2、在wx主機上運行test.py指令碼,其中script/test.py存放在file_roots指定的目錄(預設是在/srv/salt,自訂在/etc/salt/master檔案中定義),

#該命令會做2個動作:首先同步test.py到minion的cache目錄;起床運行該指令碼

salt ‘wx‘ cmd.script salt://script/test.py

3、API調用:

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


三、cp模組

功能:實現遠程檔案、目錄的複製,以及下載URL檔案等操作

樣本:

 

1、將被控主機的/etc/hosts檔案複製到被控主機本地的salt cache目錄(/var/cache/salt/minion/localfiles/)

salt ‘*‘ cp.cache_local_file /etc/hosts

2、將主控端file_roots指定位置下的目錄複寫到被控主機/minion/目錄下

salt ‘*‘ cp.get_dir salt://script/ /minion/

3、將主控端file_roots指定位置下的檔案複製到被控主機/minion/test.py檔案(file為檔案名稱)

salt ‘*‘ cp.get_dir salt://script/test.py /minion/test.py

4、下載URL內容到被控主機指定位置(/tmp/index.html)

salt ‘*‘ cp.get_url http://www.slashdot.ort /tmp/index.html

5、API調用:

client.cmd(‘*‘,‘cp.get_file‘,[‘salt://script/test.py‘,‘/minion/test.py‘])


四、

cron模組

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

樣本:

1、查看指定被控主機、root使用者的crontab操作

salt ‘wx‘ cron.raw_cron root

2、指定被控主機、root使用者添加/usr/local/weekly任務zuoye

salt ‘wx‘ cron.set_job root ‘*‘ ‘*‘ ‘*‘ ‘*‘ 1 /usr/local/weekly 

3、除指定被控主機、root使用者crontab的/usr/local/weekly任務zuoye

salt ‘wx‘ cron.rm_job root /usr/local/weekly 

 

API調用:

client.cmd(‘wx‘,‘cron.set_job‘,[‘root‘,‘*‘,‘*‘,‘*‘,‘*‘,1,‘/usr/local/weekly‘])


五、

dnsutil模組

功能:實現被控主機通用DNS操作

樣本:

1、加指定被控主機hosts的主機配置項

salt ‘wx‘ dnsutil.hosts_append /etc/hosts 127.0.0.1 adl.yuk.com,ad2.yuk.com

2、除指定被控主機的hosts的主機配置項

salt ‘wx‘ dnsutil.hosts_remove /etc/hosts ad1.yuk.com

API調用:

clietn.cmd(‘wx‘,‘dnsutil.hosts_append‘,[‘/etc/hosts‘,‘127.0.0.1‘,‘ad1.yuk.com‘,‘ad2.yuk.com‘])


六、

file模組

功能:被控主機常見的檔案操作,包括檔案讀寫、許可權、尋找、校正

樣本:

 

1、驗所有被控主機/etc/fstab檔案的md5值是否為xxxxxxxxxxxxx,一致則返回True值

salt ‘*‘ file.check_hash /etc/fstab md5=xxxxxxxxxxxxxxxxxxxxx


1、驗所有被控主機檔案的加密資訊,支援md5、sha1、sha224、shs256、sha384、sha512密碼編譯演算法

salt ‘*‘ file.get_sum /etc/passwd md5


2、改所有被控主機/etc/passwd檔案的屬組、使用者權限、等價於chown root:root /etc/passwd

salt ‘*‘ file.chown /etc/passwd root root


3、制所有被控主機/path/to/src檔案到本地的/path/to/dst檔案

salt ‘*‘ file.copy /path/to/src /path/to/dst


4、查所有被控主機/etc目錄是否存在,存在則返回True,檢查檔案是否存在使用file.file_exists方法

salt ‘*‘ file.directory_exists /etc


5、取所有被控主機/etc/passwd的stats資訊

salt ‘*‘ file.stats /etc/passwd


6、取所有被控主機/etc/passwd的許可權mode,如755,644

salt ‘*‘ file.get_mode /etc/passwd


7、改所有被控主機/etc/passwd的許可權mode為0644

salt ‘*‘ file.set_mode /etc/passwd 0644


8、所有被控主機建立/opt/test目錄

salt ‘*‘ file.mkdir /opt/test


9、所有被控主機/etc/httpd/httpd.conf檔案的LogLevel參數的warn值修改為info

salt ‘*‘ file.sed /etc/httpd/httpd.conf ‘LogLevel warn‘ ‘LogLevel info‘


10、有被控主機的/tmp/test/test.conf檔案追加內容‘maxclient 100’

salt ‘*‘ file.append /tmp/test/test.conf ‘maxclient 100‘


11、所有被控主機的/tmp/foo檔案

salt ‘*‘ file.remove /tmp/foo

 

API調用:

client.cmd(‘*‘,‘file.remove‘,[‘/tmp/foo‘])


七、

iptables模組

功能:被控主機的iptables支援

樣本: 

1、所有被控主機端追加(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 --state RELATED,ESTABLISHED -j ACCEPT‘


2、所有被控主機刪除指定鏈編號為3(position=3)或指定存在的規則

salt ‘*‘ iptalbes.delete filter INPUT position=3

salt ‘*‘ iptables.delete filter INPUT rule=‘-m state --state RELATEC,ESTABLISHED -j ACCEPT‘


3、存所有被控主機端主機規則到本地硬碟(/etc/sysconfig/iptables)

salt ‘*‘ iptables.save /etc/sysconfig/iptables

 

API調用:

client.cmd(‘*‘,‘iptables.append‘,[‘filter‘,‘INPUT‘,‘rule=\‘-p tcp --sport 80 -j ACCEPT\‘‘])

 

八、

network模組

功能:返回被控主機的網路資訊

樣本:

1、指定被控主機擷取dig、ping、traceroute目錄網域名稱資訊

salt ‘wx‘ network.dig www.qq.com

salt ‘wx‘ network.ping www.qq.com

salt ‘wx‘ network.traceroute www.qq.com


2、取指定被控主機的mac地址

salt ‘wx‘ network.hwaddr eth0


3、測指定被控主機是否屬於10.0.0.0/16子網範圍,屬於則返回True

salt ‘wx‘ network.in_subnet 10.0.0.0/16


4、取指定被控主機的網卡配置資訊

salt ‘wx‘ network.interfaces


5、取指定被控主機的IP地址配置資訊

salt ‘wx‘ network.ip_addrs


6、取指定被控主機的子網資訊

salt ‘wx‘ network.subnets

 

API調用:

client.cmd(‘wx‘,‘network.ip_addrs‘)

九、

pkg包管理模組

功能:被控主機程式包管理,如:yum、apt-getdegn


樣本: 

1、所有被控主機安裝PHP環境,根據不同系統發行版調用不同安裝工具進行部署,如redhat平台的yum,等價於yum -y install php

salt ‘*‘ pkg.install php


2、載所有被控主機的PHP環境

salt ‘*‘ pkg.remove php


3、級所有被控主機的軟體包

salt ‘*‘ pkg.upgrade

 

API調用:

client.cmd(‘*‘,‘pkg.remove‘,[‘php‘])

十、

service服務模組

功能:被控主機程式包服務管理


樣本:

1、啟(enable)、禁用(disable)nginx開機自啟動指令碼

salt ‘*‘ service.enable nginx

salt ‘*‘ service.disable nginx

2、對nginx服務的reload、restart、start、stop、status操作

salt ‘*‘ service.reload nginx

salt ‘*‘ service.restart nginx

salt ‘*‘ service.start nginx

salt ‘*‘ service.stop nginx

salt ‘*‘ service.status nginx


API調用:

client.cmd(‘*‘,‘service.stop‘,[‘nginx‘])


Saltstack 基礎模組記錄二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.