Repository: ansible/ansible

Issue:
Inventory script errors not checked or exposed
##### Issue Type:

Bug report / Feature idea
##### Ansible Version:

```
% ansible --version
ansible 1.8.2
  configured module search path = None
```
##### Environment:

Running from OS X, against Ubuntu Trusty.
##### Summary:

If using a dynamic inventory script that fails, ansible does not fail how I'd expect, including checking exit status and relaying output (at least stderr).
##### Steps To Reproduce:
1. Have an inventory script that fails (exits non-zero)
2. Use it: `ansible-playbook site.yml -i /path/to/script`
3. See ansible blow up still trying to parse JSON in the output
##### Expected Results:

I would expect ansible to check the script's exit status around [here](https://github.com/ansible/ansible/blob/1b8faa68d99f2f1f373df4ce43e6c2ff71d81257/lib/ansible/inventory/script.py#L45), and ideally relay at least stderr to me.

Example:

```
% ansible-playbook site.yml -i /path/to/script
ERROR: running inventory script /path/to/script failed, stderr below:

Something's not configured
```
##### Actual Results:

```
% ansible-playbook site.yml -i /path/to/script
Traceback (most recent call last):
  File "/usr/local/Cellar/ansible/1.8.2/libexec/bin/ansible-playbook", line 324, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/usr/local/Cellar/ansible/1.8.2/libexec/bin/ansible-playbook", line 160, in main
    inventory = ansible.inventory.Inventory(options.inventory, vault_password=vault_pass)
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/inventory/__init__.py", line 118, in __init__
    self.parser = InventoryScript(filename=host_list)
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/inventory/script.py", line 49, in __init__
    self.groups = self._parse(stderr)
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/inventory/script.py", line 57, in _parse
    self.raw  = utils.parse_json(self.data)
  File "/usr/local/Cellar/ansible/1.8.2/libexec/lib/python2.7/site-packages/ansible/utils/__init__.py", line 551, in parse_json
    results = json.loads(data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/inventory/script.py b/lib/ansible/inventory/script.py
--- a/lib/ansible/inventory/script.py
+++ b/lib/ansible/inventory/script.py
@@ -43,6 +43,10 @@ def __init__(self, filename=C.DEFAULT_HOST_LIST):
         except OSError, e:
             raise errors.AnsibleError("problem running %s (%s)" % (' '.join(cmd), e))
         (stdout, stderr) = sp.communicate()
+
+        if sp.returncode != 0:
+            raise errors.AnsibleError("Inventory script (%s) had an execution error: %s " % (filename,stderr))
+
         self.data = stdout
         # see comment about _meta below
         self.host_vars_from_top = None
diff --git a/v2/ansible/inventory/script.py b/v2/ansible/inventory/script.py
--- a/v2/ansible/inventory/script.py
+++ b/v2/ansible/inventory/script.py
@@ -43,6 +43,10 @@ def __init__(self, filename=C.DEFAULT_HOST_LIST):
         except OSError, e:
             raise errors.AnsibleError("problem running %s (%s)" % (' '.join(cmd), e))
         (stdout, stderr) = sp.communicate()
+
+        if sp.returncode != 0:
+            raise errors.AnsibleError("Inventory script (%s) had an execution error: %s " % (filename,stderr))
+
         self.data = stdout
         # see comment about _meta below
         self.host_vars_from_top = None


Test patch: