From 0e2642e392951cd3d3eb7696c770005fb7c97d06 Mon Sep 17 00:00:00 2001 From: Vinay Dandekar Date: Thu, 6 Sep 2018 15:24:26 -0400 Subject: [PATCH] [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 4ce09ea62da1741a3852ad51b4a565821ab50565) --- lib/ansible/modules/cloud/amazon/ec2_metadata_facts.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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)