国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python的自動化部署模塊Fabric的安裝及使用指南

2020-01-04 17:50:15
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Python的自動化部署模塊Fabric的安裝及使用指南,文中以Debian系統為環境進行了實例演示,需要的朋友可以參考下
 

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 fabricReading http://pypi.python.org/simple/fabric/Best match: Fabric 1.6.1Downloading http://pypi.python.org/packages/source/F/Fabric/Fabric-1.6.1.tar.gz#md5=c318ac3f7011ede0be1ca9a20f435735Processing Fabric-1.6.1.tar.gzRunning Fabric-1.6.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-CVuLrs/Fabric-1.6.1/egg-dist-tmp-ZFNoWYwarning: 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 fileInstalling fab script to /usr/bin....Installed /usr/lib/python2.5/site-packages/pycrypto-2.6-py2.5-linux-x86_64.eggFinished processing dependencies for fabric

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

apt-get install python-pippip install fabricapt-get install python-paramiko

導入模塊未報錯說明安裝成功.


實例:

1.在調用fabric的時候使用命令行參數,-H 指定哪臺主機

root@10.1.6.201:python# cat fabfile4.py 
#!/usr/bin/env python#coding=utf-8from 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   cachedMem:     1005    968     37     0     36    831-/+ buffers/cache:    100    904Swap:     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-8from fabric.api import *from fabric.context_managers import * env.host_string = '10.1.1.45'env.port = '22000'       #默認端口22,默認登錄用戶rootenv.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-8from 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-8from 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 /tmptotal 31684drwxr-xr-x 2 root root   4096 May 13 22:08 bindrwxr-xr-x 3 root root   4096 May 13 22:08 confdrwxr-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-amd64drwxr-xr-x 6 root root   4096 May 13 22:08 libdrwxr-xr-x 2 root root   4096 May 13 22:08 lib64drwxr-xr-x 2 root root   4096 May 13 22:08 rundrwxr-xr-x 2 root root   4096 May 13 22:08 sbindrwxr-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 /tmptotal 31688-rw-r--r-- 1 root root    6 May 29 22:29 a.txtdrwxr-xr-x 2 root root   4096 May 13 22:08 bindrwxr-xr-x 3 root root   4096 May 13 22:08 confdrwxr-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-amd64drwxr-xr-x 6 root root   4096 May 13 22:08 libdrwxr-xr-x 2 root root   4096 May 13 22:08 lib64drwxr-xr-x 2 root root   4096 May 13 22:08 rundrwxr-xr-x 2 root root   4096 May 13 22:08 sbindrwxr-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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 祁东县| 枣强县| 水富县| 阿荣旗| 孟连| 舟曲县| 阳谷县| 连城县| 湘乡市| 仁布县| 克山县| 万宁市| 灵宝市| 巫溪县| 曲靖市| 札达县| 当阳市| 边坝县| 汪清县| 邢台市| 定边县| 孝昌县| 玛纳斯县| 奇台县| 句容市| 阿荣旗| 建德市| 余江县| 重庆市| 城步| 无锡市| 桓台县| 南宁市| 大理市| 金川县| 连城县| 垦利县| 开平市| 黄大仙区| 马公市| 额尔古纳市|