From d06cd869b8ea98967300ac1f0ff81cd68cd5a04e Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Tue, 4 Dec 2018 11:18:46 -0800 Subject: [PATCH] Revert "fix cache 'update' method to be 'mapping' compatible" This reverts commit 68301f890addfe4ca8d3b57063e2f70fd21474c9. --- changelogs/fragments/vm_fix.yml | 2 -- lib/ansible/plugins/cache/__init__.py | 18 +++++------------- lib/ansible/vars/manager.py | 27 ++++++++++++++------------- 3 files changed, 19 insertions(+), 28 deletions(-) delete mode 100644 changelogs/fragments/vm_fix.yml diff --git a/changelogs/fragments/vm_fix.yml b/changelogs/fragments/vm_fix.yml deleted file mode 100644 index f247cce338..0000000000 --- a/changelogs/fragments/vm_fix.yml +++ /dev/null @@ -1,2 +0,0 @@ -bugfixes: - - fix issue with incorrect dict update in vars manager diff --git a/lib/ansible/plugins/cache/__init__.py b/lib/ansible/plugins/cache/__init__.py index 2b1d51fb5a..cfb5ebce3e 100644 --- a/lib/ansible/plugins/cache/__init__.py +++ b/lib/ansible/plugins/cache/__init__.py @@ -289,18 +289,10 @@ class FactCache(MutableMapping): """ Flush the fact cache of all keys. """ self._plugin.flush() - def update(self, host_facts): - """ We override the normal update to ensure we always use the 'setter' method """ - for key in host_facts: - 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]) + def update(self, key, value): + host_cache = self._plugin.get(key) + host_cache.update(value) + self._plugin.set(key, host_cache) class InventoryFileCacheModule(BaseFileCacheModule): @@ -319,7 +311,7 @@ class InventoryFileCacheModule(BaseFileCacheModule): def validate_cache_connection(self): try: super(InventoryFileCacheModule, self).validate_cache_connection() - except AnsibleError: + except AnsibleError as e: cache_connection_set = False else: cache_connection_set = True diff --git a/lib/ansible/vars/manager.py b/lib/ansible/vars/manager.py index 439f2d8beb..42f823beaa 100644 --- a/lib/ansible/vars/manager.py +++ b/lib/ansible/vars/manager.py @@ -622,7 +622,8 @@ class VariableManager: ''' Clears the facts for a host ''' - self._fact_cache.pop(hostname, None) + if hostname in self._fact_cache: + del self._fact_cache[hostname] def set_host_facts(self, host, facts): ''' @@ -632,16 +633,13 @@ class VariableManager: 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)) - 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) - display.deprecated("Your configured fact cache plugin is using a deprecated form of the 'update' method", version="2.12") - except KeyError: + if host.name not in self._fact_cache: self._fact_cache[host.name] = facts + else: + try: + self._fact_cache.update(host.name, facts) + except KeyError: + self._fact_cache[host.name] = facts def set_nonpersistent_facts(self, host, facts): ''' @@ -651,10 +649,13 @@ class VariableManager: 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)) - try: - self._nonpersistent_fact_cache[host.name].update(facts) - except KeyError: + if host.name not in self._nonpersistent_fact_cache: 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): '''