Fabric 学习笔记

转载源:http://my.oschina.net/guol/blog/97607  

前面学习了理论,下面该练练手了。两台机器:10.1.6.186、10.1.6.159。fabric部署在10.1.6.186上面

1  执行一个简单的task任务,显示两台机器的/home/guol/目录下的文件

01 #!/usr/bin/python
02 from fabric.api import *
03 from fabric.context_managers import *
04  
05 env.hosts=['10.1.6.186','10.1.6.159']
06 env.password='xxxxxx'
07  
08 def task1():
09     with cd('/home/guol'):
10         run('ls -l')
11  
12 ##结果
13 root@vm11:/tmp# fab task1
14 [10.1.6.186] Executing task 'task1'
15 [10.1.6.186] run: ls -l
16 [10.1.6.186] out: total 0
17 [10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
18 [10.1.6.186] out:
19  
20 [10.1.6.159] Executing task 'task1'
21 [10.1.6.159] run: ls -l
22 [10.1.6.159] out: total 0
23 [10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 159-remote
24 [10.1.6.159] out:
25  
26  
27 Done.
28 Disconnecting from 10.1.6.159... done.
29 Disconnecting from 10.1.6.186... done.
2  执行和1相同的任务,不过排除掉10.1.6.159这台机器

01 #!/usr/bin/python
02 from fabric.api import *
03 from fabric.context_managers import *
04  
05 env.hosts=['10.1.6.186','10.1.6.159']
06 env.password='xxxxxx'
07 env.exclude_hosts=['10.1.6.159']
08  
09 def task1():
10     with cd('/home/guol'):
11         run('ls -l')
12  
13 ##执行
14 root@vm11:/tmp# fab task1
15 [10.1.6.186] Executing task 'task1'
16 [10.1.6.186] run: ls -l
17 [10.1.6.186] out: total 0
18 [10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
19 [10.1.6.186] out:
20  
21  
22 Done.
23 Disconnecting from 10.1.6.186... done.
3  执行和2相同任务,再增加一个task2,并且把taskN伪装成meta任务执行

01 #!/usr/bin/python
02 from fabric.api import *
03 from fabric.colors import *
04 from fabric.context_managers import *
05  
06 env.hosts=['10.1.6.186','10.1.6.159']
07 env.password='xxxxxx'
08 env.exclude_hosts=['10.1.6.159']
09  
10  
11 def task1():
12     with cd('/home/guol'):
13         run('ls -l')
14  
15 def task2():
16     print(green("I'm fabric"))
17  
18 def deploy():
19     execute(task1)
20     execute(task2)
21  
22 ##执行
23 root@vm11:/tmp# fab deploy
24 [10.1.6.186] Executing task 'deploy'
25 [10.1.6.186] Executing task 'task1'
26 [10.1.6.186] run: ls -l
27 [10.1.6.186] out: total 0
28 [10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
29 [10.1.6.186] out:
30  
31 [10.1.6.186] Executing task 'task2'
32 I'm fabric
33  
34 Done.
35 Disconnecting from 10.1.6.186... done.
4  不同的机器执行不同的task

01 #!/usr/bin/python
02 from fabric.api import *
03 from fabric.colors import *
04 from fabric.context_managers import *
05  
06 env.roledefs={'web1':['10.1.6.186'],'web2':['10.1.6.159']}
07 env.password='xxxxxx'
08  
09 @roles('web1')
10 def task1():
11     with cd('/home/guol'):
12         run('ls -l')
13 @roles('web2')
14 def task2():
15     print(green("I'm fabric"))
16  
17 def deploy():
18     execute(task1)
19     execute(task2)
20 ##执行
21 root@vm11:/tmp# fab deploy
22 [10.1.6.186] Executing task 'task1'
23 [10.1.6.186] run: ls -l
24 [10.1.6.186] out: total 0
25 [10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
26 [10.1.6.186] out:
27  
28 [10.1.6.159] Executing task 'task2'
29 I'm fabric
30  
31 Done.
32 Disconnecting from 10.1.6.186... done.
5  把159的/home/guol/159-remote拉取到186的  /home/guol/目录下

01 #!/usr/bin/python
02 from fabric.api import *
03 from fabric.colors import *
04 from fabric.context_managers import *
05 env.hosts=['10.1.6.159']
06 env.password='xxxxxx'
07  
08 def task1():
09     print(green("I'm 186 /home/guol/"))
10     local('ls -l /home/guol')
11 def task2():
12     print(green("I'm get 159's 159-remote file to 186"))
13     get('/home/guol/159-remote','/home/guol')
14     print(yellow("I'm 186 /home/guol/"))
15     local('ls -l /home/guol')
16  
17 def deploy():
18     execute(task1)
19     execute(task2)
20  
21 ##执行
22 root@vm11:/tmp# fab deploy
23 [10.1.6.159] Executing task 'deploy'
24 [10.1.6.159] Executing task 'task1'
25 I'm 186 /home/guol/
26 [localhost] local: ls -/home/guol
27 total 0
28 -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
29 [10.1.6.159] Executing task 'task2'
30 I'm get 159'159-remote file to 186
31 [10.1.6.159] download: /home/guol/159-remote <- /home/guol/159-remote
32 I'm 186 /home/guol/
33 [localhost] local: ls -/home/guol
34 total 0
35 -rw-r--r-- 1 root root 0 Dec 21 14:28 159-remote
36 -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
37  
38 Done.
39 Disconnecting from 10.1.6.159... done.
6    把186的/home/guol/   186-local推送到159的   /home/guol/目录下

01 #!/usr/bin/python
02 from fabric.api import *
03 from fabric.colors import *
04 from fabric.context_managers import *
05 env.hosts=['10.1.6.159']
06 env.password='xxxxxx'
07  
08 def task1():
09     print(green("I'm 159 /home/guol/"))
10     with cd('/home/guol'):
11         run('ls -l')
12 def task2():
13     print(green("I'm put 186's 186-local file to 159"))
14     put('/home/guol/186-local','/home/guol')
15     print(yellow("I'm 159 /home/guol/"))
16     with cd('/home/guol'):
17         run('ls -l')
18 def deploy():
19     execute(task1)
20     execute(task2)
21  
22 ##执行
23 root@vm11:/tmp# fab deploy
24 [10.1.6.159] Executing task 'deploy'
25 [10.1.6.159] Executing task 'task1'
26 I'm 159 /home/guol/
27 [10.1.6.159] run: ls -l
28 [10.1.6.159] out: total 0
29 [10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 159-remote
30 [10.1.6.159] out:
31  
32 [10.1.6.159] Executing task 'task2'
33 I'm put 186'186-local file to 159
34 [10.1.6.159] put: /home/guol/186-local -/home/guol/186-local
35 I'm 159 /home/guol/
36 [10.1.6.159] run: ls -l
37 [10.1.6.159] out: total 0
38 [10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 159-remote
39 [10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 14:33 186-local
40 [10.1.6.159] out:
41  
42  
43 Done.
44 Disconnecting from 10.1.6.159... done.
7  在186上打开一个159的交互式的shell

01 #!/usr/bin/python
02 from fabric.api import *
03 from fabric.colors import *
04 from fabric.context_managers import *
05 env.hosts=['10.1.6.159']
06 env.password='xxxxxx'
07  
08 def task3():
09     open_shell("ifconfig eth0|grep '10.1.6.159'")
10 def deploy():
11      execute(task3)
12  
13 ##执行
14 root@vm11:/tmp# fab deploy
15 [10.1.6.159] Executing task 'deploy'
16 [10.1.6.159] Executing task 'task3'
17 Welcome to Ubuntu 12.10 (GNU/Linux 3.5.0-17-generic x86_64)
18 Last login: Fri Dec 21 14:39:39 2012 from 10.1.6.186
19 ifconfig eth0|grep '10.1.6.159'
20 root@vm12:~# ifconfig eth0|grep '10.1.6.159'
21           inet addr:10.1.6.159  Bcast:10.1.6.255  Mask:255.255.255.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值