Commit e8d8f7c4 authored by yumaojun03's avatar yumaojun03

ops: ansible_api add ansible api 2.0 adhoc runner

parent f6b2abb1
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import os import os
import json
from ansible.executor.task_queue_manager import TaskQueueManager from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.inventory import Inventory, Host, Group from ansible.inventory import Inventory, Host, Group
from ansible.vars import VariableManager from ansible.vars import VariableManager
...@@ -10,6 +11,7 @@ from ansible.executor import playbook_executor ...@@ -10,6 +11,7 @@ from ansible.executor import playbook_executor
from ansible.utils.display import Display from ansible.utils.display import Display
from ansible.playbook.play import Play from ansible.playbook.play import Play
import ansible.constants as default_config import ansible.constants as default_config
from ansible.plugins.callback import CallbackBase
class AnsibleError(StandardError): class AnsibleError(StandardError):
...@@ -142,7 +144,7 @@ class MyInventory(object): ...@@ -142,7 +144,7 @@ class MyInventory(object):
if key not in ["name", "port", "ip", "username", "password", "key"]: if key not in ["name", "port", "ip", "username", "password", "key"]:
host.set_variable(key, value) host.set_variable(key, value)
for g in self.groups: for g in self.groups:
if g.name == asset['group']: if g.name == asset.get('group', 'default'):
g.add_host(host) g.add_host(host)
def validate(self): def validate(self):
...@@ -151,6 +153,7 @@ class MyInventory(object): ...@@ -151,6 +153,7 @@ class MyInventory(object):
def gen_inventory(self): def gen_inventory(self):
self.validate() self.validate()
i = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=[]) i = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=[])
self.__gen_group()
for g in self.groups: for g in self.groups:
i.add_group(g) i.add_group(g)
self.variable_manager.set_inventory(i) self.variable_manager.set_inventory(i)
...@@ -223,7 +226,7 @@ class PlayBookRunner(object): ...@@ -223,7 +226,7 @@ class PlayBookRunner(object):
class ADHocRunner(object): class ADHocRunner(object):
"""ADHoc接口 """ADHoc接口
""" """
def __init__(self, inventory, config, play_data, become_pass, verbosity=0): def __init__(self, inventory, config, become_pass=None, verbosity=0):
""" """
:param inventory: myinventory实例 :param inventory: myinventory实例
:param config: Config实例 :param config: Config实例
...@@ -251,22 +254,34 @@ class ADHocRunner(object): ...@@ -251,22 +254,34 @@ class ADHocRunner(object):
self.options.become_user = 'root' self.options.become_user = 'root'
self.passwords = {'become_pass': become_pass} self.passwords = {'become_pass': become_pass}
# 初始化Play # 初始化callback插件
self.play = Play().load(play_data, variable_manager=inventory.variable_manager, loader=inventory.loader) # self.results_callback = ResultCallback()
self.inventory = inventory.inventory # 初始化Play
play_source = {
"name": "Ansible Play",
"hosts": "*",
"gather_facts": "no",
"tasks": [
dict(action=dict(module='shell', args='id'), register='shell_out'),
dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
}
self.play = Play().load(play_source, variable_manager=inventory.variable_manager, loader=inventory.loader)
self.inventory = inventory
def run(self, play_data): def run(self):
"""执行ADHoc 记录日志, 处理结果 """执行ADHoc 记录日志, 处理结果
""" """
tqm = None tqm = None
# TODO:日志和结果分析
try: try:
# TODO: 日志和结果分析
tqm = TaskQueueManager( tqm = TaskQueueManager(
inventory=self.inventory, inventory=self.inventory.inventory,
variable_manager=self.inventory.variable_manager, variable_manager=self.inventory.variable_manager,
loader=self.inventory.loader, loader=self.inventory.loader,
stdout_callback=default_config.DEFAULT_STDOUT_CALLBACK,
options=self.options, options=self.options,
passwords=self.passwords passwords=self.passwords
) )
...@@ -275,4 +290,21 @@ class ADHocRunner(object): ...@@ -275,4 +290,21 @@ class ADHocRunner(object):
return result return result
finally: finally:
if tqm: if tqm:
tqm.cleanup() tqm.cleanup()
\ No newline at end of file
if __name__ == "__main__":
conf = Config()
assets = [{
"name": "localhost",
"ip": "localhost",
"port": "22",
"username": "yumaojun",
"password": "xxx",
"key": "asset_private_key",
}]
inv = MyInventory(*assets)
print inv.inventory.get_group('default').get_hosts()
hoc = ADHocRunner(inv, conf, 'xxx')
hoc.run()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment