Fabric 学习笔记

来源:互联网 发布:java ffmpeg 视频转码 编辑:程序博客网 时间:2024/05/23 20:46

转载源: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
02from fabric.api import *
03from fabric.context_managers import *
04 
05env.hosts=['10.1.6.186','10.1.6.159']
06env.password='xxxxxx'
07 
08def task1():
09    with cd('/home/guol'):
10        run('ls -l')
11 
12##结果
13root@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 
27Done.
28Disconnecting from 10.1.6.159... done.
29Disconnecting from 10.1.6.186... done.
2  执行和1相同的任务,不过排除掉10.1.6.159这台机器

01#!/usr/bin/python
02from fabric.api import *
03from fabric.context_managers import *
04 
05env.hosts=['10.1.6.186','10.1.6.159']
06env.password='xxxxxx'
07env.exclude_hosts=['10.1.6.159']
08 
09def task1():
10    with cd('/home/guol'):
11        run('ls -l')
12 
13##执行
14root@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 
22Done.
23Disconnecting from 10.1.6.186... done.
3  执行和2相同任务,再增加一个task2,并且把taskN伪装成meta任务执行

01#!/usr/bin/python
02from fabric.api import *
03from fabric.colors import *
04from fabric.context_managers import *
05 
06env.hosts=['10.1.6.186','10.1.6.159']
07env.password='xxxxxx'
08env.exclude_hosts=['10.1.6.159']
09 
10 
11def task1():
12    with cd('/home/guol'):
13        run('ls -l')
14 
15def task2():
16    print(green("I'm fabric"))
17 
18def deploy():
19    execute(task1)
20    execute(task2)
21 
22##执行
23root@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'
32I'm fabric
33 
34Done.
35Disconnecting from 10.1.6.186... done.
4  不同的机器执行不同的task

01#!/usr/bin/python
02from fabric.api import *
03from fabric.colors import *
04from fabric.context_managers import *
05 
06env.roledefs={'web1':['10.1.6.186'],'web2':['10.1.6.159']}
07env.password='xxxxxx'
08 
09@roles('web1')
10def task1():
11    with cd('/home/guol'):
12        run('ls -l')
13@roles('web2')
14def task2():
15    print(green("I'm fabric"))
16 
17def deploy():
18    execute(task1)
19    execute(task2)
20##执行
21root@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'
29I'm fabric
30 
31Done.
32Disconnecting from 10.1.6.186... done.
5  把159的/home/guol/159-remote拉取到186的 /home/guol/目录下

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

01#!/usr/bin/python
02from fabric.api import *
03from fabric.colors import *
04from fabric.context_managers import *
05env.hosts=['10.1.6.159']
06env.password='xxxxxx'
07 
08def task1():
09    print(green("I'm 159 /home/guol/"))
10    with cd('/home/guol'):
11        run('ls -l')
12def 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')
18def deploy():
19    execute(task1)
20    execute(task2)
21 
22##执行
23root@vm11:/tmp# fab deploy
24[10.1.6.159] Executing task 'deploy'
25[10.1.6.159] Executing task 'task1'
26I'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'
33I'm put 186'186-local file to 159
34[10.1.6.159] put: /home/guol/186-local -/home/guol/186-local
35I'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 
43Done.
44Disconnecting from 10.1.6.159... done.
7  在186上打开一个159的交互式的shell

view source
print?
01#!/usr/bin/python
02from fabric.api import *
03from fabric.colors import *
04from fabric.context_managers import *
05env.hosts=['10.1.6.159']
06env.password='xxxxxx'
07 
08def task3():
09    open_shell("ifconfig eth0|grep '10.1.6.159'")
10def deploy():
11     execute(task3)
12 
13##执行
14root@vm11:/tmp# fab deploy
15[10.1.6.159] Executing task 'deploy'
16[10.1.6.159] Executing task 'task3'
17Welcome to Ubuntu 12.10 (GNU/Linux 3.5.0-17-generic x86_64)
18Last login: Fri Dec 21 14:39:39 2012 from 10.1.6.186
19ifconfig eth0|grep '10.1.6.159'
20root@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