2022-10-17 03:22:21 +00:00
|
|
|
from common.utils import get_logger
|
2022-10-12 10:08:57 +00:00
|
|
|
from ..base.manager import BasePlaybookManager
|
|
|
|
|
2022-10-17 03:22:21 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2022-10-12 10:08:57 +00:00
|
|
|
|
|
|
|
class GatherFactsManager(BasePlaybookManager):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-10-14 10:59:28 +00:00
|
|
|
self.host_asset_mapper = {}
|
2022-10-12 10:08:57 +00:00
|
|
|
|
2022-10-14 10:59:28 +00:00
|
|
|
@classmethod
|
|
|
|
def method_type(cls):
|
|
|
|
return 'gather_facts'
|
2022-10-12 10:08:57 +00:00
|
|
|
|
2022-10-14 10:59:28 +00:00
|
|
|
def host_callback(self, host, asset=None, **kwargs):
|
|
|
|
super().host_callback(host, asset=asset, **kwargs)
|
|
|
|
self.host_asset_mapper[host['name']] = asset
|
2022-10-17 03:22:21 +00:00
|
|
|
return host
|
2022-10-12 10:08:57 +00:00
|
|
|
|
2022-10-14 10:59:28 +00:00
|
|
|
def on_host_success(self, host, result):
|
2022-10-17 03:22:21 +00:00
|
|
|
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))
|
2022-10-12 10:08:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|