From ff923fb6b095eb0f23226f84312780c340030acc Mon Sep 17 00:00:00 2001 From: Giovanni Sciortino <30353557+giovannisciortino@users.noreply.github.com> Date: Thu, 14 Dec 2017 21:10:42 +0100 Subject: [PATCH] Add scan_new_hosts feature in ansible foreman inventory (#33743) --- contrib/inventory/foreman.ini | 3 +++ contrib/inventory/foreman.py | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/contrib/inventory/foreman.ini b/contrib/inventory/foreman.ini index 32091a58f06..055c82cdb46 100644 --- a/contrib/inventory/foreman.ini +++ b/contrib/inventory/foreman.ini @@ -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 diff --git a/contrib/inventory/foreman.py b/contrib/inventory/foreman.py index a83c094d95a..4d6fd32b809 100755 --- a/contrib/inventory/foreman.py +++ b/contrib/inventory/foreman.py @@ -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"""