Ansible基于服務(wù)樹(shù)怎樣進(jìn)行分組全量接口調(diào)用,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)主營(yíng)交口網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開(kāi)發(fā),交口h5微信平臺(tái)小程序開(kāi)發(fā)搭建,交口網(wǎng)站營(yíng)銷推廣歡迎交口等地區(qū)企業(yè)咨詢
品茶:代碼是基于我們的服務(wù)樹(shù)結(jié)構(gòu)進(jìn)行構(gòu)建,如果需要自己構(gòu)建相應(yīng)服務(wù)樹(shù)則可以根據(jù)group host inventory進(jìn)行自行構(gòu)建。我們中帶有中文,所以命令行模式需要2.0才可以調(diào)中文,1.9需要改代碼。直接調(diào)模塊不受影響。
ansible2.0更貼近于ansible cli的常用命令執(zhí)行方式,不同于上一版本只能發(fā)送單個(gè)命令或playbook;而更推薦用戶在調(diào)用ansibleAPI的時(shí)候,將playbook的每個(gè)task拆分出來(lái),獲取每個(gè)task的結(jié)果。能夠跟靈活處理在執(zhí)行批量作業(yè)過(guò)程中的各種反饋。
將執(zhí)行操作的隊(duì)列模型,包含各類環(huán)境參數(shù)設(shè)置,歸結(jié)到“ansible.executor.task_queue_manager”類中
將執(zhí)行過(guò)程中的各個(gè)task的設(shè)置,或者說(shuō)playbook中的編排內(nèi)容,歸結(jié)到“ansible.playbook.play”中
from collections import namedtuple #有命元組 from ansible.parsing.dataloader import DataLoader #數(shù)據(jù)解析 from ansible.vars import VariableManager # 變量管旦 from ansible.inventory import Inventory # 主機(jī)配置信息 from ansible.playbook.play import Play # 劇本 from ansible.executor.task_queue_manager import TaskQueueManager # 任務(wù)消息隊(duì)列 from ansible.plugins.callback import CallbackBase #回調(diào)
inventory --> 由ansible.inventory模塊創(chuàng)建,用于導(dǎo)入inventory文件
variable_manager --> 由ansible.vars模塊創(chuàng)建,用于存儲(chǔ)各類變量信息
loader --> 由ansible.parsing.dataloader模塊創(chuàng)建,用于數(shù)據(jù)解析
options --> 存放各類配置信息的數(shù)據(jù)字典
passwords --> 登錄密碼,可設(shè)置加密信息
stdout_callback --> 回調(diào)函數(shù)
# #coding:utf8 import json import sys from ansible.runner import Runner from ansible.inventory.group import Group from ansible.inventory.host import Host from ansible.inventory import Inventory from ansible import playbook from ansible import callbacks from ansible import utils from cmdb import groups class CmdbInventory(object): ''' Get ansible.inventory for cmdb parse tree ''' def __init__(self): self.cmdbs = groups() self.inventory = self.init_inventory() def init_inventory(self, inventory=Inventory(host_list=[])): ''' :param inventory: default param, init cmdb Tree info. :return: ansible.inventory type ''' for name in self.cmdbs: if name == "_meta": # 主機(jī)變量,暫不處理 pass g = Group(name=name) # 設(shè)置組環(huán)境變量 if self.cmdbs[name].get("vars", None): vars = self.cmdbs[name]["vars"] for k,v in vars.iteritems(): g.set_variable(k, v) # 添加主機(jī)進(jìn)主機(jī)組 if self.cmdbs[name].get("hosts", None): hosts = self.cmdbs[name]["hosts"] for host in hosts: h = Host(name=host) g.add_host(h) inventory.add_group(g) # 處理子組 for name in self.cmdbs: if self.cmdbs[name].get("children", None): children = self.cmdbs[name]["children"] for child in children: g = inventory.get_group(name) child = inventory.get_group(child) g.add_child_group(child) # 處理主機(jī)的環(huán)境變量 hostvars = self.cmdbs.get("_meta",{}).get("hostvars", {}) if hostvars: for host in hostvars: inve_host = inventory.get_host(host) for k, v in hostvars[host].iteritems(): inve_host.set_variable(k, v) return inventory class Ansible(object): def __init__(self, transport="paramiko", module_name="ping", module_args="", pattern="", remote_user="", remote_pass="", play_book=False, yml_path=""): ''' Run a ansible task :param transport: paramiko, ssh, smart :param module_name: ansible module name :param module_args: ansible module args :param pattern: ansible pattern :param remote_user: transport user :param remote_pass: transport password :return: ansible task result ''' if not remote_user or not remote_pass: raise ValueError("Ansible class need params remote_user, remote_pass") if play_book: if not yml_path: raise ValueError("playbook need params yml_path") else: if not module_name or not pattern: raise ValueError("Ad-hoc need params module_name, pattern") if transport not in ("paramiko", "ssh", "smart"): raise ValueError("params transport not in paramiko, ssh, smart.") self.transport = transport self.module_name = module_name self.module_args = module_args self.pattern = pattern.decode("utf-8") # 這是因?yàn)橹形膯?wèn)題 self.remote_user = remote_user self.remote_pass = remote_pass self.play_book = play_book self.yml_path = yml_path # A 通過(guò)解析方式(這是一種單獨(dú)的方式) # ci = CmdbInventory() # inventory = ci.inventory # B 通過(guò)腳本方式(這是一種全量的方式,可通過(guò)修改文件名) self.inventory = Inventory(host_list="cmdb.py") def task(self): '''Ansible Ad-Hoc''' try: runner = Runner( transport=self.transport, module_name=self.module_name, module_args=self.module_args, pattern=self.pattern, forks=10, inventory=self.inventory, remote_user=self.remote_user, remote_pass=self.remote_pass ) result = runner.run() return True, result except Exception as e: return False, str(e) def playbook(self): stats = callbacks.AggregateStats() playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) runner_cb = callbacks.PlaybookRunnerCallbacks(stats=stats, verbose=utils.VERBOSITY) # B 通過(guò)腳本方式(這是一種全量的方式,可通過(guò)修改文件名) inventory = Inventory(host_list="cmdb.py") pb = playbook.PlayBook( inventory=self.inventory, playbook=self.yml_path, stats=stats, callbacks=playbook_cb, runner_callbacks=runner_cb, check=True, transport=self.transport, remote_user=self.remote_user, remote_pass=self.remote_pass ) result = pb.run() return True, result if __name__ == "__main__": ansible = Ansible(remote_user="", remote_pass="", play_book=True, yml_path="playbooks/ping.yml") # result = ansible.task() result = ansible.playbook() print json.dumps(result, indent=4)
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
當(dāng)前標(biāo)題:Ansible基于服務(wù)樹(shù)怎樣進(jìn)行分組全量接口調(diào)用
標(biāo)題URL:http://aaarwkj.com/article2/pesjic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、企業(yè)網(wǎng)站制作、響應(yīng)式網(wǎng)站、做網(wǎng)站、外貿(mào)建站、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)