Repository: ansible/ansible

Issue:
Redis facts cache error with --flush-cache
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
plugin/cache/redis

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]
```


##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"

Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).

-->
```
root@ansible:/etc/ansible# ansible-config dump --only-changed
CACHE_PLUGIN(/etc/ansible/ansible.cfg) = redis
CACHE_PLUGIN_TIMEOUT(/etc/ansible/ansible.cfg) = 86400
DEFAULT_BECOME(/etc/ansible/ansible.cfg) = True
DEFAULT_GATHERING(/etc/ansible/ansible.cfg) = smart
DEFAULT_REMOTE_USER(/etc/ansible/ansible.cfg) = debian
PERSISTENT_CONNECT_TIMEOUT(/etc/ansible/ansible.cfg) = 30
RETRY_FILES_ENABLED(/etc/ansible/ansible.cfg) = False
RETRY_FILES_SAVE_PATH(/etc/ansible/ansible.cfg) = /root/.ansible-retry
```

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Running on Debian Jessie :
```
lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 8.9 (jessie)
Release:	8.9
Codename:	jessie
```
pip installed python-redis:
redis==2.10.5

##### SUMMARY
<!--- Explain the problem briefly -->

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

Run ansible playbook with flush cache:
```
root@ansible:/etc/ansible# touch empty_playbook.yml
root@ansible:/etc/ansible# ansible-playbook --flush-cache -i environments/cadastre-am/hosts empty_playbook.yml 
ERROR! Unexpected Exception, this is probably a bug: 'CacheModule' object has no attribute 'cache'
to see the full traceback, use -vvv
```

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
```
I used an empty playbook for testing but also fails with a true one.
<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS

```
root@ansible:/etc/ansible# ansible-playbook -i environments/cadastre-am/hosts empty_playbook.yml 
ERROR! playbooks must be a list of plays
```
Or the playbook should go on.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
After upgrading from 2.3.x to 2.4, I runned my usual playbook, 
The play crasshed with this message

<!--- Paste verbatim command output between quotes below -->
```
root@ansible:/etc/ansible# ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -i environments/cadastre-am/ cadastre-am.yml --flush-cache --diff -vvv
ansible-playbook 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]
Using /etc/ansible/ansible.cfg as config file
Parsed /etc/ansible/environments/cadastre-am/hosts inventory source with ini plugin
ERROR! Unexpected Exception, this is probably a bug: 'CacheModule' object has no attribute 'cache'
the full traceback was:

Traceback (most recent call last):
  File "/usr/bin/ansible-playbook", line 106, in <module>
    exit_code = cli.run()
  File "/usr/lib/python2.7/dist-packages/ansible/cli/playbook.py", line 124, in run
    self._flush_cache(inventory, variable_manager)
  File "/usr/lib/python2.7/dist-packages/ansible/cli/playbook.py", line 204, in _flush_cache
    variable_manager.clear_facts(hostname)
  File "/usr/lib/python2.7/dist-packages/ansible/vars/manager.py", line 590, in clear_facts
    del self._fact_cache[hostname]
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/cache/__init__.py", line 260, in __delitem__
    self._plugin.delete(key)
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/cache/redis.py", line 119, in delete
    del self.cache[key]
AttributeError: 'CacheModule' object has no attribute 'cache'
```

I tried to fix the line my replacing self.cache with self._cache as mentionned in the cache module, I got another error:
```
root@ansible:/etc/ansible# ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook -i environments/cadastre-am/ cadastre-am.yml --flush-cache --diff -vvv
ansible-playbook 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]
Using /etc/ansible/ansible.cfg as config file
Parsed /etc/ansible/environments/cadastre-am/hosts inventory source with ini plugin
ERROR! Unexpected Exception, this is probably a bug: u'app'
the full traceback was:

Traceback (most recent call last):
  File "/usr/bin/ansible-playbook", line 106, in <module>
    exit_code = cli.run()
  File "/usr/lib/python2.7/dist-packages/ansible/cli/playbook.py", line 124, in run
    self._flush_cache(inventory, variable_manager)
  File "/usr/lib/python2.7/dist-packages/ansible/cli/playbook.py", line 204, in _flush_cache
    variable_manager.clear_facts(hostname)
  File "/usr/lib/python2.7/dist-packages/ansible/vars/manager.py", line 590, in clear_facts
    del self._fact_cache[hostname]
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/cache/__init__.py", line 260, in __delitem__
    self._plugin.delete(key)
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/cache/redis.py", line 119, in delete
    del self._cache[key]
KeyError: u'app'
```

So it might be much more broken :cry:


Discussion:
Oh, and the playbooks run if I remove the "--flush-cache" option.
edit: probably busted beyond this as well

To get redis caching working, the 'fact_caching_connection' config var also needs to be provided.

Something like:

```
fact_caching = redis
fact_caching_timeout = 3600
fact_caching_connection = localhost:6379:0
```

That should get it working, but the traceback/crash when it is missing is definitely a bug though.

And it looks like some of the docs are wrong as well (the redis section of http://docs.ansible.com/ansible/latest/playbooks_variables.html#fact-caching doesn't include the connection info in it's example) so that is a docs bug as well. 
hmm, and agreed, it looks like it is busted beyond the missing config. The plugin code docs say that option is required, but the code itself has a default value so not sure what is acceptable...
yup, its busted. Bogus ref to self.cache instead of self._cache

``` diff
diff --git a/lib/ansible/plugins/cache/redis.py b/lib/ansible/plugins/cache/redis.py
index 831ff03..893e1e2 100644
--- a/lib/ansible/plugins/cache/redis.py
+++ b/lib/ansible/plugins/cache/redis.py
@@ -116,7 +116,7 @@ class CacheModule(BaseCacheModule):
         return (self._db.zrank(self._keys_set, key) is not None)
 
     def delete(self, key):
-        del self.cache[key]
+        del self._cache[key]
         self._db.delete(self._make_key(key))
         self._db.zrem(self._keys_set, key)
```


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,7 @@ def contains(self, key):
         return (self._db.zrank(self._keys_set, key) is not None)
 
     def delete(self, key):
-        del self.cache[key]
+        del self._cache[key]
         self._db.delete(self._make_key(key))
         self._db.zrem(self._keys_set, key)
 


Test patch: