Repository: ansible/ansible

Issue:
--flush-cache operation fails with redis database
##### ISSUE TYPE
 - Bug Report
 
##### COMPONENT NAME
 - plugin/cache/redis

##### ANSIBLE VERSION
```
ansible 2.4.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/myuser/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug 29 2016, 10:12:21) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
```

##### CONFIGURATION
<!---
ALLOW_WORLD_READABLE_TMPFILES(/etc/ansible/ansible.cfg) = True
ANSIBLE_FORCE_COLOR(/etc/ansible/ansible.cfg) = True
ANSIBLE_NOCOLOR(/etc/ansible/ansible.cfg) = False
ANSIBLE_NOCOWS(/etc/ansible/ansible.cfg) = True
ANSIBLE_PIPELINING(/etc/ansible/ansible.cfg) = True
ANSIBLE_SSH_ARGS(/etc/ansible/ansible.cfg) = -C -o ControlMaster=auto -o ControlPersist=240s
ANSIBLE_SSH_CONTROL_PATH_DIR(/etc/ansible/ansible.cfg) = ~/.ansible/cp
CACHE_PLUGIN(/etc/ansible/ansible.cfg) = redis
CACHE_PLUGIN_CONNECTION(/etc/ansible/ansible.cfg) = localhost:6379:0
CACHE_PLUGIN_TIMEOUT(/etc/ansible/ansible.cfg) = 10000
COMMAND_WARNINGS(/etc/ansible/ansible.cfg) = False
DEFAULT_ALLOW_UNSAFE_LOOKUPS(/etc/ansible/ansible.cfg) = True
DEFAULT_BECOME(/etc/ansible/ansible.cfg) = True
DEFAULT_BECOME_METHOD(/etc/ansible/ansible.cfg) = sudo
DEFAULT_BECOME_USER(/etc/ansible/ansible.cfg) = root
DEFAULT_CALLBACK_WHITELIST(/etc/ansible/ansible.cfg) = ['timer', 'skippy', 'profile_roles', 'junit']
DEFAULT_EXECUTABLE(/etc/ansible/ansible.cfg) = /bin/sh
DEFAULT_FORCE_HANDLERS(/etc/ansible/ansible.cfg) = True
DEFAULT_FORKS(/etc/ansible/ansible.cfg) = 15
DEFAULT_GATHERING(/etc/ansible/ansible.cfg) = smart
DEFAULT_GATHER_SUBSET(/etc/ansible/ansible.cfg) = all
DEFAULT_GATHER_TIMEOUT(/etc/ansible/ansible.cfg) = 60
DEFAULT_HASH_BEHAVIOUR(/etc/ansible/ansible.cfg) = replace
DEFAULT_LOAD_CALLBACK_PLUGINS(/etc/ansible/ansible.cfg) = True
DEFAULT_LOG_PATH(/etc/ansible/ansible.cfg) = /var/log/ansible/ansible.log
DEFAULT_MANAGED_STR(/etc/ansible/ansible.cfg) = Ansible managed: {file} %Y-%m-%d %H:%M:%S
DEFAULT_MODULE_NAME(/etc/ansible/ansible.cfg) = command
DEFAULT_NO_TARGET_SYSLOG(/etc/ansible/ansible.cfg) = True
DEFAULT_PRIVATE_ROLE_VARS(/etc/ansible/ansible.cfg) = True
DEFAULT_SCP_IF_SSH(/etc/ansible/ansible.cfg) = smart
DEFAULT_SFTP_BATCH_MODE(/etc/ansible/ansible.cfg) = False
DEFAULT_SSH_TRANSFER_METHOD(/etc/ansible/ansible.cfg) = smart
DEFAULT_STRATEGY(/etc/ansible/ansible.cfg) = linear
DEFAULT_TIMEOUT(/etc/ansible/ansible.cfg) = 300
DEFAULT_TRANSPORT(/etc/ansible/ansible.cfg) = smart
DEFAULT_UNDEFINED_VAR_BEHAVIOR(/etc/ansible/ansible.cfg) = True
DEPRECATION_WARNINGS(/etc/ansible/ansible.cfg) = False
DISPLAY_ARGS_TO_STDOUT(/etc/ansible/ansible.cfg) = True
DISPLAY_SKIPPED_HOSTS(/etc/ansible/ansible.cfg) = True
ERROR_ON_MISSING_HANDLER(/etc/ansible/ansible.cfg) = True
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False
PARAMIKO_RECORD_HOST_KEYS(/etc/ansible/ansible.cfg) = True
RETRY_FILES_ENABLED(/etc/ansible/ansible.cfg) = False
RETRY_FILES_SAVE_PATH(/etc/ansible/ansible.cfg) = /home/myuser/.ansible-retry
SHOW_CUSTOM_STATS(/etc/ansible/ansible.cfg) = True
SYSTEM_WARNINGS(/etc/ansible/ansible.cfg) = True
-->

##### OS / ENVIRONMENT
Redhat 7.2

##### SUMMARY
When trying to flush-cache with the redis database cache plugin, the command fails with the following error:
```
ERROR! Unexpected Exception, this is probably a bug: u'NAME_OF_ONE_HOST'
```

##### STEPS TO REPRODUCE
With the following inventory and playbook, run the following commands:
- Flush the existing redis cache if needed:
```
redis-cli flushdb
```
- Run the playbook first to gather facts:
```
ansible-playbook -i hosts test-playbook.yml
```
- Try to flush the cache
```
ansible-playbook -i hosts test-playbook.yml --flush-cache
```

The playbook used is:
```yaml
- hosts: all
  tasks:
  - setup:
```
The inventory file used is:
```yaml
[test]
localhost ansible_connection=local
```

##### EXPECTED RESULTS
The cache is flushed and the playbook runs fine.

##### ACTUAL RESULTS
The playbook then fails with the following error:
```
ERROR! Unexpected Exception, this is probably a bug: u'localhost'
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/database/misc/redis.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/database/misc/redis.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @slok
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/plugins/cache/redis.py b/lib/ansible/plugins/cache/redis.py
--- a/lib/ansible/plugins/cache/redis.py
+++ b/lib/ansible/plugins/cache/redis.py
@@ -116,7 +116,8 @@ def contains(self, key):
         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)
 


Test patch: