diff --git a/lib/ansible/modules/cloud/amazon/ec2_metadata_facts.py b/lib/ansible/modules/cloud/amazon/ec2_metadata_facts.py index eea90b7b20..5a0f542f3a 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_metadata_facts.py +++ b/lib/ansible/modules/cloud/amazon/ec2_metadata_facts.py @@ -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)