摘要:在使用的過程中,一般我們常用的方式是手工填寫主機列表或者是角色列表,但是這樣當服務器數(shù)量超級多的時候,你會有想死的感覺的。
在使用 Fabric 的過程中,一般我們常用的方式是手工填寫主機列表或者是角色列表,但是這樣當服務器數(shù)量超級多的時候,你會有想死的感覺的。正好公司有 cmdb 的話,就可以結(jié)合 CMDB 來做。
動態(tài)生成主機列表PS:如果公司沒有開發(fā) CMDB 系統(tǒng),也自己盡量弄個簡單的系統(tǒng)錄入服務器數(shù)據(jù)吧,不要那么復雜,提供 API,能獲取主機列表即可。
通過參考 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. @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)
然后執(zhí)行命令:
$ 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"]
現(xiàn)在我們就可以根據(jù) CMDB 接口來動態(tài)生成主機列表了。具體見代碼吧
方法一:
#!/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)
# 調(diào)用 fab set_hosts:app fab set_hosts:db
方法二:
#!/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)
#調(diào)用 fab set_hosts:test.com do_work
上面兩種方法的區(qū)別是,第二種方法更容易替換執(zhí)行其他任務
動態(tài)生成角色列表
#!/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" ## 根據(jù)域名(服務名)查詢該域的所有服務器列表 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) ### 動態(tài)生成角色列表 **env.roledefs["ips"] = map(lambda x: x, ips)** ### 根據(jù)生成的角色列表處理任務 execute(do_work) @roles("ips") def do_work(): run("echo "Running stress test..."")
執(zhí)行任務的方式為:
fab gener_roles:test.com參考資料
Using execute with dynamically-set host lists
http://my.oschina.net/indestiny/blog/290239
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/37474.html
摘要:是基于實現(xiàn)的遠程安全連接,支持認證及密鑰方法。利用函數(shù)發(fā)送到,通過函數(shù)獲取回顯。如下全局屬性設定對象的作用是定義的全局設定,支持多個屬性及自定義屬性。相比確實簡化了不少。出現(xiàn)異常時,發(fā)出警告,繼續(xù)執(zhí)行,不要終止。 paramiko paramiko是基于Python實現(xiàn)的SSH2遠程安全連接,支持認證及密鑰方法。可以實現(xiàn)遠程命令執(zhí)行,文件傳輸,中間SSH代理等功能,相對于Pexpect...
摘要:環(huán)境服務器目標服務器組一共臺服務器需求我需要把我服務器上的某些文件同步到集群,但是我又需要并發(fā)執(zhí)行,而不是通過循環(huán)或者是串行的方式。 環(huán)境: fabric 服務器:10.10.1.1 目標服務器組:test.com (10.10.1.2-21)一共 20 臺服務器 需求: 我需要把我 fabric 服務器上的某些文件同步到 test.com 集群,但是我又需要并發(fā)執(zhí)行,而不是通過 ...
摘要:區(qū)塊鏈接到區(qū)塊,區(qū)塊鏈接到區(qū)塊。共識整個交易流的更廣泛的術(shù)語,用于生成順序協(xié)議并確認構(gòu)成區(qū)塊的交易集合的正確性。策略策略是由數(shù)字身份的屬性組成的表達式,例如。在中,智能合約被稱為鏈碼,智能合約鏈碼安裝在對等節(jié)點上并實例化為一個或多個通道。 術(shù)語表 術(shù)語很重要,以便所有Hyperledger Fabric用戶和開發(fā)人員都同意每個特定術(shù)語的含義,例如,什么是智能合約。文檔將根據(jù)需要引用術(shù)語...
摘要:私有通道是受限制的消息傳遞路徑,可用于為網(wǎng)絡成員的特定子集提供交易隱私和機密性。所有數(shù)據(jù),包括交易,成員和通道信息,在通道上是不可見的,并且任何未明確授予對通頻道的訪問權(quán)限的網(wǎng)絡成員都無法訪問。 Hyperledger Fabric功能 Hyperledger Fabric是分布式分類賬技術(shù)(DLT)的一種實現(xiàn),可在模塊化區(qū)塊鏈架構(gòu)中提供企業(yè)級網(wǎng)絡安全性,可擴展性,機密性和性能,Hyp...
摘要:的證書撤銷列表構(gòu)成不再有效的證書的參考,證書的撤銷可能由于多種原因而發(fā)生,例如,因為與證書關聯(lián)的加密私有材料已被公開導致證書可能會被撤銷。描述一個名為的當事人的數(shù)字證書,是證書的,高亮的文本顯示了關于的關鍵事實。 身份 什么是身份? 區(qū)塊鏈網(wǎng)絡中的不同參與者包括對等點、排序者、客戶端應用程序,管理員等。這些參與者中的每一個 — 網(wǎng)絡內(nèi)部或外部能夠使用服務的活動元素 — 都具有封裝在X....
閱讀 1680·2019-08-30 13:04
閱讀 2234·2019-08-30 12:59
閱讀 1796·2019-08-29 18:34
閱讀 1903·2019-08-29 17:31
閱讀 1282·2019-08-29 15:42
閱讀 3567·2019-08-29 15:37
閱讀 2892·2019-08-29 13:45
閱讀 2800·2019-08-26 13:57