Add scan_new_hosts feature in ansible foreman inventory (#33743)

This commit is contained in:
Giovanni Sciortino 2017-12-14 21:10:42 +01:00 committed by Sam Doran
parent c421878523
commit ff923fb6b0
2 changed files with 12 additions and 1 deletions

View file

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

View file

@ -139,6 +139,10 @@ class ForemanInventory(object):
self.cache_max_age = config.getint('cache', 'max_age')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
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
@ -267,13 +271,15 @@ class ForemanInventory(object):
regex = r"[^A-Za-z0-9\_]"
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"""
self.groups = dict()
self.hosts = dict()
for host in self._get_hosts():
if host['name'] in self.cache.keys() and scan_only_new_hosts:
continue
dns_name = host['name']
host_data = self._get_host_data_by_id(host['id'])
@ -387,6 +393,8 @@ class ForemanInventory(object):
self.load_facts_from_cache()
self.load_hostcollections_from_cache()
self.load_cache_from_cache()
if self.scan_new_hosts:
self.update_cache(True)
def get_host_info(self):
"""Get variables about a specific host"""