pref: 完成收集资产信息任务

pull/8970/head
ibuler 2022-10-17 11:22:21 +08:00
parent 4e8e4e4bb7
commit 6a33129349
9 changed files with 30 additions and 15 deletions

View File

@ -133,7 +133,7 @@ class BasePlaybookManager:
summary = cb.summary summary = cb.summary
for state, hosts in summary.items(): for state, hosts in summary.items():
for host in hosts: for host in hosts:
result = cb.result.get(host) result = cb.host_results.get(host)
if state == 'ok': if state == 'ok':
self.on_host_success(host, result) self.on_host_success(host, result)
else: else:

View File

@ -2,11 +2,13 @@
# #
# #
from .change_secret.manager import ChangeSecretManager from .change_secret.manager import ChangeSecretManager
from .gather_facts.manager import GatherFactsManager
class ExecutionManager: class ExecutionManager:
manager_type_mapper = { manager_type_mapper = {
'change_secret': ChangeSecretManager, 'change_secret': ChangeSecretManager,
'gather_facts': GatherFactsManager,
} }
def __init__(self, execution): def __init__(self, execution):

View File

@ -1,2 +1,2 @@
# all base inventory in base/base_inventory.txt # all base inventory in base/base_inventory.txt
asset_name(ip) ...base_inventory_vars asset_name ...base_inventory_vars

View File

@ -1,12 +1,6 @@
- hosts: windows - hosts: windows
gather_facts: yes gather_facts: yes
tasks: tasks:
# - name: Gather facts windows
# setup:
# register: facts
#
# - debug:
# var: facts
- name: Get info - name: Get info
set_fact: set_fact:
info: info:
@ -19,6 +13,6 @@
sn: "{{ ansible_product_serial }}" sn: "{{ ansible_product_serial }}"
cpu_vcpus: "{{ ansible_processor_vcpus }}" cpu_vcpus: "{{ ansible_processor_vcpus }}"
memory: "{{ ansible_memtotal_mb }}" memory: "{{ ansible_memtotal_mb }}"
t
- debug: - debug:
var: info var: info

View File

@ -1,5 +1,8 @@
from common.utils import get_logger
from ..base.manager import BasePlaybookManager from ..base.manager import BasePlaybookManager
logger = get_logger(__name__)
class GatherFactsManager(BasePlaybookManager): class GatherFactsManager(BasePlaybookManager):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
@ -13,10 +16,16 @@ class GatherFactsManager(BasePlaybookManager):
def host_callback(self, host, asset=None, **kwargs): def host_callback(self, host, asset=None, **kwargs):
super().host_callback(host, asset=asset, **kwargs) super().host_callback(host, asset=asset, **kwargs)
self.host_asset_mapper[host['name']] = asset self.host_asset_mapper[host['name']] = asset
return host
def on_host_success(self, host, result): def on_host_success(self, host, result):
print("Host: {}".format(host)) info = result.get('Get info', {}).get('res', {}).get('ansible_facts', {}).get('info', {})
print("Result: {}".format(result)) 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))

View File

@ -19,12 +19,14 @@ logger = get_logger(__file__)
def update_asset_hardware_info_on_created(asset): def update_asset_hardware_info_on_created(asset):
logger.debug("Update asset `{}` hardware info".format(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): def test_asset_conn_on_created(asset):
logger.debug("Test asset `{}` connectivity".format(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) @receiver(pre_save, sender=Node)

View File

@ -18,6 +18,14 @@ class DefaultCallback:
self.status = 'running' self.status = 'running'
self.finished = False 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): def is_success(self):
return self.status != 'successful' return self.status != 'successful'

View File

@ -181,13 +181,13 @@ class JMSInventory:
else: else:
hosts.append(host) 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: if exclude_hosts:
print(_("Skip hosts below:")) print(_("Skip hosts below:"))
for i, host in enumerate(exclude_hosts, start=1): for i, host in enumerate(exclude_hosts, start=1):
print("{}: [{}] \t{}".format(i, host['name'], host['error'])) 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': {}}} data = {'all': {'hosts': {}}}
for host in hosts: for host in hosts:
name = host.pop('name') name = host.pop('name')