From 602a2bca1b0b54156903cc91b93a134d5889ea1e Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 7 Apr 2017 13:04:55 -0400 Subject: [PATCH] fix vars file selection fixes #17382 alternate to #22979 deal with cases in which group/host have . in name updated as per feedbck only be strict about extension when doing dirs also avoid ~ endings --- lib/ansible/vars/__init__.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 2f14849f37..74f41d9caf 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -550,7 +550,7 @@ class VariableManager: else: return name - def _load_inventory_file(self, path, loader): + def _load_inventory_file(self, path, loader, filter_ext=False): ''' helper function, which loads the file and gets the basename of the file without the extension @@ -569,24 +569,26 @@ class VariableManager: names.sort() # do not parse hidden files or dirs, e.g. .svn/ - paths = [os.path.join(path, name) for name in names if not name.startswith('.')] + paths = [os.path.join(path, name) for name in names if not (name.startswith('.') or name.endswith('~'))] for p in paths: - results = self._load_inventory_file(path=p, loader=loader) + results = self._load_inventory_file(path=p, loader=loader, filter_ext=True) if results is not None: data = combine_vars(data, results) else: file_name, ext = os.path.splitext(path) data = None - if not ext or ext not in C.YAML_FILENAME_EXTENSIONS: - for test_ext in C.YAML_FILENAME_EXTENSIONS: - new_path = path + test_ext - if loader.path_exists(new_path): - data = loader.load_from_file(new_path) - break - else: + if not filter_ext or ext in C.YAML_FILENAME_EXTENSIONS: if loader.path_exists(path): data = loader.load_from_file(path) + else: + # try appending yaml extenstion to find valid files + # avoid empty extensions otherwise all files would be tried + for test_ext in (ext for ext in C.YAML_FILENAME_EXTENSIONS if ext): + new_path = path + test_ext + if loader.path_exists(new_path): + data = loader.load_from_file(new_path) + break rval = AnsibleInventoryVarsData() rval.path = path