From ee3dfef016e1f863b1392b50dab190d256d383ee Mon Sep 17 00:00:00 2001 From: Yann Rouillard Date: Wed, 3 Oct 2018 23:06:44 +0800 Subject: [PATCH] Only delete key from redis in-memory cache if present (#35126) Fixes #35120 : the redis cache plugin keeps key/value entries in an in-memory cache to avoid hitting the redis database each time. The problem is that a cache entry is only set when a value is get or set but it is always deleted when trying to delete a value. When the --flush-cache ansible-playbook option is used, the redis cache plugin is first asked to remove every entry corresponding to every hostname present in the inventory. As no value as been set/get so far, it then tries to delete an unexisting value from the cache and hence crashes with a KeyError exception. --- lib/ansible/plugins/cache/redis.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ansible/plugins/cache/redis.py b/lib/ansible/plugins/cache/redis.py index 230a9177cb1..a8edaa6a046 100644 --- a/lib/ansible/plugins/cache/redis.py +++ b/lib/ansible/plugins/cache/redis.py @@ -116,7 +116,8 @@ class CacheModule(BaseCacheModule): return (self._db.zrank(self._keys_set, key) is not None) def delete(self, key): - del self._cache[key] + if key in self._cache: + del self._cache[key] self._db.delete(self._make_key(key)) self._db.zrem(self._keys_set, key)