Python fabric遠程自動部署簡介
Fabric是一個Python(2.5-2.7)庫,用于簡化使用SSH的應用程序部署或系統管理任務。
它提供的操作包括:執行本地或遠程shell命令,上傳/下載文件,以及其他輔助功能,如提示用戶輸入、中止執行等。
本文主要介紹CentOS 6.3上使用fabric進行自動部署的基本方法。
1. 環境部署
本節主要介紹python版本升級,pip及fabric部署方法。
1.1. Python版本升級
CentOS 6.3自帶的Python版本為2.6,首先需要升級到2.7版本。由于舊版本的Python已被深度依賴,所以不能卸載原有的Python,只能全新安裝。
1. 下載Pyhon,選擇下載Gzipped source tar ball (2.7.6) (sig),網址:https://www.python.org/download/releases/2.7.6
2. 解壓安裝,命令如下
tar -xvf Python-2.7.6.tgz
cd Python-2.7.6
./configure --prefix=/usr/local/python2.7
make
make install
3. 創建鏈接來使系統默認python變為python2.7
ln -fs /usr/local/python2.7/bin/python2.7 /usr/bin/python
4. 查看Python版本
python –V
5. 修改yum配置(否則yum無法正常運行)
vi /usr/bin/yum
將第一行的#!/usr/bin/python修改為系統原有的python版本地址#!/usr/bin/python2.6
至此CentOS6.3系統Python已成功升級至2.7.6版本。
1.2. 安裝pip
Pip是一個安裝和管理python包的工具。
安裝方法如下:
1. 下載pip,地址https://raw.github.com/pypa/pip/master/contrib/get-pip.py
2. 執行安裝命令
python get-pip.py
3. 創建連接(否則會報錯提示“命令不存在”)
ln -s /usr/local/python2.7/bin/pip /usr/bin/pip
1.3. 安裝fabric
1. 執行安裝命令
pip install fabric
2. 創建連接(否則會報錯提示“命令不存在”)
ln -s /usr/local/python2.7/bin/fab /usr/bin/fab
2. 腳本編寫
本節對fabric用法進行簡單介紹,并提供實例以供參考。
2.1. Hello,fab
1. 在當前目錄下新建文件fabfile.py,輸入內容如下
def hello():
print("Hello fab!")
2. 執行命令fab hello,結果如下
# fab hello
Hello fab!
3. 文件名不為fabfile.py時需進行指定
# mv fabfile.py test.py
# fab hello
Fatal error: Couldn't find any fabfiles!
Remember that -f can be used to specify fabfile path, and use -h for help.
# fab -f test.py hello
Hello fab!
4. 參數傳遞
#vi fabfile.py
def hello(name):
print 'Hello %s!'%name
# fab hello:name=fab
Hello fab!
# fab hello:fab
Hello fab!
2.2. 本地操作
執行本地操作命令使用local
1. fabfile.py腳本內容如下
from fabric.api import local
def test():
local('cd /home/')
local('ls -l|wc -l')
2. 執行命令fab test,結果如下
# fab test
[localhost] local: cd /home/
[localhost] local: ls -l|wc -l
2.3. 遠程操作
執行遠程操作命令使用run
1. fabfile.py腳本內容如下
from fabric.api import cd,run,env,hosts
env.hosts=['192.168.85.99:22','192.168.85.101:22']
env.password='test'
def test():
with cd('/home'):
run("du -sh")
2. 執行命令fab test,結果如下
# fab test
[192.168.85.99:22] Executing task 'test'
[192.168.85.99:22] run: du -sh
[192.168.85.99:22] out: 392G .
[192.168.85.99:22] out:
[192.168.85.101:22] Executing task 'test'
[192.168.85.101:22] run: du -sh
[192.168.85.101:22] out: 5.6G .
[192.168.85.101:22] out:
Disconnecting from 192.168.85.99... done.
Disconnecting from 192.168.85.101... done.
3. 多服務器混合,需要在不同服務器進行不同操作時,可參考如下腳本
from fabric.api import env,roles,run,execute
env.roledefs = {
'server1': ['root@192.168.85.99:22',],
'server2': ['root@192.168.85.100:22', ]
}
env.password = 'test'
@roles('server1')
def task1():
run('ls /home/ -l | wc -l')
@roles('server2')
def task2():
run('du -sh /home')
def test():
execute(task1)
execute(task2)
結果如下
# fab test [root@192.168.85.99:22] Executing task 'task1' [root@192.168.85.99:22] run: ls /home/ -l | wc -l [root@192.168.85.99:22] out: 27 [root@192.168.85.99:22] out: [root@192.168.85.100:22] Executing task 'task2' [root@192.168.85.100:22] run: du -sh /home [root@192.168.85.100:22] out: 1.4G /home [root@192.168.85.100:22] out: Disconnecting from 192.168.85.99... done. Disconnecting from 192.168.85.100... done. |
posted on 2014-07-16 09:48 順其自然EVO 閱讀(411) 評論(0) 編輯 收藏 所屬分類: 測試學習專欄