Python fabric远程自动部署简介

    xiaoxiao2022-07-16  152

    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. 最新内容请见作者的GitHub页:http://qaseven.github.io/ 相关资源:敏捷开发V1.0.pptx
    最新回复(0)