[aws] ec2_metadata_facts URL encode the metadata URL to avoid errors (#43394)

- If a field in the URL has a space it would result in a 400 without making it URL safe
- Fixes #42371 and #43378

(cherry picked from commit 4ce09ea62d)
This commit is contained in:
Vinay Dandekar 2018-09-06 15:24:26 -04:00 committed by Toshio Kuratomi
parent c60baf0ca7
commit 0e2642e392

View file

@ -423,7 +423,7 @@ import time
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_text
from ansible.module_utils.urls import fetch_url
from ansible.module_utils.six.moves.urllib.parse import quote
socket.setdefaulttimeout(5)
@ -444,13 +444,14 @@ class Ec2Metadata(object):
self._prefix = 'ansible_ec2_%s'
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):
time.sleep(3)
# request went bad, retry once then raise
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):
# fail out now
self.module.fail_json(msg='Failed to retrieve metadata from AWS: {0}'.format(info['msg']), response=info)