From f5a374103f62a2ab8871fd23ce04514bcadf2316 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 11 Feb 2016 01:09:20 -0500 Subject: [PATCH] fixes for uri module - clarify docs on body_json behaviour - only tranform into json if body input is not a string users keep passing json string and expecint it to not be jsonified again - fixed issue with removes not handling path expansion correctly - switched all path variables to 'type path' to handle expansions --- network/basics/uri.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/network/basics/uri.py b/network/basics/uri.py index 73b2f059f7e..d1fa1b0ee4e 100644 --- a/network/basics/uri.py +++ b/network/basics/uri.py @@ -63,12 +63,12 @@ options: default: null body: description: - - The body of the http request/response to the web service. + - The body of the http request/response to the web service. If C(body_format) is set to 'json' it will take an already formated JSON string or convert a data structure into JSON. required: false default: null body_format: description: - - The serialization format of the body. When set to json, encodes the body argument and automatically sets the Content-Type header accordingly. + - The serialization format of the body. When set to json, encodes the body argument, if needed, and automatically sets the Content-Type header accordingly. required: false choices: [ "raw", "json" ] default: raw @@ -369,7 +369,7 @@ def main(): module = AnsibleModule( argument_spec = dict( url = dict(required=True), - dest = dict(required=False, default=None), + dest = dict(required=False, default=None, type='path'), user = dict(required=False, default=None), password = dict(required=False, default=None), body = dict(required=False, default=None), @@ -378,8 +378,8 @@ def main(): return_content = dict(required=False, default='no', type='bool'), force_basic_auth = dict(required=False, default='no', type='bool'), follow_redirects = dict(required=False, default='safe', choices=['all', 'safe', 'none', 'yes', 'no']), - creates = dict(required=False, default=None), - removes = dict(required=False, default=None), + creates = dict(required=False, default=None, type='path'), + removes = dict(required=False, default=None, type='path'), status_code = dict(required=False, default=[200], type='list'), timeout = dict(required=False, default=30, type='int'), validate_certs = dict(required=False, default=True, type='bool'), @@ -397,7 +397,7 @@ def main(): user = module.params['user'] password = module.params['password'] body = module.params['body'] - body_format = module.params['body_format'] + body_format = module.params['body_format'].lower() method = module.params['method'] dest = module.params['dest'] return_content = module.params['return_content'] @@ -411,9 +411,10 @@ def main(): dict_headers = {} - # If body_format is json, encodes the body (wich can be a dict or a list) and automatically sets the Content-Type header if body_format == 'json': - body = json.dumps(body) + # Encode the body unless its a string, then assume it is preformatted JSON + if not isinstance(body, basestring): + body = json.dumps(body) dict_headers['Content-Type'] = 'application/json' @@ -428,7 +429,6 @@ def main(): # do not run the command if the line contains creates=filename # and the filename already exists. This allows idempotence # of uri executions. - creates = os.path.expanduser(creates) if os.path.exists(creates): module.exit_json(stdout="skipped, since %s exists" % creates, changed=False, stderr=False, rc=0) @@ -436,7 +436,6 @@ def main(): # do not run the command if the line contains removes=filename # and the filename do not exists. This allows idempotence # of uri executions. - v = os.path.expanduser(removes) if not os.path.exists(removes): module.exit_json(stdout="skipped, since %s does not exist" % removes, changed=False, stderr=False, rc=0)