动态生成主机列表
通过参考 Fabric 的官方文档的 Using execute with dynamically-set host lists,其中有这么一段示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
from
fabric
.
api
import
run
,
execute
,
task
# For example, code talking to an HTTP API, or a database, or ...
from
mylib
import
external_datastore
# This is the actual algorithm involved. It does not care about host
# lists at all.
def
do_work
(
)
:
run
(
"something interesting on a host"
)
# This is the user-facing task invoked on the command line.
@
task
def
deploy
(
lookup_param
)
:
# This is the magic you don't get with @hosts or @roles.
# Even lazy-loading roles require you to declare available roles
# beforehand. Here, the sky is the limit.
host_list
=
external_datastore
.
query
(
lookup_param
)
# Put this dynamically generated host list together with the work to be
# done.
execute
(
do_work
,
hosts
=
host_list
)
|
然后执行命令:
1
2
|
$
fab
deploy
:
app
$
fab
deploy
:
db
|
其生成主机列表的格式如下:
1
|
[
'10.2.5.1'
,
'10.2.5.2'
,
'10.2.5.3'
,
'10.2.5.4'
,
'10.2.5.5'
,
'10.2.5.6'
,
'10.2.5.7'
,
'10.2.5.8'
,
'10.2.5.9'
,
'10.2.5.10'
]
|
现在我们就可以根据 CMDB 接口来动态生成主机列表了。具体见代码吧
方法一:
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/env python
#encoding=utf-8
import
sys
import
os
import
requests
from
fabric
.
api
import
env
from
fabric
.
api
import
run
from
fabric
.
api
import
put
from
fabric
.
api
import
execute
from
fabric
.
api
import
roles
from
fabric
.
api
import
parallel
from
fabric
.
api
import
cd
from
fabric
.
api
import
task
env
.
user
=
'test'
env
.
password
=
'test'
cmdburl
=
'http://cmdb.test.com/test/listServer.do'
def
find_ips_by_domain
(
domain_name
)
:
ips
=
[
]
payload
=
{
'domain'
:
domain_name
}
res
=
requests
.
get
(
cmdburl
,
params
=
payload
)
hosts
=
res
.
json
(
)
[
'object'
]
[
0
]
[
'servers'
]
for
host
in
hosts
:
host_ip
=
host
[
'ip'
]
ips
.
append
(
host_ip
)
return
ips
def
do_work
(
)
:
run
(
'echo "Running stress test..."'
)
@
task
def
set_hosts
(
domain
)
:
# Update env.hosts instead of calling execute()
host_list
=
find_ips_by_domain
(
domain
)
execute
(
do_work
,
hosts
=
host_list
)
|
1
2
3
|
# 调用
fab
set_hosts
:
app
fab
set_hosts
:
db
|
方法二:
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
|
#!/usr/bin/env python
#encoding=utf-8
import
sys
import
os
import
requests
from
fabric
.
api
import
env
from
fabric
.
api
import
run
from
fabric
.
api
import
put
from
fabric
.
api
import
execute
from
fabric
.
api
import
roles
from
fabric
.
api
import
parallel
from
fabric
.
api
import
cd
from
fabric
.
api
import
task
env
.
user
=
'test'
env
.
password
=
'test'
cmdburl
=
'http://cmdb.test.com/test/listServer.do'
def
find_ips_by_domain
(
domain_name
)
:
ips
=
[
]
payload
=
{
'domain'
:
domain_name
}
res
=
requests
.
get
(
cmdburl
,
params
=
payload
)
hosts
=
res
.
json
(
)
[
'object'
]
[
0
]
[
'servers'
]
for
host
in
hosts
:
host_ip
=
host
[
'ip'
]
ips
.
append
(
host_ip
)
return
ips
@
task
def
do_work
(
)
:
run
(
'echo "Running stress test..."'
)
@
task
def
set_hosts
(
domain
)
:
# Update env.hosts instead of calling execute()
env
.
hosts
=
find_ips_by_domain
(
domain
)
|
1
2
|
#调用
fab
set_hosts
:
test
.
com
do_work
|
上面两种方法的区别是,第二种方法更容易替换执行其他任务
动态生成角色列表
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
|
#!/usr/bin/env python
#encoding=utf-8
import
sys
import
os
import
requests
from
fabric
.
api
import
env
from
fabric
.
api
import
run
from
fabric
.
api
import
put
from
fabric
.
api
import
execute
from
fabric
.
api
import
roles
from
fabric
.
api
import
parallel
from
fabric
.
api
import
cd
from
fabric
.
api
import
task
env
.
user
=
'test'
env
.
password
=
'test'
cmdburl
=
'http://cmdb.test.com/test/listServer.do'
## 根据域名(服务名)查询该域的所有服务器列表
def
find_ips_by_domain
(
domain_name
)
:
ips
=
[
]
payload
=
{
'domain'
:
domain_name
}
res
=
requests
.
get
(
cmdburl
,
params
=
payload
)
hosts
=
res
.
json
(
)
[
'object'
]
[
0
]
[
'servers'
]
for
host
in
hosts
:
host_ip
=
host
[
'ip'
]
ips
.
append
(
host_ip
)
return
ips
@
task
def
gener_roles
(
domain_name
)
:
ips
=
find_ips_by_domain
(
domain_name
)
### 动态生成角色列表
*
*
env
.
roledefs
[
'ips'
]
=
map
(
lambda
x
:
x
,
ips
)
*
*
### 根据生成的角色列表处理任务
execute
(
do_work
)
@
roles
(
'ips'
)
def
do_work
(
)
:
run
(
'echo "Running stress test..."'
)
|
执行任务的方式为:
1
|
fab
gener_roles
:
test
.
com
|
参考资料
- Using execute with dynamically-set host lists
- http://my.oschina.net/indestiny/blog/290239
收 藏
转载请注明:成长的对话 » Fabric动态生成主机列表和角色列表