动态生成主机列表
通过参考 Fabric 的官方文档的 Using execute with dynamically-set host lists ,其中有这么一段示例代码:
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.@taskdef 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)
然后执行命令:
$ fab deploy:app$ fab deploy:db
其生成主机列表的格式如下:
['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 接口来动态生成主机列表了。具体见代码吧
方法一:
#!/usr/bin/env python#encoding=utf-8import sysimport osimport requests from fabric.api import envfrom fabric.api import runfrom fabric.api import putfrom fabric.api import executefrom fabric.api import rolesfrom fabric.api import parallelfrom fabric.api import cdfrom 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...'') @taskdef set_hosts(domain): # Update env.hosts instead of calling execute() host_list = find_ips_by_domain(domain) execute(do_work, hosts=host_list)
# 调用fab set_hosts:appfab set_hosts:db
方法二:
#!/usr/bin/env python#encoding=utf-8import sysimport osimport requests from fabric.api import envfrom fabric.api import runfrom fabric.api import putfrom fabric.api import executefrom fabric.api import rolesfrom fabric.api import parallelfrom fabric.api import cdfrom 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 @taskdef do_work(): run('echo 'Running stress test...'') @taskdef set_hosts(domain): # Update env.hosts instead of calling execute() env.hosts = find_ips_by_domain(domain)
#调用fab set_hosts:test.com do_work
上面两种方法的区别是,第二种方法更容易替换执行其他任务
动态生成角色列表
#!/usr/bin/env python#encoding=utf-8import sysimport osimport requests from fabric.api import envfrom fabric.api import runfrom fabric.api import putfrom fabric.api import executefrom fabric.api import rolesfrom fabric.api import parallelfrom fabric.api import cdfrom 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 @taskdef 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...'')
执行任务的方式为:
fab gener_roles:test.com
参考资料
收 藏
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。