Saltstack常用模块及API
Saltstack提供了非常豐富的功能模塊,涉及操作系統的基礎功能、常用工具支持等,更多模塊信息可以查看官網模塊介紹。也可以通過sys模塊列出當前版本支持的模塊。
salt '*' sys.list_modules一、模塊API使用方法:
?API的原理是通過調用 master client 模塊,實例化一個LocalClient 對象,再調用cmd()方法來實現的。以下是API實現test.ping的示例:
>>> import salt.client >>> client = salt.client.LocalClient() >>> ret = client.cmd('*','test.ping') >>> print ret? ? 結果以一個標準的python字典形式的字符串返回,可以通過eval()函數轉換成python的字典類型,方便后續的業務邏輯處理,程序運行結果如下:
{'SN100-129': True, 'SN100-128': True, 'SN100-130': True}二、常見的模塊介紹及使用方法:
(1) Archive模塊
1)功能:實現系統層面的壓縮包調用,支持gzip、gunzip、rar、tar、unrar、unzip等。
2)示例:
#采用gzip壓縮/tmp/sourcefile.txt文件,sourcefile.txt 是在客戶端存在的文件。 [root@localhost ]# salt '*' archive.gzip /tmp/sourcefile.txt #采用gunzip解壓/tmp/sourcefile.txt.gz包[root@localhost~]# salt '*' archive.gunzip /tmp/sourcefile.txt.gz3)API調用:
client.cmd('*','archive.gunzip',['/tmp/sourcefile.txt.gz'])(2) cmd模塊
1)功能:實現遠程的命令行調用執行(默認具備root操作權限,使用時需評估風險)
2)示例:
#獲取所以主機的內存使用情況 [root@localhost~]# salt '*' cmd.run "free -m"#在SN100-128主機運行test.sh腳本,其中script/test.sh存放在file_roots指定的目錄,默認目錄為(/srv/salt) #該命令會做兩個動作:首先同步test.sh到minion的cache目錄(/var/cache/salt/minion/files/base/script/);其次運行該腳本,命令如下:[root@localhost]# salt 'SN100-128' cmd.script salt://script/test.sh3)API調用:
client.cmd('SN100-128','cmd.run',['free -m'])(3) cp 模塊
1) 功能:實現遠程文件、目錄的復制,以及下載URL文件等操作。
2)示例:
#將指定被控主機的/etc/hosts文件復制到被控主機本地的salt cache目錄(/var/cache/salt/minion/localfiles);[root@localhost~]# salt '*' cp.cache_local_file /etc/hosts我的file_roots在/srv/salt下面,目錄結構如下: [root@localhost salt]# tree /srv/salt/ /srv/salt/ ├── apache │?? └── 1.txt ├── mysql ├── nginx │?? └── nginx.conf ├── script │?? ├── test.py │?? └── test.sh └── software└── soft6 directories, 4 files #將主服務器file_roots指定位置下的目錄復制到被控主機[root@locahost salt]# salt '*' cp.get_dir salt://apache/ /minion/dest#將主服務器file_roots指定位置下的文件復制到被控主機[root@localhost ~]# salt '*' cp.get_file salt://nginx/nginx.conf /minion/dest/nginx.conf #復制文件[root@localhost ~]# salt '*' cp.get_file salt://nginx/nginx.conf /minion/dest/test_nginx.conf #復制文件并重命名#下載URL內容到被控主機指定位置(將rpm包下載到salt-minion客戶端的/tmp目錄.)
salt '*' cp.get_url http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release-6-8.noarch.rpm /tmp/epel-releas3)API調用:
client.cmd('SN100-128','cp.get_file',['salt://script/test.py','/tmp/test.py'])(4) cron 模塊
1)功能:實現被控主機的crontab操作。
2)示例:
#查看指定被控主機、root用戶的crontab 清單
[root@localhost~]# salt 'SN100-128' cron.raw_cron root SN100-128:0 */2 * * * /opt/bin/crontab/date.sh#為指定的被控主機、root用戶添加/usr/local/weekly任務作業
[root@localhost~]# salt 'SN100-128' cron.set_job root '*' '*' '*' '*' 1 /usr/local/weekly SN100-128:new #/usr/local/weekly文件在被控主機上是存在的#刪除指定的被控主機、root用戶crontab 的/usr/local/weekly任務作業
[root@localhost~]# salt 'SN100-128' cron.rm_job root /usr/local/weekly SN100-128:removed3)API調用:
>>> import salt.client >>> client = salt.client.LocalClient() >>> ret = client.cmd('SN100-128','cron.set_job',['root','*','*','*','*','*','/usr/echo']) >>> print ret {'SN100-128': 'new'}(5) dnsutil 模塊
1)功能:實現被控主機通用DNS相關操作。
2)示例:
#添加指定被控主機hosts的主機配置項
[root@localhost~]# salt '*' dnsutil.hosts_append /etc/hosts 1.1.1.1 abc.com,bcd.com, SN100-128:The following line was added to /etc/hosts:1.1.1.1 abc.com bcd.com SN100-129:The following line was added to /etc/hosts:1.1.1.1 abc.com bcd.com SN100-130:The following line was added to /etc/hosts:1.1.1.1 abc.com bcd.com#刪除指定被控主機hosts的主機配置項
[root@localhost~]# salt '*' dnsutil.hosts_remove /etc/hosts abc.com SN100-128:None SN100-129:None SN100-130:None3)API 調用
client.cmd('*','dnsutil.hosts_append',['/etc/hosts','127.0.0.1','abc.com'])(6) file 模塊
1)功能:被控主機文件常見操作,包括文件讀寫、權限、查找、校驗等。
2)示例:
#修改所以被控主機/etc/passwd 文件的屬組、用戶權限,等價于chown root:root /etc/passwd
salt '*' file.chown /etc/passwd root root#復制所有被控主機本地/path/to/src 文件到/path/to/dst 文件
salt 'SN100-128' file.copy /root/filename /tmp/filename#將SN100-128主機/root/filename文件復制到/tmp/filename.#檢查所有被控主機/etc 目錄是否存在,存在則返回True,檢查文件是否存在使用file.file_exists方法
[root@localhost~]# salt '*' file.directory_exists /etc SN100-128:True SN100-129:True SN100-130:True#獲取所以被控主機/etc/passwd 的stats信息
[root@localhost~]# salt 'SN100-128' file.stats /etc/passwd SN100-128:----------atime:1465195261.8ctime:1465195234.02gid:0group:rootinode:1572021mode:0644mtime:1462760708.29size:1740target:/etc/passwdtype:fileuid:0user:root#獲取所有被控主機/etc/passwd 的權限mode,如755、644
[root@localhost~]# salt '*' file.get_mode /etc/passwd SN100-128:0644 SN100-129:0644 SN100-130:0644#在所有被控主機創建/opt/test目錄
[root@localhost~]# salt '*' file.mkdir /opt/test SN100-128:None SN100-129:None SN100-130:None#刪除所有被控主機的/tmp/test 文件(文件或目錄)
[root@localhost~]# salt '*' file.remove /opt/test SN100-128:True SN100-130:True SN100-129:True#給所有被控主機的/tmp/test/test.conf 文件追加內容"Maxclient 100"
salt '*' file.append /tmp/test/test.conf "maxclient 100"#將所有被控主機/etc/httpd/httpd.conf 文件的LogLevel 參數warn 值修改成info
salt '*' file.sed /etc/httpd/httpd.conf 'LogLevel warn' 'LogLevel info'#校驗所有被控主機文件的加密信息、支持md5、sha1、sha224、sha256、sha384、sha512加密算法
salt '*' file.get_sum /etc/passwd md53) API調用
client.cmd('*','file.remove',['/tmp/foo'])(7) network 模塊
1)功能:返回被控主機網絡信息。
2)示例:
#在指定被控主機'SN100-128'獲取dig、ping、traceroute目錄域名信息.
salt 'SN100-128' network.dig www.9888.cnsalt 'SN100-128' network.ping www.9888.cnsalt 'SN100-128' network.traceroute www.9888.cn#獲取指定被控主機‘SN100-128’的 MAC 地址
[root@localhost~]# salt 'SN100-128' network.hwaddr eth0 SN100-128:00:50:56:87:b1:54#獲取指定被控主機‘SN100-128’的網卡配置信息
salt 'SN100-128' network.interfaces#獲取指定被控主機'SN100-128'的IP地址配置信息
[root@localhost~]# salt '*' network.ip_addrs SN100-128:- 10.10.100.128#獲取指定被控主機‘SN100-128’的子網信息
[root@localhost~]# salt 'SN100-128' network.subnets SN100-128:- 10.10.100.0/24#檢測指定被控主機'SN100-128'是否屬于10.0.0.0/16子網范圍,屬于則返回True.
salt '*' network.in_subnet 10.0.0.0/163) API調用:
>>> import salt.client >>> >>> client = salt.client.LocalClient() >>> ret = client.cmd('SN100-128','network.ip_addrs') >>> >>> print ret {'SN100-128': ['10.10.100.128']} >>>(8) pkg 包管理模塊
1)功能:被控主機程序包管理,如yum、apt-get等。
2)示例:
#為被控主機安裝PHP環境,根據不同系統發行版調用不同安裝工具進行部署,如redhat平臺的yum,等價于yum -y install php
salt 'SN100-128' pkg.install php#卸載被控端主機的PHP環境
salt 'SN100-128' pkg.remove php#升級被控端主機的軟件包
salt '*' pkg.upgrade3)API調用:
client.cmd('SN100-128','pkg.remove',['php'])(9) Service 服務模塊
1)功能:被控主機程序包服務管理。
2)示例:
#開啟(enable)、禁用(disable) nginx開機自啟動服務
salt 'SN100-128' service.enable nginx salt 'SN100-128' service.disable nginx#針對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 nginx3) API調用:
client.cmd('SN100-128','service.stop',['nginx'])其他模塊:
通過上面介紹的幾個常用模塊,基本上已經覆蓋日常運維操作。Saltstack還提供了以下模塊.
user(系統用戶模塊)
group(系統組模塊)
partition(系統分區模塊)
puppet(puppet管理模塊)
system(系統重啟、關機模塊)
timezone(時區管理模塊)
nginx(Nginx管理模塊)
iptables(被控主機iptables支持模塊)
mount(文件系統掛載模塊) 等等...
更多模塊查看官網介紹:
http://docs.saltstack.com/ref/modules/all/index.html#all-salt-modules
當然也可以通過python擴展功能模塊來滿足需求...
?
總結
以上是生活随笔為你收集整理的Saltstack常用模块及API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【7集iCore3基础视频】7-2 iC
- 下一篇: 【转载】使用LR测试Oracle数据库的