Add scan_new_hosts feature in ansible foreman inventory (#33743)

pull/33705/merge
Giovanni Sciortino 7 years ago committed by Sam Doran
parent c421878523
commit ff923fb6b0

@ -155,3 +155,6 @@ rich_params = False
[cache] [cache]
path = . path = .
max_age = 60 max_age = 60
# Whether to scan foreman to add recently created hosts in inventory cache
scan_new_hosts = True

@ -139,6 +139,10 @@ class ForemanInventory(object):
self.cache_max_age = config.getint('cache', 'max_age') self.cache_max_age = config.getint('cache', 'max_age')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
self.cache_max_age = 60 self.cache_max_age = 60
try:
self.scan_new_hosts = config.getboolean('cache', 'scan_new_hosts')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
self.scan_new_hosts = False
return True return True
@ -267,13 +271,15 @@ class ForemanInventory(object):
regex = r"[^A-Za-z0-9\_]" regex = r"[^A-Za-z0-9\_]"
return re.sub(regex, "_", word.replace(" ", "")) return re.sub(regex, "_", word.replace(" ", ""))
def update_cache(self): def update_cache(self, scan_only_new_hosts=False):
"""Make calls to foreman and save the output in a cache""" """Make calls to foreman and save the output in a cache"""
self.groups = dict() self.groups = dict()
self.hosts = dict() self.hosts = dict()
for host in self._get_hosts(): for host in self._get_hosts():
if host['name'] in self.cache.keys() and scan_only_new_hosts:
continue
dns_name = host['name'] dns_name = host['name']
host_data = self._get_host_data_by_id(host['id']) host_data = self._get_host_data_by_id(host['id'])
@ -387,6 +393,8 @@ class ForemanInventory(object):
self.load_facts_from_cache() self.load_facts_from_cache()
self.load_hostcollections_from_cache() self.load_hostcollections_from_cache()
self.load_cache_from_cache() self.load_cache_from_cache()
if self.scan_new_hosts:
self.update_cache(True)
def get_host_info(self): def get_host_info(self):
"""Get variables about a specific host""" """Get variables about a specific host"""

Loading…
Cancel
Save