@ -51,6 +51,9 @@ except ImportError:
import ansible . module_utils . six . moves . http_cookiejar as cookiejar
import ansible . module_utils . six . moves . http_cookiejar as cookiejar
import ansible . module_utils . six . moves . urllib . request as urllib_request
import ansible . module_utils . six . moves . urllib . request as urllib_request
import ansible . module_utils . six . moves . urllib . error as urllib_error
import ansible . module_utils . six . moves . urllib . error as urllib_error
from ansible . module_utils . six import PY3
from ansible . module_utils . basic import get_distribution
from ansible . module_utils . basic import get_distribution
from ansible . module_utils . _text import to_bytes , to_native , to_text
from ansible . module_utils . _text import to_bytes , to_native , to_text
@ -1049,11 +1052,33 @@ def fetch_url(module, url, data=None, headers=None, method=None,
url_password = password , http_agent = http_agent , force_basic_auth = force_basic_auth ,
url_password = password , http_agent = http_agent , force_basic_auth = force_basic_auth ,
follow_redirects = follow_redirects , client_cert = client_cert ,
follow_redirects = follow_redirects , client_cert = client_cert ,
client_key = client_key , cookies = cookies )
client_key = client_key , cookies = cookies )
info . update ( r . info ( ) )
# Lowercase keys, to conform to py2 behavior, so that py3 and py2 are predictable
info . update ( dict ( ( k . lower ( ) , v ) for k , v in r . info ( ) . items ( ) ) )
# Don't be lossy, append header values for duplicate headers
# In Py2 there is nothing that needs done, py2 does this for us
if PY3 :
temp_headers = { }
for name , value in r . headers . items ( ) :
# The same as above, lower case keys to match py2 behavior, and create more consistent results
name = name . lower ( )
if name in temp_headers :
temp_headers [ name ] = ' , ' . join ( ( temp_headers [ name ] , value ) )
else :
temp_headers [ name ] = value
info . update ( temp_headers )
# parse the cookies into a nice dictionary
# parse the cookies into a nice dictionary
cookie_list = [ ]
cookie_dict = dict ( )
cookie_dict = dict ( )
# Python sorts cookies in order of most specific (ie. longest) path first. See ``CookieJar._cookie_attrs``
# Cookies with the same path are reversed from response order.
# This code makes no assumptions about that, and accepts the order given by python
for cookie in cookies :
for cookie in cookies :
cookie_dict [ cookie . name ] = cookie . value
cookie_dict [ cookie . name ] = cookie . value
cookie_list . append ( ( cookie . name , cookie . value ) )
info [ ' cookies_string ' ] = ' ; ' . join ( ' %s = %s ' % c for c in cookie_list )
info [ ' cookies ' ] = cookie_dict
info [ ' cookies ' ] = cookie_dict
# finally update the result with a message about the fetch
# finally update the result with a message about the fetch
info . update ( dict ( msg = " OK ( %s bytes) " % r . headers . get ( ' Content-Length ' , ' unknown ' ) , url = r . geturl ( ) , status = r . code ) )
info . update ( dict ( msg = " OK ( %s bytes) " % r . headers . get ( ' Content-Length ' , ' unknown ' ) , url = r . geturl ( ) , status = r . code ) )