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 ...
随机推荐
- (原+转)Ubuntu16.04软件中心闪退及wifi消失
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6321889.html 参考网址: http://blog.csdn.net/felcon/articl ...
- c++ 11 override final
C++ 11添加了两个继承控制关键字:override和final. override确保在派生类中声明的重载函数跟基类的虚函数有相同的签名.final阻止类的进一步派生和虚函数的进一步重载
- Cordova笔记(一)
跨平台的PhoneGap被Adobe收购,改名为Cordova,现在是Apache下的一款开源软件.网上能找到的关于PhoneGap的教程有些方法已不适用,在学习使用最新版的Cordova时有些问题需 ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- Redis学习之路(003)- hiredis安装及测试
一. hiredis下载地址及C API github下载:https://github.com/redis/hiredis 安装脚本: #!/bin/zsh git clone https://g ...
- [MSP430] 对MSP430单片机__delay_cycles精确延时的说明及改正
在这里, 我来讨论一下关于MSP430单片机使用__delay_cycles延时的问题. IAR for MSP430编译器提供了一个编译器内联的精确延时函数(并非真正的 函数)以提供用户精确延时使用 ...
- Linux导出/导入逻辑卷组信息
源主机上操作: 将文件系统umount # umount /u01 再将LV和VG inactive: # lvchange -an /dev/vg_u01/lv_u01 # vgchange -an ...
- 小而美的ghost driver
做过selenium自动化项目的同学应该都遇到过这样的问题:测试用例太多,运行速度过慢导致团队成员怨声载道. 于是便有了selenium grid和多线程运行selenium测试用例的方法.这些方法各 ...
- 手动脱WinUpack 壳实战
作者:Fly2015 吾爱破解培训第一课选修作业第6个练习演示样例程序.不得不反复那句话,没见过这样的壳,该壳是压缩壳的一种,相对于压缩壳,加密壳的难度要大一些.特别是IAT表的修复问题上. 首先分别 ...
- Android_Bitmap_图片的二次采样并生成缩略图
1.Bitmap概述 Android系统支持几种图片(.png (preferred), .jpg (acceptable), .gif (discouraged)), 其中Bitmap位图#ffff ...