diff --git a/lib/ansible/plugins/vars/host_group_vars.py b/lib/ansible/plugins/vars/host_group_vars.py index 6bebdb0f34..c8d22cc779 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 + found.append(b_path) + else: + # add valid extensions to name + for ext in C.YAML_FILENAME_EXTENSIONS: - 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) + 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