前面学习了理论,下面该练练手了。两台机器: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. highstock禁用UTC

    xAxis: { labels: { format: '{value:%Y-%m-%d}', /* * 也可以用 formatter 格式化函数,时间格式化说明如下: * %Y 年 * %m 月 * ...

  2. Jqeury Mobile实战之切屏效果以及屏幕滚动到底端加载更多和点击切换更多

    http://blog.csdn.net/q718330882/article/details/46120691 //页面滚动到底部加载更多事件 $( window ).scroll(function ...

  3. 【WEB2.0】 网页调用QQ聊天(PC+M站)

    很多时候,我们在网站中需要加入联系QQ的功能,我下面就来说下在web页面中调用QQ聊天是如何实现的,直接上代码: <!DOCTYPE HTML> <html> <head ...

  4. numpy文件读写的三对函数

    在Python很多库中,使用文件名的地方都可以使用文件对象来替代. 在下述三种方法中,都是如此. 一.a.tofile()和np.fromfile() numpy中的ndarray对象有一个函数tof ...

  5. Add Microsoft SQL JDBC driver to Maven(转)

    from:http://claude.betancourt.us/add-microsoft-sql-jdbc-driver-to-maven/ Add Microsoft SQL JDBC driv ...

  6. 单个APP页面支持屏幕旋转

    1.info中支持所有的方向 2.APPDelega.h中添加属性 @property (nonatomic,assign) BOOL allowRotate; APPdelegate.m中实现方法 ...

  7. 如何搜索IP的地理位置

    如何搜索IP的地理位置 http://www.ip138.com/ 打开上边这个网页以后,会显示自身的IP及地理位置,,,,也可以搜索别人的IP和地理位置,手机号等:截图如下:

  8. XmlSerializer 对象序列化成XML 自定义编码格式(gb2312)

    随着面向服务(SOA)的开发方式的兴起,客户端和服务端之间的消息传送,很多采用了XML的格式.但是大家在日常的开发中,应该会有这么种体验,就是组织xml格式的代码太繁琐,这篇随笔也是为了和大家分享下简 ...

  9. 跟我学SharePoint 2013视频培训课程——理解SharePoint网站的体系结构(3)

    课程简介 第三天,理解SharePoint 2013 网站的体系结构 视频 SharePoint 2013 交流群 41032413

  10. Java Nashorn--Part 6

    Nashorn 的 JavaScript 语言的扩展 正如我们所讨论的,Nashorn 是一个完全符合 ECMAScript 5.1 的实现.然而除此之外,Nashorn 还实现了很多 JavaScr ...