mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Fix the way host and group vars are loaded
* In the VariableManager, we were not properly tracking if a file had already been loaded, so we continuously append data to the end of the list there for host and group vars, meaning large sets of data are duplicated multiple times * In the inventory, we were merging the host/group vars with the vars local to the host needlessly, as the VariableManager already handles that. This leads to needless duplication of the data and makes combining the vars in VariableManager take even longer.
This commit is contained in:
@@ -53,6 +53,11 @@ except ImportError:
|
||||
VARIABLE_CACHE = dict()
|
||||
HOSTVARS_CACHE = dict()
|
||||
|
||||
class AnsibleInventoryVarsData(dict):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(AnsibleInventoryVarsData, self).__init__(*args, **kwargs)
|
||||
self.path = None
|
||||
|
||||
def preprocess_vars(a):
|
||||
'''
|
||||
Ensures that vars contained in the parameter passed in are
|
||||
@@ -549,8 +554,11 @@ class VariableManager:
|
||||
if loader.path_exists(path):
|
||||
data = loader.load_from_file(path)
|
||||
|
||||
name = self._get_inventory_basename(path)
|
||||
return (name, data)
|
||||
rval = AnsibleInventoryVarsData()
|
||||
rval.path = path
|
||||
if data is not None:
|
||||
rval.update(data)
|
||||
return rval
|
||||
|
||||
def add_host_vars_file(self, path, loader):
|
||||
'''
|
||||
@@ -559,14 +567,21 @@ class VariableManager:
|
||||
the extension, for matching against a given inventory host name
|
||||
'''
|
||||
|
||||
(name, data) = self._load_inventory_file(path, loader)
|
||||
if data:
|
||||
if name not in self._host_vars_files:
|
||||
self._host_vars_files[name] = []
|
||||
self._host_vars_files[name].append(data)
|
||||
return data
|
||||
name = self._get_inventory_basename(path)
|
||||
if name not in self._host_vars_files:
|
||||
self._host_vars_files[name] = []
|
||||
|
||||
for entry in self._host_vars_files[name]:
|
||||
if entry.path == path:
|
||||
data = entry
|
||||
break
|
||||
else:
|
||||
return dict()
|
||||
data = self._load_inventory_file(path, loader)
|
||||
if data:
|
||||
self._host_vars_files[name].append(data)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def add_group_vars_file(self, path, loader):
|
||||
'''
|
||||
@@ -575,14 +590,20 @@ class VariableManager:
|
||||
the extension, for matching against a given inventory host name
|
||||
'''
|
||||
|
||||
(name, data) = self._load_inventory_file(path, loader)
|
||||
if data:
|
||||
if name not in self._group_vars_files:
|
||||
self._group_vars_files[name] = []
|
||||
self._group_vars_files[name].append(data)
|
||||
return data
|
||||
name = self._get_inventory_basename(path)
|
||||
if name not in self._group_vars_files:
|
||||
self._group_vars_files[name] = []
|
||||
|
||||
for entry in self._group_vars_files[name]:
|
||||
if entry.path == path:
|
||||
data = entry
|
||||
break
|
||||
else:
|
||||
return dict()
|
||||
data = self._load_inventory_file(path, loader)
|
||||
if data:
|
||||
self._group_vars_files[name].append(data)
|
||||
|
||||
return data
|
||||
|
||||
def clear_facts(self, hostname):
|
||||
'''
|
||||
|
||||
Reference in New Issue
Block a user