From 6a33129349344c3563dc0f714e3d3a161ea78e35 Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 17 Oct 2022 11:22:21 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=20=E5=AE=8C=E6=88=90=E6=94=B6=E9=9B=86?= =?UTF-8?q?=E8=B5=84=E4=BA=A7=E4=BF=A1=E6=81=AF=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/automations/base/manager.py | 2 +- apps/assets/automations/endpoint.py | 2 ++ apps/assets/automations/gather_facts/README.md | 0 .../automations/gather_facts/demo_inventory.txt | 2 +- .../automations/gather_facts/host/windows/main.yml | 8 +------- apps/assets/automations/gather_facts/manager.py | 13 +++++++++++-- apps/assets/signal_handlers/asset.py | 6 ++++-- apps/ops/ansible/callback.py | 8 ++++++++ apps/ops/ansible/inventory.py | 4 ++-- 9 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 apps/assets/automations/gather_facts/README.md diff --git a/apps/assets/automations/base/manager.py b/apps/assets/automations/base/manager.py index 59a5bf75c..b1d140492 100644 --- a/apps/assets/automations/base/manager.py +++ b/apps/assets/automations/base/manager.py @@ -133,7 +133,7 @@ class BasePlaybookManager: summary = cb.summary for state, hosts in summary.items(): for host in hosts: - result = cb.result.get(host) + result = cb.host_results.get(host) if state == 'ok': self.on_host_success(host, result) else: diff --git a/apps/assets/automations/endpoint.py b/apps/assets/automations/endpoint.py index 472089ca1..3e69d2953 100644 --- a/apps/assets/automations/endpoint.py +++ b/apps/assets/automations/endpoint.py @@ -2,11 +2,13 @@ # # from .change_secret.manager import ChangeSecretManager +from .gather_facts.manager import GatherFactsManager class ExecutionManager: manager_type_mapper = { 'change_secret': ChangeSecretManager, + 'gather_facts': GatherFactsManager, } def __init__(self, execution): diff --git a/apps/assets/automations/gather_facts/README.md b/apps/assets/automations/gather_facts/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/apps/assets/automations/gather_facts/demo_inventory.txt b/apps/assets/automations/gather_facts/demo_inventory.txt index ed011eae2..529a74e67 100644 --- a/apps/assets/automations/gather_facts/demo_inventory.txt +++ b/apps/assets/automations/gather_facts/demo_inventory.txt @@ -1,2 +1,2 @@ # all base inventory in base/base_inventory.txt -asset_name(ip) ...base_inventory_vars +asset_name ...base_inventory_vars diff --git a/apps/assets/automations/gather_facts/host/windows/main.yml b/apps/assets/automations/gather_facts/host/windows/main.yml index 723aa7720..377ffd10a 100644 --- a/apps/assets/automations/gather_facts/host/windows/main.yml +++ b/apps/assets/automations/gather_facts/host/windows/main.yml @@ -1,12 +1,6 @@ - hosts: windows gather_facts: yes tasks: -# - name: Gather facts windows -# setup: -# register: facts -# -# - debug: -# var: facts - name: Get info set_fact: info: @@ -19,6 +13,6 @@ sn: "{{ ansible_product_serial }}" cpu_vcpus: "{{ ansible_processor_vcpus }}" memory: "{{ ansible_memtotal_mb }}" -t + - debug: var: info diff --git a/apps/assets/automations/gather_facts/manager.py b/apps/assets/automations/gather_facts/manager.py index 4b782ae31..a2072b138 100644 --- a/apps/assets/automations/gather_facts/manager.py +++ b/apps/assets/automations/gather_facts/manager.py @@ -1,5 +1,8 @@ +from common.utils import get_logger from ..base.manager import BasePlaybookManager +logger = get_logger(__name__) + class GatherFactsManager(BasePlaybookManager): def __init__(self, *args, **kwargs): @@ -13,10 +16,16 @@ class GatherFactsManager(BasePlaybookManager): def host_callback(self, host, asset=None, **kwargs): super().host_callback(host, asset=asset, **kwargs) self.host_asset_mapper[host['name']] = asset + return host def on_host_success(self, host, result): - print("Host: {}".format(host)) - print("Result: {}".format(result)) + info = result.get('Get info', {}).get('res', {}).get('ansible_facts', {}).get('info', {}) + asset = self.host_asset_mapper.get(host) + if asset and info: + asset.info = info + asset.save() + else: + logger.error("Not found info, task name must be 'Get info': {}".format(host)) diff --git a/apps/assets/signal_handlers/asset.py b/apps/assets/signal_handlers/asset.py index 5aac26319..6caef6385 100644 --- a/apps/assets/signal_handlers/asset.py +++ b/apps/assets/signal_handlers/asset.py @@ -19,12 +19,14 @@ logger = get_logger(__file__) def update_asset_hardware_info_on_created(asset): logger.debug("Update asset `{}` hardware info".format(asset)) - update_assets_hardware_info_util.delay([asset]) + # Todo: + # update_assets_hardware_info_util.delay([asset]) def test_asset_conn_on_created(asset): logger.debug("Test asset `{}` connectivity".format(asset)) - test_asset_connectivity_util.delay([asset]) + # Todo: + # test_asset_connectivity_util.delay([asset]) @receiver(pre_save, sender=Node) diff --git a/apps/ops/ansible/callback.py b/apps/ops/ansible/callback.py index 84e106e64..344605a18 100644 --- a/apps/ops/ansible/callback.py +++ b/apps/ops/ansible/callback.py @@ -18,6 +18,14 @@ class DefaultCallback: self.status = 'running' self.finished = False + @property + def host_results(self): + results = {} + for state, hosts in self.result.items(): + for host, items in hosts.items(): + results[host] = items + return results + def is_success(self): return self.status != 'successful' diff --git a/apps/ops/ansible/inventory.py b/apps/ops/ansible/inventory.py index ce2c69a46..f6c19b7a2 100644 --- a/apps/ops/ansible/inventory.py +++ b/apps/ops/ansible/inventory.py @@ -181,13 +181,13 @@ class JMSInventory: else: hosts.append(host) - exclude_hosts = list(filter(lambda x: x.get('exclude'), hosts)) + exclude_hosts = list(filter(lambda x: x.get('error'), hosts)) if exclude_hosts: print(_("Skip hosts below:")) for i, host in enumerate(exclude_hosts, start=1): print("{}: [{}] \t{}".format(i, host['name'], host['error'])) - hosts = list(filter(lambda x: not x.get('exclude'), hosts)) + hosts = list(filter(lambda x: not x.get('error'), hosts)) data = {'all': {'hosts': {}}} for host in hosts: name = host.pop('name')