@ -423,7 +423,7 @@ import time
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . _text import to_text
from ansible . module_utils . _text import to_text
from ansible . module_utils . urls import fetch_url
from ansible . module_utils . urls import fetch_url
from ansible . module_utils . six . moves . urllib . parse import quote
socket . setdefaulttimeout ( 5 )
socket . setdefaulttimeout ( 5 )
@ -444,13 +444,14 @@ class Ec2Metadata(object):
self . _prefix = ' ansible_ec2_ %s '
self . _prefix = ' ansible_ec2_ %s '
def _fetch ( self , url ) :
def _fetch ( self , url ) :
response , info = fetch_url ( self . module , url , force = True )
encoded_url = quote ( url , safe = ' % /:=&?~#+!$,; \' @()*[] ' )
response , info = fetch_url ( self . module , encoded_url , force = True )
if info . get ( ' status ' ) not in ( 200 , 404 ) :
if info . get ( ' status ' ) not in ( 200 , 404 ) :
time . sleep ( 3 )
time . sleep ( 3 )
# request went bad, retry once then raise
# request went bad, retry once then raise
self . module . warn ( ' Retrying query to metadata service. First attempt failed: {0} ' . format ( info [ ' msg ' ] ) )
self . module . warn ( ' Retrying query to metadata service. First attempt failed: {0} ' . format ( info [ ' msg ' ] ) )
response , info = fetch_url ( self . module , url, force = True )
response , info = fetch_url ( self . module , encoded_ url, force = True )
if info . get ( ' status ' ) not in ( 200 , 404 ) :
if info . get ( ' status ' ) not in ( 200 , 404 ) :
# fail out now
# fail out now
self . module . fail_json ( msg = ' Failed to retrieve metadata from AWS: {0} ' . format ( info [ ' msg ' ] ) , response = info )
self . module . fail_json ( msg = ' Failed to retrieve metadata from AWS: {0} ' . format ( info [ ' msg ' ] ) , response = info )