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
(cherry picked from commit 602a2bca1b
)
This commit is contained in:
parent
1fdeef511c
commit
4567527249
1 changed files with 12 additions and 10 deletions
|
@ -541,7 +541,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
|
||||
|
@ -560,24 +560,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
|
||||
|
|
Loading…
Reference in a new issue