转载源:http://my.oschina.net/guol/blog/97607
前面学习了理论,下面该练练手了。两台机器:10.1.6.186、10.1.6.159。fabric部署在10.1.6.186上面。
1 执行一个简单的task任务,显示两台机器的/home/guol/目录下的文件
02 | from fabric.api import * |
03 | from fabric.context_managers import * |
05 | env.hosts = [ '10.1.6.186' , '10.1.6.159' ] |
09 | with cd( '/home/guol' ): |
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 |
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 |
28 | Disconnecting from 10.1 . 6.159 ... done. |
29 | Disconnecting from 10.1 . 6.186 ... done. |
2 执行和1相同的任务,不过排除掉10.1.6.159这台机器
02 | from fabric.api import * |
03 | from fabric.context_managers import * |
05 | env.hosts = [ '10.1.6.186' , '10.1.6.159' ] |
07 | env.exclude_hosts = [ '10.1.6.159' ] |
10 | with cd( '/home/guol' ): |
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 |
23 | Disconnecting from 10.1 . 6.186 ... done. |
3 执行和2相同任务,再增加一个task2,并且把taskN伪装成meta任务执行
02 | from fabric.api import * |
03 | from fabric.colors import * |
04 | from fabric.context_managers import * |
06 | env.hosts = [ '10.1.6.186' , '10.1.6.159' ] |
08 | env.exclude_hosts = [ '10.1.6.159' ] |
12 | with cd( '/home/guol' ): |
16 | print (green( "I'm fabric" )) |
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 |
31 | [ 10.1 . 6.186 ] Executing task 'task2' |
35 | Disconnecting from 10.1 . 6.186 ... done. |
4 不同的机器执行不同的task
02 | from fabric.api import * |
03 | from fabric.colors import * |
04 | from fabric.context_managers import * |
06 | env.roledefs = { 'web1' :[ '10.1.6.186' ], 'web2' :[ '10.1.6.159' ]} |
11 | with cd( '/home/guol' ): |
15 | print (green( "I'm fabric" )) |
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 |
28 | [ 10.1 . 6.159 ] Executing task 'task2' |
32 | Disconnecting from 10.1 . 6.186 ... done. |
5 把159的/home/guol/159-remote拉取到186的
/home/guol/目录下
02 | from fabric.api import * |
03 | from fabric.colors import * |
04 | from fabric.context_managers import * |
05 | env.hosts = [ '10.1.6.159' ] |
09 | print (green( "I'm 186 /home/guol/" )) |
10 | local( 'ls -l /home/guol' ) |
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' ) |
23 | [ 10.1 . 6.159 ] Executing task 'deploy' |
24 | [ 10.1 . 6.159 ] Executing task 'task1' |
26 | [localhost] local: ls - l / home / guol |
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' s 159 - remote file to 186 |
31 | [ 10.1 . 6.159 ] download: / home / guol / 159 - remote < - / home / guol / 159 - remote |
33 | [localhost] local: ls - l / home / guol |
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 |
39 | Disconnecting from 10.1 . 6.159 ... done. |
6
把186的/home/guol/
186-local推送到159的
/home/guol/目录下
02 | from fabric.api import * |
03 | from fabric.colors import * |
04 | from fabric.context_managers import * |
05 | env.hosts = [ '10.1.6.159' ] |
09 | print (green( "I'm 159 /home/guol/" )) |
10 | with cd( '/home/guol' ): |
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' ): |
24 | [ 10.1 . 6.159 ] Executing task 'deploy' |
25 | [ 10.1 . 6.159 ] Executing task 'task1' |
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 |
32 | [ 10.1 . 6.159 ] Executing task 'task2' |
33 | I 'm put 186' s 186 - local file to 159 |
34 | [ 10.1 . 6.159 ] put: / home / guol / 186 - local - > / home / guol / 186 - local |
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 |
44 | Disconnecting from 10.1 . 6.159 ... done. |
7 在186上打开一个159的交互式的shell
02 | from fabric.api import * |
03 | from fabric.colors import * |
04 | from fabric.context_managers import * |
05 | env.hosts = [ '10.1.6.159' ] |
09 | open_shell( "ifconfig eth0|grep '10.1.6.159'" ) |
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' |
21 | inet addr: 10.1 . 6.159 Bcast: 10.1 . 6.255 Mask: 255.255 . 255.0 |