Change: Code cleanup, create _populate_host method

This commit is contained in:
Robin Pierre Cordier 2022-03-10 15:39:25 -05:00
parent 82b44ad8ad
commit 0c89469c1e
2 changed files with 22 additions and 14 deletions

View File

@ -87,9 +87,6 @@ class InventoryModule(BaseInventoryPlugin, Cacheable, Constructable):
self.groups = self.get_option('groups') self.groups = self.get_option('groups')
self.keyed_groups = self.get_option('keyed_groups') self.keyed_groups = self.get_option('keyed_groups')
# self.process_scope = self.get_option('process_scope')
# self.process_results = self.get_option('process_results')
# Prepare Kheops instance # Prepare Kheops instance
self.config_file = self.get_option('config') self.config_file = self.get_option('config')
ansible_config = { ansible_config = {
@ -102,21 +99,32 @@ class InventoryModule(BaseInventoryPlugin, Cacheable, Constructable):
self.config_file, self.config_file,
path, path,
] ]
kheops = AnsibleKheops(configs=configs, display=self.display) self.kheops = AnsibleKheops(configs=configs, display=self.display)
# Loop over each hosts # Loop over each hosts
for host_name in inventory.hosts: for host_name in inventory.hosts:
try:
self._populate_host(host_name)
except Exception as err:
self.display.error(f"Got errors while processing Kheops lookup for host: %s, %s" % (host_name, err))
raise err
def _populate_host(self, host_name):
host = self.inventory.get_host(host_name) host = self.inventory.get_host(host_name)
ret = kheops.super_lookup( try:
ret = self.kheops.super_lookup(
keys=None, keys=None,
scope=None, scope=None,
_templar=self.templar, _templar=self.templar,
_variables=host.get_vars(), _variables=host.get_vars(),
jinja2_native=self.jinja2_native, jinja2_native=self.jinja2_native,
#trace=True,
#explain=True,
) )
except AnsibleError as err:
self.display.error (f"Could lookup Kheops data for host: {host_name}")
raise err
# Inject variables into host # Inject variables into host
for key, value in ret.items(): for key, value in ret.items():

View File

@ -243,7 +243,7 @@ class AnsibleKheops:
raise AnsibleError("Kheops client mode is not implemented") raise AnsibleError("Kheops client mode is not implemented")
self.config = config self.config = config
self.display.v("Kheops instance has been created") self.display.v("Kheops instance has been created, with config: %s" % config['instance_config'])
def get_config(self): def get_config(self):
""" """