前面学习了理论,下面该练练手了。两台机器: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实践操作

    Python fabric实践操作 http://my.oschina.net/guol/blog/97607

  2. python实训总结报告书_20172304 实验四python综合实践报告

    20172304 实验四python综合实践报告 姓名:段志轩 学号:20172304 指导教师:王志强 课程:Python程序设计 实验时间:2020年5月13日至2020年6月14日 实验分析 本 ...

  3. python程序设计实践教程答案-Python程序设计实践教程

    书名:Python程序设计实践教程 定价:29.8 ISBN:9787115532602 作者:储岳中 薛希玲 版次:*1版 出版时间:2020-04 内容提要: 本书是Python语言程序设计的配套 ...

  4. Python自动化实践

    *** Python自动化实践 *** 1.为什么要写代码实现接口自动化 大家知道很多接口测试工具可以实现对接口的测试,如postman.jmeter.fiddler等等,而且使用方便,那么为什么还要 ...

  5. python的文件操作、模块操作、os模块、time、datatime模块以及模块的制作

    Day12新手小白学python 第十二节 python的文件操作.模块操作.os模块.time.datatime模块以及模块的制作 目录 Day12新手小白学python 前言 一.文件打开关闭 二 ...

  6. Python爬虫实践-网易云音乐

    1.前言 最近,网易的音乐很多听不到了,刚好也看到很多教程,跟进学习了一下,也集大全了吧,本来想优化一下的,但是发现问题还是有点复杂,最后另辟捷径,提供了简单的方法啊! 本文主要参考 python编写 ...

  7. 《Python程序设计》实验四 Python综合实践实验报告

    <Python程序设计>实验四 Python综合实践实验报告 1.实验内容 Python综合应用:爬虫.数据处理.可视化.机器学习.神经网络.游戏.网络安全等. 在华为ECS服务器(Ope ...

  8. pyhton如何导入包的每一个文件_如何开始第一个 Python 编程实践项目?

    首发公众号:交通攻城狮 微信ID:TrafficBlog 2020,第 15 期 导语:上期我们谈了谈如何高效的入门 Python 编程,了解了 Python 的编程环境以及常用的包,如 Pandas ...

  9. python怎么开始编程_如何开始第一个 Python 编程实践项目?

    导语:上期我们谈了谈如何高效的入门 Python 编程,了解了 Python 的编程环境以及常用的包,如 Pandas.Matplotlib.Numpy 等.这次我们将以实践项目的形式,帮助大家快速的 ...

最新文章

  1. 一步一步写算法(之hash表)
  2. SecureCRT图形界面(通过设置调用Xmanager - Passive程序)
  3. 页目录项和页表项——《x86汇编语言:从实模式到保护模式》读书笔记43
  4. 导出Excel表格时,如何把数据库表中的编号转换成配置文件中的汉字
  5. 作者:吴城文,男,清华大学计算机科学与技术系硕士生。
  6. Java环境配置及第一个HelloWord(Win)
  7. C++ Sets(集合)
  8. hdu - 5033 - Building(单调栈)
  9. 一个可以免费下载数据集的网站
  10. mitmproxy工具使用小红书数据抓取
  11. 吉比特H2-3光猫破解超级密码
  12. PHP WebShell 免杀
  13. 使用命令查看linux编码,如何利用命令查看linux 系统汉字编码
  14. 基于Python实现的手写数字识别系统
  15. 页面增加问号图标,鼠标放置会提示文字信息。(记录日志)
  16. WARN: Establishing SSL connection without server‘s identity verification is not recommended
  17. 简一论币:8.14 BTC回撤介入多单谨防延续回撤,11600短多尝试
  18. SAP如何控制采购价格不显示(适用ME23N、ME2L、ME2M、ME2K、ME2J、ME2C、ME2B、ME2N等)
  19. RPG游戏-道具系统
  20. 系统集成项目管理工程师备考资料(口袋应试第二版)7

热门文章

  1. Execl导入问题之文本转换
  2. 卡耐基大学计算机专业分类,卡内基梅隆大学计算机专业
  3. RocketMQ实现原理
  4. Shell下的环境变量
  5. 查看网卡[网络接口]
  6. zookeeper的设计猜想-Leader角色
  7. ConcurrentHashMap的源码分析-扩容过程图解
  8. IDEA 集成Lombok 插件-安装插件
  9. 消息发送到消息接收的整体流程
  10. 新版本springboot-springboot与springcloud理解误区