Fixing a few bugs in the HostVars performance areas

* Also refresh inventory in the HostVars manager process when things
  are changed via add_host/group_by
* Raise j2undefined rather than return it
This commit is contained in:
James Cammarata 2015-11-17 09:15:10 -05:00
parent 4d024fc82a
commit 9f31c073fe
4 changed files with 13 additions and 6 deletions

View file

@ -198,11 +198,13 @@ class TaskQueueManager:
'hostvars',
callable=lambda: hostvars,
# FIXME: this is the list of exposed methods to the DictProxy object, plus our
# one special one (set_variable_manager). There's probably a better way
# special ones (set_variable_manager/set_inventory). There's probably a better way
# to do this with a proper BaseProxy/DictProxy derivative
exposed=('set_variable_manager', '__contains__', '__delitem__', '__getitem__',
'__len__', '__setitem__', 'clear', 'copy', 'get', 'has_key', 'items',
'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'),
exposed=(
'set_variable_manager', 'set_inventory', '__contains__', '__delitem__',
'__getitem__', '__len__', '__setitem__', 'clear', 'copy', 'get', 'has_key',
'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'
),
)
self._hostvars_manager = HostVarsManager()
self._hostvars_manager.start()

View file

@ -246,11 +246,13 @@ class StrategyBase:
new_host_info = result_item.get('add_host', dict())
self._add_host(new_host_info, iterator)
self._tqm._hostvars_manager.hostvars().set_inventory(self._inventory)
elif result[0] == 'add_group':
host = result[1]
result_item = result[2]
self._add_group(host, result_item)
self._tqm._hostvars_manager.hostvars().set_inventory(self._inventory)
elif result[0] == 'notify_handler':
task_result = result[1]

View file

@ -119,7 +119,7 @@ class VariableManager:
self._host_vars_files = data.get('host_vars_files', defaultdict(dict))
self._group_vars_files = data.get('group_vars_files', defaultdict(dict))
self._omit_token = data.get('omit_token', '__omit_place_holder__%s' % sha1(os.urandom(64)).hexdigest())
self._inventory = None
self._inventory = data.get('inventory', None)
def _get_cache_entry(self, play=None, host=None, task=None):
play_id = "NONE"

View file

@ -57,13 +57,16 @@ class HostVars(collections.Mapping):
def set_variable_manager(self, variable_manager):
self._variable_manager = variable_manager
def set_inventory(self, inventory):
self._inventory = inventory
def _find_host(self, host_name):
return self._inventory.get_host(host_name)
def __getitem__(self, host_name):
host = self._find_host(host_name)
if host is None:
return j2undefined
raise j2undefined
data = self._variable_manager.get_vars(loader=self._loader, host=host, play=self._play, include_hostvars=False)