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

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

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

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

  1. #!/usr/bin/python
  2. from fabric.api import *
  3. from fabric.colors import *
  4. from fabric.context_managers import *
  5.  
  6. env.hosts=['10.1.6.186','10.1.6.159']
  7. env.password='xxxxxx'
  8. env.exclude_hosts=['10.1.6.159']
  9.  
  10. def task1():
  11. with cd('/home/guol'):
  12. run('ls -l')
  13.  
  14. def task2():
  15. print(green("I'm fabric"))
  16.  
  17. def deploy():
  18. execute(task1)
  19. execute(task2)
  20.  
  21. ##执行
  22. root@vm11:/tmp# fab deploy

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

  1. #!/usr/bin/python
  2. from fabric.api import *
  3. from fabric.colors import *
  4. from fabric.context_managers import *
  5.  
  6. env.roledefs={'web1':['10.1.6.186'],'web2':['10.1.6.159']}
  7. env.password='xxxxxx'
  8.  
  9. @roles('web1')
  10. def task1():
  11. with cd('/home/guol'):
  12. run('ls -l')
  13. @roles('web2')
  14. def task2():
  15. print(green("I'm fabric"))
  16.  
  17. def deploy():
  18. execute(task1)
  19. execute(task2)
  20. ##执行
  21. root@vm11:/tmp# fab deploy

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

  1. #!/usr/bin/python
  2. from fabric.api import *
  3. from fabric.colors import *
  4. from fabric.context_managers import *
  5. env.hosts=['10.1.6.159']
  6. env.password='xxxxxx'
  7.  
  8. def task1():
  9. print(green("I'm 186 /home/guol/"))
  10. local('ls -l /home/guol')
  11. def task2():
  12. print(green("I'm get 159's 159-remote file to 186"))
  13. get('/home/guol/159-remote','/home/guol')
  14. print(yellow("I'm 186 /home/guol/"))
  15. local('ls -l /home/guol')
  16.  
  17. def deploy():
  18. execute(task1)
  19. execute(task2)
  20.  
  21. ##执行
  22. root@vm11:/tmp# fab deploy
  23. [10.1.6.159] Executing task 'deploy'
  24. [10.1.6.159] Executing task 'task1'
  25. I'm 186 /home/guol/
  26. [localhost] local: ls -l /home/guol
  27. total 0
  28. -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
  29. [10.1.6.159] Executing task 'task2'
  30. I'm get 159's 159-remote file to 186
  31. [10.1.6.159] download: /home/guol/159-remote <- /home/guol/159-remote
  32. I'm 186 /home/guol/
  33. [localhost] local: ls -l /home/guol
  34. total 0
  35. -rw-r--r-- 1 root root 0 Dec 21 14:28 159-remote
  36. -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
  37.  
  38. Done.
  39. Disconnecting from 10.1.6.159... done.

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

  1. #!/usr/bin/python
  2. from fabric.api import *
  3. from fabric.colors import *
  4. from fabric.context_managers import *
  5. env.hosts=['10.1.6.159']
  6. env.password='xxxxxx'
  7.  
  8. def task1():
  9. print(green("I'm 159 /home/guol/"))
  10. with cd('/home/guol'):
  11. run('ls -l')
  12. def task2():
  13. print(green("I'm put 186's 186-local file to 159"))
  14. put('/home/guol/186-local','/home/guol')
  15. print(yellow("I'm 159 /home/guol/"))
  16. with cd('/home/guol'):
  17. run('ls -l')
  18. def deploy():
  19. execute(task1)
  20. execute(task2)
  21.  
  22. ##执行
  23. root@vm11:/tmp# fab deploy
  24. [10.1.6.159] Executing task 'deploy'
  25. [10.1.6.159] Executing task 'task1'
  26. I'm 159 /home/guol/
  27. [10.1.6.159] run: ls -l
  28. [10.1.6.159] out: total 0
  29. [10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 159-remote
  30. [10.1.6.159] out:
  31.  
  32. [10.1.6.159] Executing task 'task2'
  33. I'm put 186's 186-local file to 159
  34. [10.1.6.159] put: /home/guol/186-local -> /home/guol/186-local
  35. I'm 159 /home/guol/
  36. [10.1.6.159] run: ls -l
  37. [10.1.6.159] out: total 0
  38. [10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 159-remote
  39. [10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 14:33 186-local
  40. [10.1.6.159] out:
  41.  
  42. Done.
  43. Disconnecting from 10.1.6.159... done.

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

  1. #!/usr/bin/python
  2. from fabric.api import *
  3. from fabric.colors import *
  4. from fabric.context_managers import *
  5. env.hosts=['10.1.6.159']
  6. env.password='xxxxxx'
  7. def task3():
  8. open_shell("ifconfig eth0|grep '10.1.6.159'")
  9. def deploy():
  10. execute(task3)
  11. ##执行
  12. root@vm11:/tmp# fab deploy
  13. [10.1.6.159] Executing task 'deploy'
  14. [10.1.6.159] Executing task 'task3'
  15. Welcome to Ubuntu 12.10 (GNU/Linux 3.5.0-17-generic x86_64)
  16. Last login: Fri Dec 21 14:39:39 2012 from 10.1.6.186
  17. ifconfig eth0|grep '10.1.6.159'
  18. root@vm12:~# ifconfig eth0|grep '10.1.6.159'
  19. 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. VMware无法识别USB设备

    VMware虚拟机开始还能识别USB设备/U盘,突然就不行了,在网上找了好久,提供的方法大致如下: 1.   首先Ctrl+R启动运行,输入services.msc,找到一个VMware USB dr ...

  2. python_day7学习笔记

    类 1)创建一个类 #coding=utf-8 __author__ = 'Administrator' class Employee: '所有员工的基类' empCount = 0 def __in ...

  3. Nginx-进程模型

    1.整体框架 正常执行起来的Nginx有很多进程,有master_process和worker_process进程,master_process是监控进程即主线程,worker_process是工作进 ...

  4. poj 3264(RMQ或者线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 42929   Accepted: 20184 ...

  5. 微擎系统BUG漏洞解决方法汇总(原创)

    微擎微赞系统BUG漏洞解决方法汇总 弄了微擎系统来玩玩,发觉这个系统BUG还不少,阿里云的提醒都一大堆,主要是没有针对SQL注入做预防,处理的办法基本都是用转义函数. 汇总: 1. 漏洞名称: 微擎任 ...

  6. GT-----FAQ整理

    1.pss0,pss1,这里的序号0和1是什么意思?      说明选的目标调试 App 有至少 2 个进程,先启动的那个进程的 pss 值会被加后缀 0,后启动那个会被加后 缀 1.所有参数前面的“ ...

  7. tomcat放置静态html页面

    因公司tomcat网站需要部署一个html的静态网页上去 在项目目录下建立了一个 /usr/local/tomcat/apache-tomcat-brain-api/webapps/ROOT/html ...

  8. PHP的命名空间namespace

    对于命名空间,官方文档已经说得很详细[查看],我在这里做了一下实践和总结. 命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只 ...

  9. ARK登录信息

    101,389B,382:仙境353:中心岛380:畸变404:孤岛371:焦土487:灭绝 eaglexmw:389b[65493013] : 初级畸变,高级飞升,TEK全解(有380权限),黑鬼, ...

  10. CodeForces 738A Interview with Oleg

    模拟. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...