Fix HostVars to support containment tests

This commit is contained in:
Toshio Kuratomi 2015-07-31 12:47:54 -07:00
parent 8e164eb46b
commit 3939348286

View file

@ -19,13 +19,16 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import collections
from jinja2 import Undefined as j2undefined from jinja2 import Undefined as j2undefined
from ansible.template import Templar from ansible.template import Templar
__all__ = ['HostVars'] __all__ = ['HostVars']
class HostVars(dict): # Note -- this is a Mapping, not a MutableMapping
class HostVars(collections.Mapping):
''' A special view of vars_cache that adds values from the inventory when needed. ''' ''' A special view of vars_cache that adds values from the inventory when needed. '''
def __init__(self, vars_manager, play, inventory, loader): def __init__(self, vars_manager, play, inventory, loader):
@ -46,3 +49,13 @@ class HostVars(dict):
self._lookup[host_name] = templar.template(result, fail_on_undefined=False) self._lookup[host_name] = templar.template(result, fail_on_undefined=False)
return self._lookup[host_name] return self._lookup[host_name]
def __contains__(self, host_name):
item = self.get(host_name)
if item and item is not j2undefined:
return True
return False
def __iter__(self):
raise NotImplementedError('HostVars does not support iteration as hosts are discovered on an as needed basis.')
def __len__(self):
raise NotImplementedError('HostVars does not support len. hosts entries are discovered dynamically as needed')