-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ansible.builtin.uri doesn't return json
dict when python interpreter is python2.
#79592
Comments
Files identified in the description: If these files are incorrect, please update the |
json
dict when python interpreter is python2.json
dict when python interpreter is python2.
-label affects_2.11 |
Ultimately this is caused by #76386 Oh they joys of supporting all the idiosyncrasies of Py2 and Py3. Py3: We need to check if I'm thinking we probably need to create a helper called something like diff --git a/lib/ansible/modules/uri.py b/lib/ansible/modules/uri.py
index c6b592d940..915f5c5671 100644
--- a/lib/ansible/modules/uri.py
+++ b/lib/ansible/modules/uri.py
@@ -553,6 +553,16 @@ def form_urlencoded(body):
return body
+def has_headers(r):
+ if PY3 and r and r.fp:
+ # r may be None for some errors
+ # r.fp may be None depending on the error, which means there are no headers either
+ return True
+ if PY2 and r and hasattr(r, 'headers'):
+ # r.headers may be missing on certain exceptions
+ return True
+ return False
+
+
def uri(module, url, dest, body, body_format, method, headers, socket_timeout, ca_path, unredirected_headers, decompress,
ciphers, use_netrc):
# is dest is set and is a directory, let's check if we get redirected and
@@ -690,9 +700,7 @@ def main():
filename = get_response_filename(r) or 'index.html'
dest = os.path.join(dest, filename)
- if r and r.fp is not None:
- # r may be None for some errors
- # r.fp may be None depending on the error, which means there are no headers either
+ if has_headers(r):
content_type, main_type, sub_type, content_encoding = parse_content_type(r)
else:
content_type = 'application/octet-stream' This will require more extensive testing to ensure it doesn't regress the previously mentioned fix, and resolves the issues. Integration tests will need added as well. As far as integration tests are concerned, afaik httpbin doesn't have a means of returning a JSON body along with a status code. I created a simple flask app for a reproducer: Flask App...
from flask import Flask
app = Flask(__name__)
@app.route('/<int:code>')
def status(code):
return '{"hello": "world"}', code, {'Content-Type': 'application/json'}
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0') I'm not pursuing this further, as I'm about to be AFK for several weeks. Also, the |
Yeah, I would love to get rid of python2 from my environment 😞 I've implemented fixes in my ansible roles to work around this via setting Mainly wanted to raise this issue as no one else had and it took me a good chunk of my day to figure out why some CI jobs were failing with the updated versions. I also created a simple flask app with molecule testing/tasks https://github.com/stenh0use/ansible-uri-issue.git, although your flask app is neater than mine ;) As for the labels, I didn't read the bot help properly, turns out they don't work for everyone 😄 |
json
dict when python interpreter is python2.json
dict when python interpreter is python2.
Summary
When upgrading some roles from
ansible 2.11.1
toansible 2.13.7
I discovered a regression in the behavior of theansible.builtin.uri
module.Requirements for the regression to appear:
ansible 2.13+
withpython3
python2
as the default python bin/interpreterContent-type: application/json
and the content is a validjson
string, but the response code is not200
The expected return values in ansible should include the key in the dictionary
json
with the content loaded. When the above requirements are met this does not happen.The module documentation states the following about
return_content
key.Issue Type
Bug Report
Component Name
ansible.builtin.uri
Ansible Version
Configuration
see molecule / example role.
OS / Environment
Centos7 / Python2 interpreter
Steps to Reproduce
Run the below and see that the bug does not exist in 2.11 when the target host has python2 running
Run the below and then see that the bug does exist once upgrading to 2.13+ and the target host has python2 running
Expected Results
I expect the json dict key to be populated even when the response code is not 200, but is in the expected list as per the documentation.
Actual Results
Code of Conduct
The text was updated successfully, but these errors were encountered: