fix cache 'update' method to be 'mapping' compatible
- also simplify the update functions - fix methods and allwow backwards compat with plugins overriding 'update'
This commit is contained in:
parent
9973bea504
commit
68301f890a
3 changed files with 27 additions and 18 deletions
2
changelogs/fragments/vm_fix.yml
Normal file
2
changelogs/fragments/vm_fix.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- fix issue with incorrect dict update in vars manager
|
18
lib/ansible/plugins/cache/__init__.py
vendored
18
lib/ansible/plugins/cache/__init__.py
vendored
|
@ -292,10 +292,18 @@ class FactCache(MutableMapping):
|
||||||
""" Flush the fact cache of all keys. """
|
""" Flush the fact cache of all keys. """
|
||||||
self._plugin.flush()
|
self._plugin.flush()
|
||||||
|
|
||||||
def update(self, key, value):
|
def update(self, host_facts):
|
||||||
host_cache = self._plugin.get(key)
|
""" We override the normal update to ensure we always use the 'setter' method """
|
||||||
host_cache.update(value)
|
for key in host_facts:
|
||||||
self._plugin.set(key, host_cache)
|
try:
|
||||||
|
host_cache = self._plugin.get(key)
|
||||||
|
if host_cache:
|
||||||
|
host_cache.update(host_facts[key])
|
||||||
|
else:
|
||||||
|
host_cache = host_facts[key]
|
||||||
|
self._plugin.set(key, host_cache)
|
||||||
|
except KeyError:
|
||||||
|
self._plugin.set(key, host_facts[key])
|
||||||
|
|
||||||
|
|
||||||
class InventoryFileCacheModule(BaseFileCacheModule):
|
class InventoryFileCacheModule(BaseFileCacheModule):
|
||||||
|
@ -314,7 +322,7 @@ class InventoryFileCacheModule(BaseFileCacheModule):
|
||||||
def validate_cache_connection(self):
|
def validate_cache_connection(self):
|
||||||
try:
|
try:
|
||||||
super(InventoryFileCacheModule, self).validate_cache_connection()
|
super(InventoryFileCacheModule, self).validate_cache_connection()
|
||||||
except AnsibleError as e:
|
except AnsibleError:
|
||||||
cache_connection_set = False
|
cache_connection_set = False
|
||||||
else:
|
else:
|
||||||
cache_connection_set = True
|
cache_connection_set = True
|
||||||
|
|
|
@ -625,8 +625,7 @@ class VariableManager:
|
||||||
'''
|
'''
|
||||||
Clears the facts for a host
|
Clears the facts for a host
|
||||||
'''
|
'''
|
||||||
if hostname in self._fact_cache:
|
self._fact_cache.pop(hostname, None)
|
||||||
del self._fact_cache[hostname]
|
|
||||||
|
|
||||||
def set_host_facts(self, host, facts):
|
def set_host_facts(self, host, facts):
|
||||||
'''
|
'''
|
||||||
|
@ -636,13 +635,16 @@ class VariableManager:
|
||||||
if not isinstance(facts, dict):
|
if not isinstance(facts, dict):
|
||||||
raise AnsibleAssertionError("the type of 'facts' to set for host_facts should be a dict but is a %s" % type(facts))
|
raise AnsibleAssertionError("the type of 'facts' to set for host_facts should be a dict but is a %s" % type(facts))
|
||||||
|
|
||||||
if host.name not in self._fact_cache:
|
try:
|
||||||
self._fact_cache[host.name] = facts
|
|
||||||
else:
|
|
||||||
try:
|
try:
|
||||||
|
# this is a cache plugin, not a dictionary
|
||||||
|
self._fact_cache.update({host.name: facts})
|
||||||
|
except TypeError:
|
||||||
|
# this is here for backwards compatibilty for the time cache plugins were not 'dict compatible'
|
||||||
self._fact_cache.update(host.name, facts)
|
self._fact_cache.update(host.name, facts)
|
||||||
except KeyError:
|
display.deprecated("Your configured fact cache plugin is using a deprecated form of the 'update' method", version="2.12")
|
||||||
self._fact_cache[host.name] = facts
|
except KeyError:
|
||||||
|
self._fact_cache[host.name] = facts
|
||||||
|
|
||||||
def set_nonpersistent_facts(self, host, facts):
|
def set_nonpersistent_facts(self, host, facts):
|
||||||
'''
|
'''
|
||||||
|
@ -652,13 +654,10 @@ class VariableManager:
|
||||||
if not isinstance(facts, dict):
|
if not isinstance(facts, dict):
|
||||||
raise AnsibleAssertionError("the type of 'facts' to set for nonpersistent_facts should be a dict but is a %s" % type(facts))
|
raise AnsibleAssertionError("the type of 'facts' to set for nonpersistent_facts should be a dict but is a %s" % type(facts))
|
||||||
|
|
||||||
if host.name not in self._nonpersistent_fact_cache:
|
try:
|
||||||
|
self._nonpersistent_fact_cache[host.name].update(facts)
|
||||||
|
except KeyError:
|
||||||
self._nonpersistent_fact_cache[host.name] = facts
|
self._nonpersistent_fact_cache[host.name] = facts
|
||||||
else:
|
|
||||||
try:
|
|
||||||
self._nonpersistent_fact_cache[host.name].update(facts)
|
|
||||||
except KeyError:
|
|
||||||
self._nonpersistent_fact_cache[host.name] = facts
|
|
||||||
|
|
||||||
def set_host_variable(self, host, varname, value):
|
def set_host_variable(self, host, varname, value):
|
||||||
'''
|
'''
|
||||||
|
|
Loading…
Reference in a new issue