Repository: ansible/ansible

Issue:
ansible crashes if cwd has been removed
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

core
##### ANSIBLE VERSION

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

```
ansible 2.2.0
  config file =
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

None
##### OS / ENVIRONMENT

N/A
##### SUMMARY

os.getcwd raises OSError if the current working directory no longer exists. Ansible checks the cwd for an optional ansible.cfg file. Currently ansible does not catch the OSError and instead crashes. I think the correct behavior, since the cwd config file is optional, would be to ignore this error.
##### STEPS TO REPRODUCE

```
virtualenv venv
venv/bin/pip install ansible
mkdir foo
cd foo
rmdir $(pwd)
../venv/bin/ansible --version
```
##### EXPECTED RESULTS

```
ansible 2.2.0
  config file =
  configured module search path = Default w/o overrides
```
##### ACTUAL RESULTS

```
Traceback (most recent call last):
  File "../venv/bin/ansible", line 44, in <module>
    import ansible.constants as C
  File "/Users/dpowers/Code/ansible/venv/lib/python2.7/site-packages/ansible/constants.py", line 128, in <module>
    p, CONFIG_FILE = load_config_file()
  File "/Users/dpowers/Code/ansible/venv/lib/python2.7/site-packages/ansible/constants.py", line 115, in load_config_file
    path1 = os.getcwd() + "/ansible.cfg"
OSError: [Errno 2] No such file or directory
```

ansible crashes if cwd has been removed
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

core
##### ANSIBLE VERSION

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

```
ansible 2.2.0
  config file =
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

None
##### OS / ENVIRONMENT

N/A
##### SUMMARY

os.getcwd raises OSError if the current working directory no longer exists. Ansible checks the cwd for an optional ansible.cfg file. Currently ansible does not catch the OSError and instead crashes. I think the correct behavior, since the cwd config file is optional, would be to ignore this error.
##### STEPS TO REPRODUCE

```
virtualenv venv
venv/bin/pip install ansible
mkdir foo
cd foo
rmdir $(pwd)
../venv/bin/ansible --version
```
##### EXPECTED RESULTS

```
ansible 2.2.0
  config file =
  configured module search path = Default w/o overrides
```
##### ACTUAL RESULTS

```
Traceback (most recent call last):
  File "../venv/bin/ansible", line 44, in <module>
    import ansible.constants as C
  File "/Users/dpowers/Code/ansible/venv/lib/python2.7/site-packages/ansible/constants.py", line 128, in <module>
    p, CONFIG_FILE = load_config_file()
  File "/Users/dpowers/Code/ansible/venv/lib/python2.7/site-packages/ansible/constants.py", line 115, in load_config_file
    path1 = os.getcwd() + "/ansible.cfg"
OSError: [Errno 2] No such file or directory
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py
--- a/lib/ansible/constants.py
+++ b/lib/ansible/constants.py
@@ -112,7 +112,10 @@ def load_config_file():
         path0 = os.path.expanduser(path0)
         if os.path.isdir(path0):
             path0 += "/ansible.cfg"
-    path1 = os.getcwd() + "/ansible.cfg"
+    try:
+        path1 = os.getcwd() + "/ansible.cfg"
+    except OSError:
+        path1 = None
     path2 = os.path.expanduser("~/.ansible.cfg")
     path3 = "/etc/ansible/ansible.cfg"
 


Test patch: