saltstack常用模塊及API
列出當前版本支持的模塊
salt '*' sys.list_modules
API原理是通過調(diào)用master client模塊,實例化一個LocalClient對象,再調(diào)用cmd()方法來實現(xiàn)的。
以下API實現(xiàn)test.ping的示例:(其他API調(diào)用只需要改變cmd即可)
import salt.client
client = salt.client.LocalClient()
ret = client.cmd('*','test.ping) #cmd內(nèi)格式:'<操作目標>','<模塊>','[參數(shù)]'。例:'*','cmd.run',['df -h'] print ret
常用模塊
Archive模塊
功能:實現(xiàn)系統(tǒng)層面的壓縮包調(diào)用,支持gzip、gunzip、rar、tar、unrar、unzip等
示例:
#采用gunzip解壓sourcefile.txt.gz包
salt '*' archive.gunzip sourcefile.txt.gz
#采用gzip壓縮sourcefile.txt文件
salt '*' archive.gzip sourcefile.txt
API調(diào)用:
client.cmd('*','archive.gunzip',['sourcefile.txt.gz'])
cmd模塊
功能:實現(xiàn)遠程的命令行調(diào)用執(zhí)行(默認具備root操作權(quán)限,使用時需評估風險)
示例:
#獲取所欲被控主機的內(nèi)存使用情況
salt '*' cmd.run 'free -m'
#在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
API調(diào)用:
client.cmd('*','cmd.run',['free -m'])
cp模塊
功能:實現(xiàn)遠程文件、目錄的復制,以及下載URL文件等操作
示例:
#將被控主機的/etc/hosts文件復制到被控主機本地的salt cache目錄(/var/cache/salt/minion/localfiles/)
salt '*' cp.cache_local_file /etc/hosts
#將主控端file_roots指定位置下的目錄復制到被控主機/minion/目錄下
salt '*' cp.get_dir salt://script/ /minion/
#將主控端file_roots指定位置下的文件復制到被控主機/minion/test.py文件(file為文件名)
salt '*' cp.get_dir salt://script/test.py /minion/test.py
#下載URL內(nèi)容到被控主機指定位置(/tmp/index.html)
salt '*' cp.get_url http://www.slashdot.ort /tmp/index.html
API調(diào)用:
client.cmd('*','cp.get_file',['salt://script/test.py','/minion/test.py'])
cron模塊
功能:實現(xiàn)被控主機的crontab操作
示例:
#查看指定被控主機、root用戶的crontab操作
salt 'wx' cron.raw_cron root
#為指定被控主機、root用戶添加/usr/local/weekly任務(wù)zuoye
salt 'wx' cron.set_job root '*' '*' '*' '*' 1 /usr/local/weekly
#刪除指定被控主機、root用戶crontab的/usr/local/weekly任務(wù)zuoye
salt 'wx' cron.rm_job root /usr/local/weekly
API調(diào)用:
client.cmd('wx','cron.set_job',['root','*','*','*','*',1,'/usr/local/weekly'])
dnsutil模塊
功能:實現(xiàn)被控主機通用DNS操作
示例:
#添加指定被控主機hosts的主機配置項
salt 'wx' dnsutil.hosts_append /etc/hosts 127.0.0.1 adl.yuk.com,ad2.yuk.com
#刪除指定被控主機的hosts的主機配置項
salt 'wx' dnsutil.hosts_remove /etc/hosts ad1.yuk.com
API調(diào)用:
clietn.cmd('wx','dnsutil.hosts_append',['/etc/hosts','127.0.0.1','ad1.yuk.com','ad2.yuk.com'])
file模塊
功能:被控主機常見的文件操作,包括文件讀寫、權(quán)限、查找、校驗
示例:
#校驗所有被控主機/etc/fstab文件的md5值是否為xxxxxxxxxxxxx,一致則返回True值
salt '*' file.check_hash /etc/fstab md5=xxxxxxxxxxxxxxxxxxxxx
#校驗所有被控主機文件的加密信息,支持md5、sha1、sha224、shs256、sha384、sha512加密算法
salt '*' file.get_sum /etc/passwd md5
#修改所有被控主機/etc/passwd文件的屬組、用戶權(quán)限、等價于chown root:root /etc/passwd
salt '*' 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的權(quán)限mode,如755,644
salt '*' file.get_mode /etc/passwd
#修改所有被控主機/etc/passwd的權(quán)限mode為0644
salt '*' file.set_mode /etc/passwd 0644
#在所有被控主機創(chuàng)建/opt/test目錄
salt '*' file.mkdir /opt/test
#將所有被控主機/etc/httpd/httpd.conf文件的LogLevel參數(shù)的warn值修改為info
salt '*' file.sed /etc/httpd/httpd.conf 'LogLevel warn' 'LogLevel info'
#給所有被控主機的/tmp/test/test.conf文件追加內(nèi)容‘maxclient 100’
salt '*' file.append /tmp/test/test.conf 'maxclient 100'
#刪除所有被控主機的/tmp/foo文件
salt '*' file.remove /tmp/foo
API調(diào)用:
client.cmd('*','file.remove',['/tmp/foo'])
iptables模塊
功能:被控主機的iptables支持
示例:
#在所有被控主機端追加(append)、插入(insert)iptables規(guī)則,其中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'
#在所有被控主機刪除指定鏈編號為3(position=3)或指定存在的規(guī)則
salt '*' iptalbes.delete filter INPUT position=3 salt '*' iptables.delete filter INPUT rule='-m state --state RELATEC,ESTABLISHED -j ACCEPT'
#保存所有被控主機端主機規(guī)則到本地硬盤(/etc/sysconfig/iptables)
salt '*' iptables.save /etc/sysconfig/iptables
API調(diào)用:
client.cmd('*','iptables.append',['filter','INPUT','rule=\'-p tcp --sport 80 -j ACCEPT\''])
network模塊
功能:返回被控主機的網(wǎng)絡(luò)信息
示例:
#在指定被控主機獲取dig、ping、traceroute目錄域名信息
salt 'wx' network.dig www.qq.com
salt 'wx' network.ping www.qq.com
salt 'wx' network.traceroute www.qq.com
#獲取指定被控主機的mac地址
salt 'wx' network.hwaddr eth0
#檢測指定被控主機是否屬于10.0.0.0/16子網(wǎng)范圍,屬于則返回True
salt 'wx' network.in_subnet 10.0.0.0/16
#獲取指定被控主機的網(wǎng)卡配置信息
salt 'wx' network.interfaces
#獲取指定被控主機的IP地址配置信息
salt 'wx' network.ip_addrs
#獲取指定被控主機的子網(wǎng)信息
salt 'wx' network.subnets
API調(diào)用:
client.cmd('wx','network.ip_addrs')
pkg包管理模塊
功能:被控主機程序包管理,如:yum、apt-getdegn
示例:
#為所有被控主機安裝PHP環(huán)境,根據(jù)不同系統(tǒng)發(fā)行版調(diào)用不同安裝工具進行部署,如redhat平臺的yum,等價于yum -y install php
salt '*' pkg.install php
#卸載所有被控主機的PHP環(huán)境
salt '*' pkg.remove php
#升級所有被控主機的軟件包
salt '*' pkg.upgrade
API調(diào)用:
client.cmd('*','pkg.remove',['php'])
service服務(wù)模塊
功能:被控主機程序包服務(wù)管理
示例:
#開啟(enable)、禁用(disable)nginx開機自啟動腳本
salt '*' service.enable nginx
salt '*' service.disable nginx
#針對nginx服務(wù)的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調(diào)用:
client.cmd('*','service.stop',['nginx'])
其他模塊
除了上述模塊外,saltstack還提供了:
user(系統(tǒng)用戶模塊)
group(系統(tǒng)組模塊)
partition(系統(tǒng)分區(qū)模塊)
puppet(puppet管理模塊)
system(系統(tǒng)重啟、關(guān)機模塊)
timezone(時區(qū)管理模塊)
nginx(nginx管理模塊)
mount(文件系統(tǒng)掛載模塊)