1、簡介html
Fabric是基於Python 2.5及以上版本實現的SSH命令行工具,簡化了SSH了應用程序部署及系統管理任務,它提供了系統基礎的操做組件,能夠實現本地或遠程shell命令,包括命令執行,文件上傳,下載及完整執行日誌輸出等功能。Fabric在paramiko的基礎上作了更高一層的封裝,操做起來會更簡單.python
Fabric官方文檔:http://www.fabfile.org/ios
API文檔:http://docs.fabfile.org/en/1.10/c++
基礎案例文檔:http://docs.fabfile.org/en/1.10/tutorial.htmlshell
Fabric中文文檔:http://fabric-docs-cn.readthedocs.org/zh_CN/latest/api
2、安裝bash
(1) 安裝epel源 rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm sed -i 's/^#//' /etc/yum.repos.d/epel.repo sed -i 's/mirrorlist/#mirrorlist/' /etc/yum.repos.d/epel.repo (2)安裝依賴包 yum install gcc gcc-c++ python-devel openssl-devel openssl zlin zlib-devel -y (3)安裝pip yum install python-pip -y (4)安裝fabric pip install fabric (5)測試fabric是否安裝正確 python -c 'import fabric'
3、Fabric的應用多線程
注意事項:fab命令引用默認文件名爲fabfile.py,若是使用非默認文件名稱,則須要經過-f來執行,如ide
fab -H 192.168.1.100,192.168.1.105 -f host_type.py host_type函數
若是管理機與目標主機未配置祕鑰認證信任,將會提示輸入目標主機對應帳號登陸密碼。
fab做爲fabric程序的命令行入口,提供了豐富的參數調用
工做中的應用場景:因爲目前咱們用的都是雲平臺,好比阿里雲、騰訊雲、以前還用過一段時間的ucloud等等,用起來效果仍是挺好的,有時候爲了更方便的管理,對系統進行優化,安裝一些agent(zabbix,saltstack,network等),這個時候咱們就能夠用fabric進行操做,感受效果挺好的。
因爲fabric是單線程工做的,以前我想將其改爲多線程,可是沒有成功,若是有朋友應該怎麼修改,也請麻煩告訴我一聲,謝謝啦,多交朋友多脈圈,哈哈
在這裏分享一個febric的腳本
#!/usr/bin/env python #encoding: utf-8 from fabric.api import * from fabric.colors import * from fabric.context_managers import * from fabric.contrib.console import confirm import os #定義目標主機信息 env.user='root' env.hosts=['192.168.0.141',] env.password='redhat' #定義目錄結構 LocalDir = "/home/saltroot/gameroot" RemoteDir = "/home/saltclient/gameroot/" LocalFile = os.path.join(LocalDir,"script.tar.gz") RemoteFile = os.path.join(RemoteDir,"script.tar.gz") #打包文件 def tar_task(): with lcd(LocalDir): local("tar -zcf script.tar.gz script") #上傳文件 def put_task(): run("mkdir -p %s" % RemoteDir) with settings(warn_only=True): #put上傳出現異常時繼續執行,非終止 result = put(LocalFile,RemoteFile) if result.failed and not confirm("put file failed, Continue[Y/N]?"): abort("Aborting file put task!") #出現異常時,確認是否繼續,(Y繼續) #校對文件 def check_task(): with settings(warn_only=True): lmd5=local("md5sum %s" % LocalFile,capture=True).split(' ')[0] rmd5=run("md5sum %s" % RemoteFile).split(' ')[0] if lmd5==rmd5: #對比本地及遠程文件的md5信息 print yellow("OK") else: print red("ERROR") #初始化 def agent_task(): with cd(RemoteDir): run("tar -zxf script.tar.gz") with cd("script/"): run("./init.sh") #4個功能一塊兒實現 @task #限定只有go函數對fab可見 def go(): print yellow("program start ...") tar_task() put_task() check_task() agent_task() print green("program sucessful ...") ############################################ # 命令執行方式 # fab go # 額外的命令 # @roles('new') # def show(): # print green('success') # print red('fail') # print yellow('yellow') #定義業務角色 #env.user='root' #env.roledefs = { # 'new': ['192.168.0.100',], # 'ios': ['192.168.0.130','192.168.0.101'], # 'Andorid': ['192.168.0.200', '192.168.0.201', '192.168.0.230'] #} # #env.passwords = { # 'root@192.168.0.100:22': 'redhat', # 'root@192.168.0.120:22': 'redhat' #} ############################################