aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido G√ľnther <agx@sigxcpu.org>2016-08-05 16:52:55 +0200
committerDaniel Lobato <elobatocs@gmail.com>2016-08-08 13:25:22 +0200
commitaaac5ade66fb3345bcb6f3c3a979d596c91c602e (patch)
treeddea22fd4d2c32bff26ef2b698f45f734db79370
parentc9adc706352cb84fa5069a4be0d7cadd937da018 (diff)
Allow to disable fact cache
This brings down inventory generation by about 50% and I doubt lots of people use it to fetch facts from other hosts.
-rw-r--r--foreman.ini2
-rwxr-xr-xforeman_ansible_inventory.py15
2 files changed, 15 insertions, 2 deletions
diff --git a/foreman.ini b/foreman.ini
index fb103a8..42312da 100644
--- a/foreman.ini
+++ b/foreman.ini
@@ -10,6 +10,8 @@ group_patterns = ["{app}-{tier}-{color}",
"{app}",
"{tier}"]
group_prefix = foreman_
+# Whether to fetch facts from Foreman and store them on the host
+want_facts = True
[cache]
path = .
diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py
index bba2cdd..e42aeb3 100755
--- a/foreman_ansible_inventory.py
+++ b/foreman_ansible_inventory.py
@@ -68,8 +68,10 @@ class ForemanInventory(object):
self.inventory['_meta']['hostvars'][hostname] = {
'foreman': self.cache[hostname],
'foreman_params': self.params[hostname],
- 'foreman_facts': self.facts[hostname],
}
+ if self.want_facts:
+ self.inventory['_meta']['hostvars'][hostname]['foreman_facts'] = self.facts[hostname]
+
data_to_print += self.json_format_dict(self.inventory, True)
print(data_to_print)
@@ -121,6 +123,11 @@ class ForemanInventory(object):
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
self.group_prefix = "foreman_"
+ try:
+ self.want_facts = config.getboolean('ansible', 'want_facts')
+ except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+ self.want_facts = True
+
# Cache related
try:
cache_path = os.path.expanduser(config.get('cache', 'path'))
@@ -200,6 +207,9 @@ class ForemanInventory(object):
"""
Fetch all host facts of the host
"""
+ if not self.want_facts:
+ return {}
+
ret = self._get_facts_by_id(host['id'])
if len(ret.values()) == 0:
facts = {}
@@ -293,7 +303,8 @@ class ForemanInventory(object):
def load_facts_from_cache(self):
""" Reads the index from the cache file sets self.index """
-
+ if not self.want_facts:
+ return
cache = open(self.cache_path_facts, 'r')
json_facts = cache.read()
self.facts = json.loads(json_facts)