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/pythonfrom 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/pythonfrom 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/pythonfrom 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 fabricDone.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/pythonfrom 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 fabricDone.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/pythonfrom 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/guoltotal 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-remoteI'm 186 /home/guol/[localhost] local: ls -l /home/guoltotal 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-localDone.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/pythonfrom 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-localI'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/pythonfrom 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.186ifconfig 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 ...
随机推荐
- 【laravel5.*】详解laravel中的依赖注入
1.下面这个是自定义的类,钉钉扫码登录web 网页授权OAuth2.0,是一个典型的依赖注入参考示例:
- winform视频教程-任务定时循环执行小工具
有时候,我们可能遇到这样的一种需求,就是需要有一个小工具,在服务器中不断的执行一些任务,那么这样的需求,我们可以做一个windows服务程序,或者做一个winform wpf等小程序,然后小程序里面开 ...
- (原)torch中threads的addjob函数使用方法
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6549452.html 参考网址: https://github.com/torch/threads#e ...
- [SQL Server]从 varchar 数据类型到 datetime 数据类型的转换产生一个超出范围的值。
见下图sql, 使用dateadd()转换时报如题错误, 原因是数据库表中存入的数据格式不正确, 数据格式不正确, 数据格式不正确, 重要的事情讲3遍!! ca.batch_no的前8位必须是日 ...
- 整理打印PI值
准备锻炼背诵PI的小数,找到PI值: PI=3. 141592653589793238462643383279502884197169399375105820974944592307816406286 ...
- property相关参数介绍
1.copy 使用类型 :NSString,block 2.assign使用类型 :delegate,int,float,NSInteger,bool,枚举,结构体... 3.retain使用类型 : ...
- MySQL 分区表原理及数据备份转移实战
MySQL 分区表原理及数据备份转移实战 1.分区表含义 分区表定义指根据可以设置为任意大小的规则,跨文件系统分配单个表的多个部分.实际上,表的不同部分在不同的位置被存储为单独的表.用户所选择的.实现 ...
- 关于新加坡IT薪酬和找工作网站
关于新加坡IT薪酬 很多朋友发邮件或留言问我关于新加坡IT薪酬的问题,由于前段时间比较忙,所以没有及时一一回复,在此表示抱歉. 新加坡IT薪酬范围大概如下(月薪,新加坡币对人民币为1:5): Juni ...
- 树莓派UFW防火墙简单设置
ufw是一个主机端的iptables类防火墙配置工具,比较容易上手.如果你有一台暴露在外网的树莓派,则可通过这个简单的配置提升安全性. 安装方法 sudo apt-get install ufw 当然 ...
- js 对文件操作
下面是对此知识的系统介绍(转自互联网): Javascript是网页制作中离不开的脚本语言,依靠它,一个网页的内容才生动活泼.富有朝气.但也许你还没有发现并应用它的一些更高级的功能吧?比如,对文件和文 ...