optimize file finding
This commit is contained in:
parent
deeffc61d7
commit
f23920aa5e
1 changed files with 21 additions and 13 deletions
|
@ -40,10 +40,11 @@ from ansible.inventory.host import Host
|
||||||
from ansible.inventory.group import Group
|
from ansible.inventory.group import Group
|
||||||
from ansible.utils.vars import combine_vars
|
from ansible.utils.vars import combine_vars
|
||||||
|
|
||||||
|
FOUND = {}
|
||||||
|
|
||||||
class VarsModule(BaseVarsPlugin):
|
class VarsModule(BaseVarsPlugin):
|
||||||
|
|
||||||
def get_vars(self, loader, path, entities):
|
def get_vars(self, loader, path, entities, cache=True):
|
||||||
''' parses the inventory file '''
|
''' parses the inventory file '''
|
||||||
|
|
||||||
if not isinstance(entities, list):
|
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)))
|
raise AnsibleParserError("Supplied entity must be Host or Group, got %s instead" % (type(entity)))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
found_files = []
|
||||||
# load vars
|
# load vars
|
||||||
opath = os.path.realpath(os.path.join(self._basedir, subdir))
|
opath = os.path.realpath(os.path.join(self._basedir, subdir))
|
||||||
b_opath = to_bytes(opath)
|
key = '%s.%s' % (entity.name, opath)
|
||||||
# no need to do much if path does not exist for basedir
|
if cache and key in FOUND:
|
||||||
if os.path.exists(b_opath):
|
found_files = FOUND[key]
|
||||||
if os.path.isdir(b_opath):
|
else:
|
||||||
self._display.debug("\tprocessing dir %s" % opath)
|
b_opath = to_bytes(opath)
|
||||||
for found in self._find_vars_files(opath, entity.name):
|
# no need to do much if path does not exist for basedir
|
||||||
self._display.debug("READING %s" % found)
|
if os.path.exists(b_opath):
|
||||||
new_data = loader.load_from_file(found, cache=True, unsafe=True)
|
if os.path.isdir(b_opath):
|
||||||
if new_data: # ignore empty files
|
self._display.debug("\tprocessing dir %s" % opath)
|
||||||
data = combine_vars(data, new_data)
|
found_files = self._find_vars_files(opath, entity.name)
|
||||||
else:
|
FOUND[key] = found_files
|
||||||
self._display.warning("Found %s that is not a directory, skipping: %s" % (subdir, opath))
|
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:
|
except Exception as e:
|
||||||
raise AnsibleParserError(to_text(e))
|
raise AnsibleParserError(to_text(e))
|
||||||
|
|
Loading…
Reference in a new issue