AnsibleError: Unable to create local directories

I’m trying to integrate my flask application (which uses the ansible modules for python) and I keep running into this problem. It looks like ansible, upon import, tries to create some temporary directory, but the path that it tries to write to is invalid.I have attached a copy of my Apache Log, where you can see the error at the bottom of the log. It seems that something is wrong with the ansible.utils.path.unfrackpath() function, which is supposed to remove all environment variables and such from a given path. As you can see, the path keeps coming up as ‘/$HOME/.ansible/tmp’ instead of the value of the $HOME environment variable. If I replace the b_rpath variable inside the makedirs_safe() function of ansible.utils.path with an absolute path to a directory (/home/ubuntu/.ansible/tmp), it seems to work just fine. does anyone have any insight on this issue? any help would be appreciated, as obviously manually overriding a function would not be an acceptable solution for deployment, obviously. Thanks in advance!

Apache Log: http://pastebin.com/QnftziTN

`

[Thu Dec 22 21:01:30.319866 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx] Traceback (most recent call last):
[Thu Dec 22 21:01:30.319879 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/var/www/html/ntc-server/ntc-server.wsgi", line 3, in <module>
[Thu Dec 22 21:01:30.319917 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     from application.api import app as application
[Thu Dec 22 21:01:30.319927 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/usr/local/lib/python2.7/dist-packages/dev_tools-0.1.0-py2.7.egg/application/api.py", line 23, in <module>
[Thu Dec 22 21:01:30.320038 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     import run_playbook
[Thu Dec 22 21:01:30.320046 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/usr/local/lib/python2.7/dist-packages/dev_tools-0.1.0-py2.7.egg/application/run_playbook.py", line 5, in <module>
[Thu Dec 22 21:01:30.320087 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     from ansible.parsing.dataloader import DataLoader
[Thu Dec 22 21:01:30.320095 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/usr/local/lib/python2.7/dist-packages/ansible-2.2.0.0-py2.7.egg/ansible/parsing/dataloader.py", line 34, in <module>
[Thu Dec 22 21:01:30.320190 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     from ansible.parsing.vault import VaultLib, is_encrypted, is_encrypted_file
[Thu Dec 22 21:01:30.320198 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/usr/local/lib/python2.7/dist-packages/ansible-2.2.0.0-py2.7.egg/ansible/parsing/vault/__init__.py", line 73, in <module>
[Thu Dec 22 21:01:30.320360 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     from ansible.utils.display import Display
[Thu Dec 22 21:01:30.320367 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/usr/local/lib/python2.7/dist-packages/ansible-2.2.0.0-py2.7.egg/ansible/utils/display.py", line 35, in <module>
[Thu Dec 22 21:01:30.320458 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     from ansible import constants as C
[Thu Dec 22 21:01:30.320466 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/usr/local/lib/python2.7/dist-packages/ansible-2.2.0.0-py2.7.egg/ansible/constants.py", line 161, in <module>
[Thu Dec 22 21:01:30.320577 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     DEFAULT_LOCAL_TMP         = get_config(p, DEFAULTS, 'local_tmp',        'ANSIBLE_LOCAL_TEMP',      '$HOME/.ansible/tmp', istmppath=True)
[Thu Dec 22 21:01:30.320585 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/usr/local/lib/python2.7/dist-packages/ansible-2.2.0.0-py2.7.egg/ansible/constants.py", line 81, in get_config
[Thu Dec 22 21:01:30.320596 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     makedirs_safe(value, 0o700)
[Thu Dec 22 21:01:30.320602 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]   File "/usr/local/lib/python2.7/dist-packages/ansible-2.2.0.0-py2.7.egg/ansible/utils/path.py", line 67, in makedirs_safe
[Thu Dec 22 21:01:30.320635 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx]     raise AnsibleError("Unable to create local directories(%s): %s" % (to_native(rpath), to_native(e)))
[Thu Dec 22 21:01:30.320652 2016] [:error] [pid 16193:tid 139812161828608] [client 204.xxx.xx.x:58xxx] AnsibleError: Unable to create local directories(/$HOME/.ansible/tmp): [Errno 13] Permission denied: '/$HOME'

`

This happens as part of reading and setting up the configuration that ansible will execute with.

In particular this is caused by the local temp path, which is by default defined as $HOME/.ansible/tmp in your version of ansible. In devel this has been changed to ~/.ansible/tmp, but in any case, Ansible is trying to ensure this path exists.

You could change the location of this path, by either setting an environment variable called ANSIBLE_LOCAL_TEMP, or by setting local_tmp in an ansible.cfg file under [defaults].

Matt,

Your suggestion solved the ‘$HOME’ issue, however my last log line still reads a permissions error: AnsibleError: Unable to create local directories(/home/ubuntu/.ansible/tmp): [Errno 13] Permission denied: '/home/ubuntu/.ansible/tmp'. I’ve checked out all my permissions and everything seems ok. It’s odd to me that I am able to run my application with flask just fine, but when I try and integrate to Apache I start running into these problems.

Thanks,
Jordan

`

AnsibleError: Unable to create local directories(/home/ubuntu/.ansible/tmp): [Errno 13] Permission denied: ‘/home/ubuntu/.ansible/tmp’

`

I’ll take a guess that when you run via apache it is now being run by the www-data or similar user instead of as ubuntu. This is likely causing the permissions issue.

Maybe use a directory like /tmp instead of /home/ubuntu

You were totally right, that was the case. I can’t believe I didn’t even think that Apache would be using a different user without the same permissions… Thanks for your help!