首頁 後端開發 Python教學 Python的自动化部署模块Fabric的安装及使用指南

Python的自动化部署模块Fabric的安装及使用指南

Jun 10, 2016 pm 03:06 PM
fabric python

fabric是python2.5或者更高的库,可以通过ssh在多个host上批量执行任务.完成系统管理任务.它提供一套基本操作在本地和远程执行shell命令,或者上传下载文件,辅助提供用户输入或终止执行.

下面安装fabric模块有2种方法:

1.使用easy_install(下面是debain5环境)

root@10.1.6.200:pshell# apt-get install python-dev (安装Python头文件)
 
root@10.1.6.200:pshell# apt-get install python-setuptools (安装easy_install)
 
root@10.1.6.200:pshell# wget http://peak.telecommunity.com/dist/ez_setup.py
 
root@10.1.6.200:pshell# python ez_setup.py
 
root@10.1.6.200:pshell# easy_install fabric
登入後複製

Searching for fabric
Reading http://pypi.python.org/simple/fabric/
Best match: Fabric 1.6.1
Downloading http://pypi.python.org/packages/source/F/Fabric/Fabric-1.6.1.tar.gz#md5=c318ac3f7011ede0be1ca9a20f435735
Processing Fabric-1.6.1.tar.gz
Running Fabric-1.6.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-CVuLrs/Fabric-1.6.1/egg-dist-tmp-ZFNoWY
warning: no previously-included files matching '*' found under directory 'docs/_build'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
warning: no previously-included files matching '*.pyo' found under directory 'tests'
zip_safe flag not set; analyzing archive contents...
fabric.version: module references __file__
Adding Fabric 1.6.1 to easy-install.pth file
Installing fab script to /usr/bin
....
Installed /usr/lib/python2.5/site-packages/pycrypto-2.6-py2.5-linux-x86_64.egg
Finished processing dependencies for fabric
登入後複製

2.使用pip(下面使用的是debian7环境)

apt-get install python-pip
pip install fabric
apt-get install python-paramiko
登入後複製

导入模块未报错说明安装成功.


实例:

1.在调用fabric的时候使用命令行参数,-H 指定哪台主机

root@10.1.6.201:python# cat fabfile4.py 
登入後複製

#!/usr/bin/env python
#coding=utf-8
from fabric.api import *
 
def printMem():
   cmd_output = run('free -m')
   print cmd_output
登入後複製

root@10.1.6.201:python# fab -H root@10.1.1.45 printMem -f fabfile4.py
[root@10.1.1.45] Executing task 'printMem'
[root@10.1.1.45] run: free -m
[root@10.1.1.45] Login password for 'root': #提示输入密码
[root@10.1.1.45] out:       total    used    free   shared  buffers   cached
[root@10.1.1.45] out: Mem:     1005    968     37     0     36    831
[root@10.1.1.45] out: -/+ buffers/cache:    100    904
[root@10.1.1.45] out: Swap:     1913     0    1913
[root@10.1.1.45] out: 
登入後複製


total    used    free   shared  buffers   cached
Mem:     1005    968     37     0     36    831
-/+ buffers/cache:    100    904
Swap:     1913     0    1913
 
Done.
Disconnecting from 10.1.1.45:22000... done.
登入後複製

2.以上我们需要输入密码才能完成操作,怎样自动执行呢?可以在fabfile4文件中配置HOST.也就是环境变量.

root@10.1.6.201:python# vim fabfile.py
登入後複製

#!/usr/bin/env python
#coding=utf-8
from fabric.api import *
from fabric.context_managers import *
 
env.host_string = '10.1.1.45'
env.port = '22000'       #默认端口22,默认登录用户root
env.password='passwd'
 
def test1():
  with cd('/home'):
    run('ls -l')
 
test1()

登入後複製

root@10.1.6.201:python# python fabfile.py  #脚本执行
登入後複製

[10.1.1.45] run: ls -l
[10.1.1.45] out: total 8
[10.1.1.45] out: drwxr-xr-x 2 debian debian 4096 2012-08-27 11:54 debian
[10.1.1.45] out: drwxr-xr-x 2 root  nogroup 4096 2013-05-22 18:07 ftp
[10.1.1.45] out:

登入後複製

root@10.1.6.201:python# vim fabfile1.py
登入後複製

#!/usr/bin/env python
#coding=utf-8
from fabric.api import *
from fabric.context_managers import *
 
env.hosts = ['10.1.6.200','10.1.1.45']
env.port = '22000'
env.password='passwd'
 
def test1():
  with cd('/home'): #更改目录
    run('ls -l')

登入後複製

root@10.1.6.201:python# fab test1 -f fabfile.py #使用fab指定任务执行,注意文件后默认跟fabfile.py
登入後複製

[10.1.6.200] Executing task 'test1'
[10.1.6.200] run: ls -l
[10.1.6.200] out: total 24
[10.1.6.200] out: drwxr-xr-x 2 davehe davehe 4096 2013-02-27 10:00 davehe
[10.1.6.200] out: -rw-r--r-- 1 root  root  1990 2013-02-27 09:55 davehe.tar.gz
[10.1.6.200] out: -rw-r--r-- 1 root  root  396 2013-05-17 18:27 rsync_log_130517
[10.1.6.200] out: -rw-r--r-- 1 root  root  7916 2013-05-20 21:04 rsync_log_130520
[10.1.6.200] out: drwxr-xr-x 2 taomee taomee 4096 2013-01-29 04:27 taomee
[10.1.6.200] out: 
 
[10.1.1.45] Executing task 'test1'
[10.1.1.45] run: ls -l
[10.1.1.45] out: total 8
[10.1.1.45] out: drwxr-xr-x 2 debian debian 4096 2012-08-27 11:54 debian
[10.1.1.45] out: drwxr-xr-x 2 root  nogroup 4096 2013-05-22 18:07 ftp
[10.1.1.45] out: 
 
 
Done.
Disconnecting from 10.1.1.45:22000... done.
Disconnecting from 10.1.6.200:22000... done.

登入後複製

3.使用get/put.利用sftp协议上传下载文件

root@10.1.6.201:python# cat fabfile1.py
登入後複製


#!/usr/bin/env python
#coding=utf-8
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
 
env.hosts = ['10.1.1.45']
env.port = '22000'
env.password='passwd'
 
def test1():
  print(red("i'm 201"))
  local('ls -l /tmp')
 
def test2():
  print (green("i'm get file 45 to 186"))
  get('/home/ftp/a.txt','/tmp/') #下载
#  put('/tmp/','/home/ftp/') #上传
  local('ls -l /tmp')  #local运行本地命令
 
 
def final():
  execute(test1)
  execute(test2)
登入後複製


root@10.1.6.201:python# fab final -f fabfile1.py
登入後複製

[10.1.1.45] Executing task 'final'
[10.1.1.45] Executing task 'test1'
i'm 201
[localhost] local: ls -l /tmp
total 31684
drwxr-xr-x 2 root root   4096 May 13 22:08 bin
drwxr-xr-x 3 root root   4096 May 13 22:08 conf
drwxr-xr-x 6 root root   4096 May 13 22:08 etc
-rwxr-xr-x 1 root root   6797 May 13 22:08 init
-rw-r--r-- 1 root root 32400896 May 13 22:07 initrd.img-3.2.0-4-amd64
drwxr-xr-x 6 root root   4096 May 13 22:08 lib
drwxr-xr-x 2 root root   4096 May 13 22:08 lib64
drwxr-xr-x 2 root root   4096 May 13 22:08 run
drwxr-xr-x 2 root root   4096 May 13 22:08 sbin
drwxr-xr-x 6 root root   4096 May 13 22:08 scripts
[10.1.1.45] Executing task 'test2'
i'm get file 45 to 186
[10.1.1.45] download: /tmp/a.txt <- /home/ftp/a.txt
[localhost] local: ls -l /tmp
total 31688
-rw-r--r-- 1 root root    6 May 29 22:29 a.txt
drwxr-xr-x 2 root root   4096 May 13 22:08 bin
drwxr-xr-x 3 root root   4096 May 13 22:08 conf
drwxr-xr-x 6 root root   4096 May 13 22:08 etc
-rwxr-xr-x 1 root root   6797 May 13 22:08 init
-rw-r--r-- 1 root root 32400896 May 13 22:07 initrd.img-3.2.0-4-amd64
drwxr-xr-x 6 root root   4096 May 13 22:08 lib
drwxr-xr-x 2 root root   4096 May 13 22:08 lib64
drwxr-xr-x 2 root root   4096 May 13 22:08 run
drwxr-xr-x 2 root root   4096 May 13 22:08 sbin
drwxr-xr-x 6 root root   4096 May 13 22:08 scripts
 
Done.
Disconnecting from 10.1.1.45:22000... done.

登入後複製


上面实例中只列举了几个常用的farbic环境变量.如env.hosts,env.password等,可以不需要交互输入密码.

以下还有常用环境变量以供参考:

  • exclude_hosts:指定一个主机列表,在fab执行时,忽略列表中的机器
  • user:ssh使用哪个用户登录远程主机
  • hosts :全局的host列表
  • host_string :当fabric连接远程机器执行run、put时,设置的user/host/port等
  • password:默认ssh连接远程主机密码,也可以是sudo提示输入密码
  • password:一个字典供内部使用,为每台主机host设置密码,key是主机,value值存放密码
  • port:设置默认端口
  • roledefs:使用字典定义角色名字对应的主机ip
  • roles:一个全局的role列表
from fabric.api import run, roles
 
env.roledefs = {
  'db': ['db1', 'db2'],
  'web': ['web1', 'web2', 'web3'],
}
 
@roles('db')
def migrate():
  # Database stuff here.
  pass
 
@roles('web')
def update():
  # Code updates here.
  pass

登入後複製

fab也可以使用命令设置环境变量,常用命令

  • -f FABFILE, --fabfile=FABFILE  默认fabfile.py
  • -H HOSTS, --hosts=HOSTS     env.hosts=hosts
  • -p PASSWORD, --password=PASSWORD  env.password
  • -R ROLES, --roles=ROLES   env.roles
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1669
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

在PHP和Python之間進行選擇:指南 在PHP和Python之間進行選擇:指南 Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

sublime怎麼運行代碼python sublime怎麼運行代碼python Apr 16, 2025 am 08:48 AM

在 Sublime Text 中運行 Python 代碼,需先安裝 Python 插件,再創建 .py 文件並編寫代碼,最後按 Ctrl B 運行代碼,輸出會在控制台中顯示。

PHP和Python:深入了解他們的歷史 PHP和Python:深入了解他們的歷史 Apr 18, 2025 am 12:25 AM

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

Python vs. JavaScript:學習曲線和易用性 Python vs. JavaScript:學習曲線和易用性 Apr 16, 2025 am 12:12 AM

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

Golang vs. Python:性能和可伸縮性 Golang vs. Python:性能和可伸縮性 Apr 19, 2025 am 12:18 AM

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

vscode在哪寫代碼 vscode在哪寫代碼 Apr 15, 2025 pm 09:54 PM

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

notepad 怎麼運行python notepad 怎麼運行python Apr 16, 2025 pm 07:33 PM

在 Notepad 中運行 Python 代碼需要安裝 Python 可執行文件和 NppExec 插件。安裝 Python 並為其添加 PATH 後,在 NppExec 插件中配置命令為“python”、參數為“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通過快捷鍵“F6”運行 Python 代碼。

See all articles