两台机器:10.1.6.186、10.1.6.159。fabric部署在10.1.6.186上面

1  执行和1相同的任务,不过排除掉10.1.6.159这台机器

 1 #!/usr/bin/python
2 from fabric.api import *
3 from fabric.context_managers import *
4
5 env.hosts=['10.1.6.186','10.1.6.159']
6 env.password='xxxxxx'
7 env.exclude_hosts=['10.1.6.159']
8
9 def task1():
10 with cd('/home/guol'):
11 run('ls -l') fab task1

2 执行和2相同任务,再增加一个task2,并且把taskN伪装成meta任务执行

#!/usr/bin/python
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import * env.hosts=['10.1.6.186','10.1.6.159']
env.password='xxxxxx'
env.exclude_hosts=['10.1.6.159'] def task1():
with cd('/home/guol'):
run('ls -l') def task2():
print(green("I'm fabric")) def deploy():
execute(task1)
execute(task2) ##执行
root@vm11:/tmp# fab deploy

3 不同的机器执行不同的task

#!/usr/bin/python
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import * env.roledefs={'web1':['10.1.6.186'],'web2':['10.1.6.159']}
env.password='xxxxxx' @roles('web1')
def task1():
with cd('/home/guol'):
run('ls -l')
@roles('web2')
def task2():
print(green("I'm fabric")) def deploy():
execute(task1)
execute(task2)
##执行
root@vm11:/tmp# fab deploy

4 把159的/home/guol/159-remote拉取到186的 /home/guol/目录下

#!/usr/bin/python
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
env.hosts=['10.1.6.159']
env.password='xxxxxx' def task1():
print(green("I'm 186 /home/guol/"))
local('ls -l /home/guol')
def task2():
print(green("I'm get 159's 159-remote file to 186"))
get('/home/guol/159-remote','/home/guol')
print(yellow("I'm 186 /home/guol/"))
local('ls -l /home/guol') def deploy():
execute(task1)
execute(task2) ##执行
root@vm11:/tmp# fab deploy
[10.1.6.159] Executing task 'deploy'
[10.1.6.159] Executing task 'task1'
I'm 186 /home/guol/
[localhost] local: ls -l /home/guol
total 0
-rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
[10.1.6.159] Executing task 'task2'
I'm get 159's 159-remote file to 186
[10.1.6.159] download: /home/guol/159-remote <- /home/guol/159-remote
I'm 186 /home/guol/
[localhost] local: ls -l /home/guol
total 0
-rw-r--r-- 1 root root 0 Dec 21 14:28 159-remote
-rw-r--r-- 1 root root 0 Dec 21 13:32 186-local Done.
Disconnecting from 10.1.6.159... done.

5 把186的/home/guol/ 186-local推送到159的 /home/guol/目录下

#!/usr/bin/python
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
env.hosts=['10.1.6.159']
env.password='xxxxxx' def task1():
print(green("I'm 159 /home/guol/"))
with cd('/home/guol'):
run('ls -l')
def task2():
print(green("I'm put 186's 186-local file to 159"))
put('/home/guol/186-local','/home/guol')
print(yellow("I'm 159 /home/guol/"))
with cd('/home/guol'):
run('ls -l')
def deploy():
execute(task1)
execute(task2) ##执行
root@vm11:/tmp# fab deploy
[10.1.6.159] Executing task 'deploy'
[10.1.6.159] Executing task 'task1'
I'm 159 /home/guol/
[10.1.6.159] run: ls -l
[10.1.6.159] out: total 0
[10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 159-remote
[10.1.6.159] out: [10.1.6.159] Executing task 'task2'
I'm put 186's 186-local file to 159
[10.1.6.159] put: /home/guol/186-local -> /home/guol/186-local
I'm 159 /home/guol/
[10.1.6.159] run: ls -l
[10.1.6.159] out: total 0
[10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 159-remote
[10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 14:33 186-local
[10.1.6.159] out: Done.
Disconnecting from 10.1.6.159... done.

7  在186上打开一个159的交互式的shell

#!/usr/bin/python
from fabric.api import *
from fabric.colors import *
from fabric.context_managers import *
env.hosts=['10.1.6.159']
env.password='xxxxxx' def task3():
open_shell("ifconfig eth0|grep '10.1.6.159'")
def deploy():
execute(task3) ##执行
root@vm11:/tmp# fab deploy
[10.1.6.159] Executing task 'deploy'
[10.1.6.159] Executing task 'task3'
Welcome to Ubuntu 12.10 (GNU/Linux 3.5.0-17-generic x86_64)
Last login: Fri Dec 21 14:39:39 2012 from 10.1.6.186
ifconfig eth0|grep '10.1.6.159'
root@vm12:~# ifconfig eth0|grep '10.1.6.159'
inet addr:10.1.6.159 Bcast:10.1.6.255 Mask:255.255.255.0

python部署工具fabric的更多相关文章

  1. Python自动化运维工具-Fabric部署及使用总结

    使用shell命令进行复杂的运维时,代码往往变得复杂难懂,而使用python脚本语言来编写运维程序,就相当于开发普通的应用一样,所以维护和扩展都比较简单,更重要的是python运维工具fabric能自 ...

  2. Python自动化运维工具fabric的安装

    使用shell命令进行复杂的运维时,代码往往变得复杂难懂,而使用python脚本语言来编写运维程序,就相当于开发普通的应用一样,所以维护和扩展都比较简单,更重要的是python运维工具fabric能自 ...

  3. 20180903 - Python Pip 工具下载whl包与离线安装

    20180903 - Python Pip 工具下载whl包与离线安装 1. 我的Blog 博客园 https://www.cnblogs.com/piggybaba 个人网站 http://pigg ...

  4. Python模块学习 - fabric

    简介 fabric是一个Python的库,同时它也是一个命令行工具.使用fabric提供的命令行工具,可以很方便地执行应用部署和系统管理等操作. fabric依赖于paramiko进行ssh交互,fa ...

  5. 轻量级自动化运维工具Fabric的安装与实践

    一.背景环境 在运维工作中,经常会遇到重复性的劳动,这个时候为了效率就必须要使用自动化运维工具. 这里我给大家介绍轻量级自动化运维工具Fabric,Fabric是基于Python语言开发的,是开发同事 ...

  6. Openstack部署工具

    Openstack发展很猛,很多朋友都很认同,2013年,会很好的解决OpenStack部署的问题,让安装,配置变得更加简单易用. 很多公司都投入人力去做这个,新浪也计划做一个Openstack的is ...

  7. Python生态工具、文本处理和系统管理(虚拟)

    一.Python生态工具 一.Python内置小工具 1.秒级启动一个下载服务器 Python 内置了一个下载服务器就能够显著提升效率了 . 例如, 你的同事要让你传的文件位于某一个目录下,那么,你可 ...

  8. 阿里云运维部署工具AppDeploy详细教程

    AppDeploy是一个通过SSH实现的命令行工具,可完成应用部署和远程运维管理.当前工具实现为两个版本:普通版(伪代码描述语言)和Python版.Python版使用Python语法规则,可实现您的各 ...

  9. python模块之 fabric

    Python模块之Fabric   Fabric简介 Fabric是一个Python库,可以通过SSH在多个host上批量执行任务.你可以编写任务脚本,然后通过Fabric在本地就可以使用SSH在大量 ...

随机推荐

  1. Redis -- 过期时间 和 缓存 例子

    1.设置 key的生存时间,过期自动删除 exprire key  seconds    设置过期时间 秒数 ttl key   查询剩余时间 如果 设置了过期时间.对key进行 set 操作,会清除 ...

  2. JavaScript自定义事件,动态添加属性

    根据事件的不同,可用的自定义方法也不同. document.createEvent('Event'); 实现主要有4个步骤: 1.创建事件. 2.初始化事件(三个参数:事件名,是否起泡,是否取消默认触 ...

  3. MapReduce框架组成

    原文地址:MapReduce的架构组成 MapReduce基本架构 分而治之,并行计算 一句话 -- 整体主从架构,map加reduce:map.split入磁盘,数据对分partition:shuf ...

  4. hdu 2955(概率转化,01背包)

    Hot~~招聘——巴卡斯(杭州),壹晨仟阳(杭州),英雄互娱(杭州) (包括2016级新生)除了校赛,还有什么途径可以申请加入ACM校队? Robberies Time Limit: 2000/100 ...

  5. PHP需要学习成长路径

    第一阶段:基础阶段(基础PHP程序员) 重点:把LNMP搞熟练(核心是安装配置基本操作) 目标:能够完成基本的LNMP系统安装,简单配置维护;能够做基本的简单系统的PHP开发;能够在PHP中型系统中支 ...

  6. jstree无限级菜单ajax按需动态加载子节点

    业余时间研究了一下jstree,更新非常快已经是3.0了,首先看一下效果截图: 1.页面引入样式和脚本(注意路径根据实际情况) <link href="~/Scripts/vakata ...

  7. hdu 5117 数学公式展开 + dp

    题目大意:有n个灯泡,m个按钮,(1 <= n, m <= 50),每个按钮和ki 个灯泡相关, 按下后,转换这些灯泡的状态,问你所有2^m的按下按钮的 组合中亮着的灯泡的数量的三次方的和 ...

  8. 部署openstack(N)版本-本地yum源(1)

    部署本地openstack yum源,原因主要是我想安装老版本,N版(newton),部署本地yum源,安装速度可以更快. 1. 使用apache提供yum服务 yum install -y http ...

  9. Python与SQLite日期时间函数的使法

    SQLite的时间函数跟Python的时间函数有些许差别,所以稍做记录,供自己以后查询. 网上有将SQLite官方WIKI内容翻译成中文的文章,大家有兴趣可以搜索一下,我这里单纯记录一下个人比较常用的 ...

  10. Centos7搭建SVN Server手记

    安装svn和依赖模块 yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql 配置和使用 mkdir -p /opt/s ...