Python fabric实践操作
前面学习了理论,下面该练练手了。两台机器: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实践操作的更多相关文章
- python fabric远程操作和部署
博客迁往:新地址(点击直达) 新博客使用markdown维护,线下有版本号库,自己写的所以会定时更新同步.同一时候提供更好的导航和阅读体验 csdn对markdown支持不好.所以旧版不会花时间进行同 ...
- python新手 实践操作 作业
#有如下值集合 [11,22,33,44,55,66,77,88,99],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中.即: {'k1': 大于66的所 ...
- Python Fabric远程自动部署简介
Fabric是一个Python(2.5-2.7)库,用于简化使用SSH的应用程序部署或系统管理任务. 它提供的操作包括:执行本地或远程shell命令,上传/下载文件,以及其他辅助功能,如提示用户输入. ...
- Python入门经典. 以解决计算问题为导向的Python编程实践
Python入门经典. 以解决计算问题为导向的Python编程实践(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1juLsew8UiOErRheQPOuTaw 提取 ...
- Python 最佳实践指南 2018 学习笔记
基础信息 版本 Python 2.7 Python 3.x Python2.7 版本在 2020 年后不再提供支持,建议新手使用 3.x 版本进行学习 实现 CPython:Python的标准实现: ...
- 使用python fabric搭建RHEL 7.2大数据基础环境以及部分优化
1.使用python fabric进行Linux基础配置 使用python,可以让任何事情高效起来,包括运维工作,fabric正式这样一套基于python2的类库,它执行本地或远程shell命令提供了 ...
- <读书笔记>001-以解决问题为导向的python编程实践
以解决问题为导向的python编程实践 0.第0章:计算机科学 思考:计算机科学是否为计算机编程的简称? 编程的困难点:1.同时做2件事(编程语言的语法.语义+利用其解决问题) 2.什么是好程序(解 ...
- Python Fabric模块详解
Python Fabric模块详解 什么是Fabric? 简单介绍一下: Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...
- 后端程序员之路 6、Python fabric
直接写shell固然也很好,但是用python来写脚本,也是美滋滋.fabric是一个封装部署.多机操作等功能的python库. Welcome to Fabric! - Fabric documen ...
随机推荐
- Easyui入门视频教程 第11集---Window的使用
目录 Easyui入门视频教程 第11集---Window的使用 Easyui入门视频教程 第10集---Messager的使用 Easyui入门视频教程 第09集---登录完善 图标自定义 ...
- 阿里云ecs配置辅助网卡绑定公网ip地址
EIP直通车 前置条件:1.大家的实例是从经典迁移到VPC里面的,上古时期,经典实例大家购买实例的时候都是买了带宽的.而这种带宽一般情况下都是包年包月的,而且这种绑定在实例上的IP,我们把它叫做公网I ...
- nullpointerxception——处理思路
概念: 1.所谓的指针,就是java中的对象的引用.比如String s;这个s就是指针.2.所谓的空指针,就是指针的内容为空.比如上面的s,如果令它指向null,就是空指针.3.所谓的空指针异常,就 ...
- Python实现鸢尾花数据集分类问题——基于skearn的LogisticRegression
Python实现鸢尾花数据集分类问题——基于skearn的LogisticRegression 一. 逻辑回归 逻辑回归(Logistic Regression)是用于处理因变量为分类变量的回归问题, ...
- 代码管理(一)git
代码管理工具有很多 主要的有两个 git 和 svn svn 衍生出来的软件有 cornerstone smartSVN git 的图形界面工具有 Sourcetree 前几年 s ...
- Easy APNs Provider 消息推送测试工具
1.Easy APNs Provider 简介 Easy APNs Provider 是一款为 iOS.Mac App 提供推送测试的小工具. App Store 下载地址 Easy APNs Pro ...
- log4j(三)——如何控制不同级别的日志信息的输出?
一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 二:老规矩,先来个栗子,然后再聊聊感受 import org.apache.log4j.*; //by godtrue p ...
- Android的API版本和名称对应关系
Android版本名和API Level关系全称 Android的版本 Android版本名称Code name Android的API level Android 1.0 (API level 1) ...
- 源码安装mysql 5.7.19数据库
1.系统要求yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel 2.创建用户和组groupadd mysql & ...
- SQL 给字符串补0
第一种方法: right('00000'+cast(@count as varchar),5) 其中'00000'的个数为right函数的最后参数,例如这里是5,所以有5个0 @count就是被格式化 ...