前面学习了理论,下面该练练手了。两台机器:10.1.6.186、10.1.6.159。fabric部署在10.1.6.186上面。

1  执行一个简单的task任务,显示两台机器的/home/guol/目录下的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python
from fabric.api import *
from fabric.context_managers import *
 
env.hosts=['10.1.6.186','10.1.6.159']
env.password='xxxxxx'
 
def task1():
    with cd('/home/guol'):
        run('ls -l')
 
##结果
root@vm11:/tmp# fab task1
[10.1.6.186] Executing task 'task1'
[10.1.6.186] run: ls -l
[10.1.6.186] out: total 0
[10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
[10.1.6.186] out:
 
[10.1.6.159] Executing task 'task1'
[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:
 
 
Done.
Disconnecting from 10.1.6.159... done.
Disconnecting from 10.1.6.186... done.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python
from fabric.api 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')
 
##执行
root@vm11:/tmp# fab task1
[10.1.6.186] Executing task 'task1'
[10.1.6.186] run: ls -l
[10.1.6.186] out: total 0
[10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
[10.1.6.186] out:
 
 
Done.
Disconnecting from 10.1.6.186... done.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/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
[10.1.6.186] Executing task 'deploy'
[10.1.6.186] Executing task 'task1'
[10.1.6.186] run: ls -l
[10.1.6.186] out: total 0
[10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
[10.1.6.186] out:
 
[10.1.6.186] Executing task 'task2'
I'm fabric
 
Done.
Disconnecting from 10.1.6.186... done.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/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
[10.1.6.186] Executing task 'task1'
[10.1.6.186] run: ls -l
[10.1.6.186] out: total 0
[10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
[10.1.6.186] out:
 
[10.1.6.159] Executing task 'task2'
I'm fabric
 
Done.
Disconnecting from 10.1.6.186... done.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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远程操作和部署

    博客迁往:新地址(点击直达) 新博客使用markdown维护,线下有版本号库,自己写的所以会定时更新同步.同一时候提供更好的导航和阅读体验 csdn对markdown支持不好.所以旧版不会花时间进行同 ...

  2. python新手 实践操作 作业

    #有如下值集合 [11,22,33,44,55,66,77,88,99],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中.即: {'k1': 大于66的所 ...

  3. Python Fabric远程自动部署简介

    Fabric是一个Python(2.5-2.7)库,用于简化使用SSH的应用程序部署或系统管理任务. 它提供的操作包括:执行本地或远程shell命令,上传/下载文件,以及其他辅助功能,如提示用户输入. ...

  4. Python入门经典. 以解决计算问题为导向的Python编程实践

    Python入门经典. 以解决计算问题为导向的Python编程实践(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1juLsew8UiOErRheQPOuTaw 提取 ...

  5. Python 最佳实践指南 2018 学习笔记

    基础信息 版本 Python 2.7 Python 3.x Python2.7 版本在 2020 年后不再提供支持,建议新手使用 3.x 版本进行学习 实现 CPython:Python的标准实现: ...

  6. 使用python fabric搭建RHEL 7.2大数据基础环境以及部分优化

    1.使用python fabric进行Linux基础配置 使用python,可以让任何事情高效起来,包括运维工作,fabric正式这样一套基于python2的类库,它执行本地或远程shell命令提供了 ...

  7. <读书笔记>001-以解决问题为导向的python编程实践

    以解决问题为导向的python编程实践 0.第0章:计算机科学 思考:计算机科学是否为计算机编程的简称? 编程的困难点:1.同时做2件事(编程语言的语法.语义+利用其解决问题)  2.什么是好程序(解 ...

  8. Python Fabric模块详解

    Python Fabric模块详解 什么是Fabric? 简单介绍一下: ​ Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...

  9. 后端程序员之路 6、Python fabric

    直接写shell固然也很好,但是用python来写脚本,也是美滋滋.fabric是一个封装部署.多机操作等功能的python库. Welcome to Fabric! - Fabric documen ...

随机推荐

  1. Qt中的QTableView 中的列放入Widget

    QTableView是Qt中Model View理念的框架,View只展现数据,所以通过互交修改编辑数据,需要用到委托这个概念Delegate. 所以基本思路是继承QItemDelegate这个类,然 ...

  2. 最常用的Java库一览(13年的文章)

    来源于:http://www.importnew.com/7530.html 本文由 ImportNew - 邢 敏 翻译自 programcreek.欢迎加入翻译小组.转载请见文末要求. 写在前面: ...

  3. 邮件相关协议及JavaMail 包简介

    1. 邮件服务器 按功能划分,邮件服务器可以划分为两种类型: SMTP邮件服务器:用于替用户发送邮件和接收外面发送给本地用户的邮件,相当于现实生活中邮局的邮件接收部门(可接收普通用户要投出的邮件和其他 ...

  4. 【转】IOCP创建

    转自:http://www.cmnsoft.com/wordpress/?p=248 感谢原作者.我在此整理一下: 完成端口(IOCP)是WINDOWS平台上特有的一种技术.要使用IOCP技术,就要用 ...

  5. html5界面手机播放mp3

    1把这段代码复制到htm5界面. <audio id="audio" src="2.mp3" style="opacity:0" pr ...

  6. ceph-RGW Jewel版新概念

    一.概述 zone: 包含多个RGW实例的一个逻辑概念.zone不能跨集群,同一个zone的数据保存在同一组pool中: zonegroup:一个zonegroup如果包含一个或多个zone,如果包含 ...

  7. C#中缓存的简单方法及使用Sql设置缓存依赖项

    概述 使用Cache高速缓存可以提高数据的读取速度,减少服务器与客户端之间的数据交互.因为Cache一经创建就会占用服务器上的资源,所以Cache并不是越多越好,一般用于数据较固定,使用较频繁的地方. ...

  8. 【exe4j】如何利用exe4j把java桌面程序生成exe文件

    前言: 我们都知道Java可以将二进制程序打包成可执行jar文件,双击这个jar和双击exe效果是一样一样的,但感觉还是不同.其实将java程序打包成exe也需要这个可执行jar文件. 准备: ecl ...

  9. 虚拟化—Docker解决方案

    What is Docker? Docker is an open-source project to easily create lightweight, portable, self-suffic ...

  10. Linux VFS数据结构

    先说明一下,linux内核中各种数据结构也不停的在变,所以不同版本的内核各个数据结构的定义可能会差别很大,这一组关于linux 文件系统的文章中的代码都摘自linux-2.6.34.1. VFS依赖于 ...