1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Brian Coca 2017-04-07 13:04:55 -04:00 committed by Brian Coca
parent 3965689328
commit 602a2bca1b

View file

@ -550,7 +550,7 @@ class VariableManager:
else: else:
return name 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 helper function, which loads the file and gets the
basename of the file without the extension basename of the file without the extension
@ -569,24 +569,26 @@ class VariableManager:
names.sort() names.sort()
# do not parse hidden files or dirs, e.g. .svn/ # 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: 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: if results is not None:
data = combine_vars(data, results) data = combine_vars(data, results)
else: else:
file_name, ext = os.path.splitext(path) file_name, ext = os.path.splitext(path)
data = None data = None
if not ext or ext not in C.YAML_FILENAME_EXTENSIONS: if not filter_ext or ext in C.YAML_FILENAME_EXTENSIONS:
for test_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 new_path = path + test_ext
if loader.path_exists(new_path): if loader.path_exists(new_path):
data = loader.load_from_file(new_path) data = loader.load_from_file(new_path)
break break
else:
if loader.path_exists(path):
data = loader.load_from_file(path)
rval = AnsibleInventoryVarsData() rval = AnsibleInventoryVarsData()
rval.path = path rval.path = path