diff --git a/apps/ops/ansible_api.py b/apps/ops/ansible_api.py index 09fb06cd5..48a313767 100644 --- a/apps/ops/ansible_api.py +++ b/apps/ops/ansible_api.py @@ -21,7 +21,6 @@ from ansible.plugins.callback import CallbackBase from models import Tasker, AnsiblePlay, AnsibleTask, AnsibleHostResult - logger = logging.getLogger(__name__) @@ -30,13 +29,13 @@ class AnsibleError(StandardError): class Config(object): - """Ansible运行时配置类, 用于初始化Ansible. + """Ansible运行时配置类, 用于初始化Ansible的一些默认配置. """ def __init__(self, verbosity=None, inventory=None, listhosts=None, subset=None, module_paths=None, extra_vars=None, - forks=None, ask_vault_pass=False, vault_password_files=None, new_vault_password_file=None, + forks=10, ask_vault_pass=False, vault_password_files=None, new_vault_password_file=None, output_file=None, tags=None, skip_tags=None, one_line=None, tree=None, ask_sudo_pass=False, ask_su_pass=False, sudo=None, sudo_user=None, become=None, become_method=None, become_user=None, become_ask_pass=False, - ask_pass=False, private_key_file=None, remote_user=None, connection="smart", timeout=None, ssh_common_args=None, + ask_pass=False, private_key_file=None, remote_user=None, connection="smart", timeout=10, ssh_common_args=None, sftp_extra_args=None, scp_extra_args=None, ssh_extra_args=None, poll_interval=None, seconds=None, check=False, syntax=None, diff=None, force_handlers=None, flush_cache=None, listtasks=None, listtags=None, module_path=None): self.verbosity = verbosity diff --git a/apps/ops/models.py b/apps/ops/models.py index d3e52566e..812445531 100644 --- a/apps/ops/models.py +++ b/apps/ops/models.py @@ -142,8 +142,9 @@ class AnsibleHostResult(models.Model): """ result = {} for key in interfaces: - if "ansible_" + key in facts.keys(): - result[key] = facts.get(key) + gather_key = "ansible_" + key + if gather_key in facts.keys(): + result[key] = facts.get(gather_key) return result def __deal_setup(self): @@ -175,7 +176,7 @@ class AnsibleHostResult(models.Model): data['interface'] = self.__gather_interface(facts, interfaces) return {"msg": None, "data": data} else: - return {"msg": "there isn't module_name field! can't process this data format", "data": None} + return {"msg": "there result isn't ansible setup module result! can't process this data format", "data": None} @property def deal_setup(self): diff --git a/apps/ops/taskers.py b/apps/ops/taskers.py index 17dbd3a5b..814a202ea 100644 --- a/apps/ops/taskers.py +++ b/apps/ops/taskers.py @@ -1,8 +1,9 @@ +# ~*~ coding: utf-8 ~*~ from __future__ import unicode_literals from .tasks import * -from .models import Tasker, AnsiblePlay, AnsibleTask, AnsibleHostResult +from .models import Tasker from uuid import uuid1 from celery.result import AsyncResult @@ -15,6 +16,39 @@ def get_result(task_id): return {"Completed": False, "data": None} +def __get_result_by_tasker_id(tasker_uuid, deal_method): + tasker = Tasker.objects.get(uuid=tasker_uuid) + total = tasker.total_hosts + total_len = len(total) + host_results = [] + + # 存储数据 + for play in tasker.plays.all(): + for t in play.tasks.all(): + task = {'name': t.name, 'uuid': t.uuid, 'percentage': 0, 'completed': {'success': {}, 'failed': {}}} + completed = [] + count = 0 + for h in t.host_results.all(): + completed.append(h.name) + count += 1 + if h.is_success: + result = getattr(h, deal_method) + if result.get('msg') is None: + task['completed']['success'][h.name] = result.get('data') + else: + task['completed']['failed'][h.name] = result.get('msg') + else: + task['completed']['failed'][h.name] = h.failed_msg + + # 计算进度 + task['percentage'] = float(count * 100 / total_len) + task['waited'] = list(set(total) - set(completed)) + + host_results.append(task) + + return host_results + + def start_get_hardware_info(*assets): name = "Get host hardware information" uuid = "tasker-" + uuid1().hex @@ -23,18 +57,30 @@ def start_get_hardware_info(*assets): def __get_hardware_info(tasker_uuid): - tasker = Tasker.objects.get(uuid=tasker_uuid) - host_results = [] - - for play in tasker.plays.all(): - for t in play.tasks.all(): - for h in t.host_results.all(): - host_results.append(h) - - return host_results + return __get_result_by_tasker_id(tasker_uuid, 'deal_setup') def get_hardware_info(tasker_uuid): + """ + + :param assets: 资产列表 + :return: 返回数据结构样列 + {u'data': [{u'completed': { + u'failed': {u'192.168.232.135': u'Authentication failure.'}, + u'success': {u'192.168.1.119': {u'cpu': u'GenuineIntel Intel Xeon E312xx (Sandy Bridge) 6\u6838', + u'disk': {: }, + u'env': {: }, + u'interface': {: }, + u'mem': 3951, + u'os': u'Ubuntu 16.04(xenial)', + u'sn': u'NA'}}}, + u'name': u'', + u'percentage': 100.0, + u'uuid': u'87cfedfe-ba55-44ff-bc43-e7e73b869ca1', + u'waited': []} + ], + u'msg': None} + """ try: return {"msg": None, "data": __get_hardware_info(tasker_uuid)} except Exception as e: @@ -49,18 +95,24 @@ def start_ping_test(*assets): def __get_ping_test(tasker_uuid): - tasker = Tasker.objects.get(uuid=tasker_uuid) - host_results = [] - - for play in tasker.plays.all(): - for t in play.tasks.all(): - for h in t.host_results.all(): - host_results.append(h) - - return host_results + return __get_result_by_tasker_id(tasker_uuid, 'deal_ping') def get_ping_test(tasker_uuid): + """ + + :param assets: 资产列表 + :return: 返回数据结构样列 + {u'data': [{u'completed': { + u'failed': {u'192.168.232.135': u'Authentication failure.'}, + u'success': {u'192.168.1.119': {u'success': True}}}, + u'name': u'', + u'percentage': 100.0, + u'uuid': u'3e6e0d3b-bee0-4383-b19e-bec6ba55d346', + u'waited': []} + ], + u'msg': None} + """ try: return {"msg": None, "data": __get_ping_test(tasker_uuid)} except Exception as e: