From 13a1111ddebbadf2db161724eb470154ecdccc88 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 15 Jun 2017 12:34:05 -0400 Subject: [PATCH] tighter host/group_var discovery now only loads nonext or valid yaml extensions only first file gets loaded directories must match name (initially) so no name.yml is loaded anymore --- lib/ansible/plugins/vars/host_group_vars.py | 53 +++++++++++++-------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/ansible/plugins/vars/host_group_vars.py b/lib/ansible/plugins/vars/host_group_vars.py index 6bebdb0f340..c8d22cc779f 100644 --- a/lib/ansible/plugins/vars/host_group_vars.py +++ b/lib/ansible/plugins/vars/host_group_vars.py @@ -94,27 +94,42 @@ class VarsModule(BaseVarsPlugin): b_path = to_bytes(os.path.join(path, name)) found = [] - for ext in C.YAML_FILENAME_EXTENSIONS + ['']: - if '.' in ext: - full_path = b_path + to_bytes(ext) - elif ext: - full_path = b'.'.join([b_path, to_bytes(ext)]) + # first look for w/o extensions + if os.path.exists(b_path): + if os.path.isdir(b_path): + found.extend(self._get_dir_files(b_path)) else: - full_path = b_path - - if os.path.exists(full_path): - self._display.debug("\tfound %s" % to_text(full_path)) - if os.path.isdir(full_path): - # matched dir name, so use all files included recursively - for spath in os.listdir(full_path): - if spath.startswith('.'): - continue - full_spath = os.path.join(full_path, spath) - if os.path.isdir(full_spath): - found.extend(self._find_vars_files(full_spath, '')) - else: - found.append(full_spath) + found.append(b_path) + else: + # add valid extensions to name + for ext in C.YAML_FILENAME_EXTENSIONS: + + if '.' in ext: + full_path = b_path + to_bytes(ext) + elif ext: + full_path = b'.'.join([b_path, to_bytes(ext)]) else: + full_path = b_path + + if os.path.exists(full_path) and os.path.isfile(full_path): found.append(full_path) + break + return found + + def _get_dir_files(self, path): + + found = [] + for spath in os.listdir(path): + if not spath.startswith('.'): # skip hidden + + ext = os.path.splitext()[-1] + full_spath = os.path.join(path, spath) + + if os.path.isdir(full_spath) and not ext: # recursive search if dir + found.extend(self._get_dir_files(full_spath)) + elif os.path.isfile(full_spath) and (not ext or ext in C.YAML_FILENAME_EXTENSIONS): + # only consider files with valid extensions or no extension + found.append(full_spath) + return found