Repository: ansible/ansible

Issue:
uri module return value's .json attribute fails silently when content fails to parse
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
When registering the return value from the uri module, and accessing the .json attribute later, if the content in the response can't be parsed (JSON syntax errors, etc), the .json attribute silently returns an empty string instead of raising an error. This causes seemingly unrelated and often very difficult to track down errors later on in the playbook.

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
uri

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.3
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/creeadmin/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, 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).-->
ansible-config dump --only-changed
ANSIBLE_PIPELINING(/usr/lib/cree/ansible/ansible.cfg) = True
DEFAULT_FORKS(/usr/lib/cree/ansible/ansible.cfg) = 20
DEFAULT_VAULT_PASSWORD_FILE(/usr/lib/cree/ansible/ansible.cfg) = /home/ansible/.
RETRY_FILES_ENABLED(/usr/lib/cree/ansible/ansible.cfg) = False

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, 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.-->
N/A

##### 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. -->
With the following tasks below. If the totpServer returns mal-formed JSON content (in my case, it's a custom Flask server), the "pass" environment variable isn't set and hash-pw.py fails on a key error when trying to read os.environ["pass"]. Seems like ansible should throw an error when trying to access the .json attribute instead of just returning nothing silently.


<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: "Get generated passwords from deployment server"
  uri:
    url: "{{ totpServer }}"
    method: "POST"
    body:
      serial: "{{ ansible_local.system_serial_number }}"
    body_format: "json"
    headers:
      Content-Type: "application/json"
    return_content: "yes"
    timeout: 300
  register: "passwords"

# Several tasks later....

- name: "Hashing OS password"
  shell: "{{ APP_HOME }}/ansible/scripts/hash-pw.py"
  environment:
    pass: "{{ passwords.json.osPW }}"
    salt: "{{ tpmsalt.stdout }}"
  register: pwhash
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Seems like trying to access the .json attribute should throw a meaningful error message instead of just returning nothing silently.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Silent failure as described above. I've since fixed the issue so I can't paste a full command output, but it basically just failed the later "Hashing OS password" showing the Python traceback. If it's really necessary, I can try to reproduce it and paste the output, but I'm not sure what would be gleaned from that.

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
Files identified in the description:
* [lib/ansible/modules/net_tools/basics/uri.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/net_tools/basics/uri.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @romeotheriault
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I don't think this is really related to the `uri` module specifically.  If you were to have used the variable differently, you would have gotten an error indicating something like:

```
The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'json'
```

It seems we might have a problem with `environment`, in that `environment` silently ignored the undefined error.

Resolving patch:
diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py
--- a/lib/ansible/playbook/task.py
+++ b/lib/ansible/playbook/task.py
@@ -280,7 +280,8 @@ def _parse_env_kv(k, v):
                 except AnsibleUndefinedVariable as e:
                     if self.action in ('setup', 'gather_facts') and 'ansible_env' in to_native(e):
                         # ignore as fact gathering sets ansible_env
-                        pass
+                        return
+                    raise
 
             if isinstance(value, list):
                 for env_item in value:


Test patch: