Repository: ansible/ansible

Issue:
jsonfile cache plugin tracebacks if fact_caching_connection isnt set
<!---
Please do not report issues/requests related to Ansible modules here !!

Report them to the appropriate modules-core or modules-extras project:
  - https://github.com/ansible/ansible-modules-core/issues
  - https://github.com/ansible/ansible-modules-extras/issues

Also verify first that your issue/request is not already reported in GitHub
-->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

<!--- Name of the plugin/task/feature -->

lib/ansible/plugins/cache/jsonfile.py
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
[fedora-23:ansible (jsonfile_no_caching_connection_config %)]$ ansible --version
ansible 2.2.0 (jsonfile_no_caching_connection_config 0bda419f66) last updated 2016/09/14 13:14:17 (GMT -400)
  lib/ansible/modules/core: (detached HEAD ae6992bf8c) last updated 2016/09/14 13:01:17 (GMT -400)
  lib/ansible/modules/extras: (detached HEAD afd0b23836) last updated 2016/09/14 13:01:17 (GMT -400)
  config file = /home/adrian/.ansible.cfg
  configured module search path = ['/home/adrian/src/ansible-modules-core', '/home/adrian/src/ansible-modules-extras']

```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

Set 'fact_caching=jsonfile' without setting fact_caching_connection

```
gathering = smart
fact_caching = jsonfile
```
##### OS / ENVIRONMENT

fedora23
##### SUMMARY

<!--- Explain the problem briefly -->

With config:

```
gathering = smart
fact_caching = jsonfile
```

```
[fedora-23:ansible (cache_timeout_0 %)]$ ANSIBLE_CONFIG=~/ansible/ansible-fact-jsonfile.cfg ansible-playbook -vvv repro/archive_single_file/archive.yml 
Using /home/adrian/ansible/ansible-fact-jsonfile.cfg as config file
ERROR! Unexpected Exception: argument of type 'NoneType' is not iterable
the full traceback was:

Traceback (most recent call last):
  File "/home/adrian/src/ansible/bin/ansible-playbook", line 103, in <module>
    exit_code = cli.run()
  File "/home/adrian/src/ansible/lib/ansible/cli/playbook.py", line 126, in run
    variable_manager = VariableManager()
  File "/home/adrian/src/ansible/lib/ansible/vars/__init__.py", line 97, in __init__
    self._fact_cache = FactCache()
  File "/home/adrian/src/ansible/lib/ansible/plugins/cache/__init__.py", line 35, in __init__
    self._plugin = cache_loader.get(C.CACHE_PLUGIN)
  File "/home/adrian/src/ansible/lib/ansible/plugins/__init__.py", line 358, in get
    obj = obj(*args, **kwargs)
  File "/home/adrian/src/ansible/lib/ansible/plugins/cache/jsonfile.py", line 53, in __init__
    self._cache_dir = os.path.expanduser(os.path.expandvars(C.CACHE_PLUGIN_CONNECTION)) # expects a dir path
  File "/usr/lib64/python2.7/posixpath.py", line 287, in expandvars
    if '$' not in path:
TypeError: argument of type 'NoneType' is not iterable
```
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->

```

```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

No traceback, helpful error message.
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->

```

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/plugins/cache/jsonfile.py b/lib/ansible/plugins/cache/jsonfile.py
--- a/lib/ansible/plugins/cache/jsonfile.py
+++ b/lib/ansible/plugins/cache/jsonfile.py
@@ -50,7 +50,11 @@ def __init__(self, *args, **kwargs):
 
         self._timeout = float(C.CACHE_PLUGIN_TIMEOUT)
         self._cache = {}
-        self._cache_dir = os.path.expanduser(os.path.expandvars(C.CACHE_PLUGIN_CONNECTION))
+        self._cache_dir = None
+
+        if C.CACHE_PLUGIN_CONNECTION:
+            # expects a dir path
+            self._cache_dir = os.path.expanduser(os.path.expandvars(C.CACHE_PLUGIN_CONNECTION))
 
         if not self._cache_dir:
             raise AnsibleError("error, 'jsonfile' cache plugin requires the 'fact_caching_connection' config option to be set (to a writeable directory path)")


Test patch: