optimize file finding

This commit is contained in:
Brian Coca 2017-05-24 16:14:07 -04:00 committed by Brian Coca
parent deeffc61d7
commit f23920aa5e

View file

@ -40,10 +40,11 @@ from ansible.inventory.host import Host
from ansible.inventory.group import Group
from ansible.utils.vars import combine_vars
FOUND = {}
class VarsModule(BaseVarsPlugin):
def get_vars(self, loader, path, entities):
def get_vars(self, loader, path, entities, cache=True):
''' parses the inventory file '''
if not isinstance(entities, list):
@ -61,20 +62,27 @@ class VarsModule(BaseVarsPlugin):
raise AnsibleParserError("Supplied entity must be Host or Group, got %s instead" % (type(entity)))
try:
found_files = []
# load vars
opath = os.path.realpath(os.path.join(self._basedir, subdir))
b_opath = to_bytes(opath)
# no need to do much if path does not exist for basedir
if os.path.exists(b_opath):
if os.path.isdir(b_opath):
self._display.debug("\tprocessing dir %s" % opath)
for found in self._find_vars_files(opath, entity.name):
self._display.debug("READING %s" % found)
new_data = loader.load_from_file(found, cache=True, unsafe=True)
if new_data: # ignore empty files
data = combine_vars(data, new_data)
else:
self._display.warning("Found %s that is not a directory, skipping: %s" % (subdir, opath))
key = '%s.%s' % (entity.name, opath)
if cache and key in FOUND:
found_files = FOUND[key]
else:
b_opath = to_bytes(opath)
# no need to do much if path does not exist for basedir
if os.path.exists(b_opath):
if os.path.isdir(b_opath):
self._display.debug("\tprocessing dir %s" % opath)
found_files = self._find_vars_files(opath, entity.name)
FOUND[key] = found_files
else:
self._display.warning("Found %s that is not a directory, skipping: %s" % (subdir, opath))
for found in found_files:
new_data = loader.load_from_file(found, cache=True, unsafe=True)
if new_data: # ignore empty files
data = combine_vars(data, new_data)
except Exception as e:
raise AnsibleParserError(to_text(e))