diff --git a/changelogs/fragments/2364-ec2-metadata-uri-options.yml b/changelogs/fragments/2364-ec2-metadata-uri-options.yml new file mode 100644 index 00000000000..9660c0cd7ef --- /dev/null +++ b/changelogs/fragments/2364-ec2-metadata-uri-options.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - ec2_metadata_facts - Add support to change instance metadata service endpoint uri for test diff --git a/plugins/modules/ec2_metadata_facts.py b/plugins/modules/ec2_metadata_facts.py index db955f0ae0d..8e3d3e6249c 100644 --- a/plugins/modules/ec2_metadata_facts.py +++ b/plugins/modules/ec2_metadata_facts.py @@ -31,6 +31,42 @@ type: int default: 60 version_added: 8.2.0 + ec2_metadata_token_uri: + description: + - This option is for testing purpuse. + - If not specified, V(http://169.254.169.254/latest/api/token) is used. + type: str + version_added: 8.3.0 + ec2_metadata_uri: + description: + - This option is for testing purpuse. + - If not specified, V(http://169.254.169.254/latest/meta-data/) is used. + type: str + version_added: 8.3.0 + ec2_metadata_instance_tags_uri: + description: + - This option is for testing purpuse. + - If not specified, V(http://169.254.169.254/latest/meta-data/tags/instance) is used. + type: str + version_added: 8.3.0 + ec2_sshdata_uri: + description: + - This option is for testing purpuse. + - If not specified, V(http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key) is used. + type: str + version_added: 8.3.0 + ec2_userdata_uri: + description: + - This option is for testing purpuse. + - If not specified, V(http://169.254.169.254/latest/user-data/) is used. + type: str + version_added: 8.3.0 + ec2_dynamicdata_uri: + description: + - This option is for testing purpuse. + - If not specified, V(http://169.254.169.254/latest/dynamic/) is used. + type: str + version_added: 8.3.0 """ EXAMPLES = r""" @@ -672,6 +708,12 @@ def run(self): def main(): argument_spec = dict( metadata_token_ttl_seconds=dict(required=False, default=60, type="int", no_log=False), + ec2_metadata_token_uri=dict(required=False, default=None, type="str"), + ec2_metadata_uri=dict(required=False, default=None, type="str"), + ec2_metadata_instance_tags_uri=dict(required=False, default=None, type="str"), + ec2_sshdata_uri=dict(required=False, default=None, type="str"), + ec2_userdata_uri=dict(required=False, default=None, type="str"), + ec2_dynamicdata_uri=dict(required=False, default=None, type="str"), ) module = AnsibleModule( @@ -684,7 +726,15 @@ def main(): if metadata_token_ttl_seconds <= 0 or metadata_token_ttl_seconds > 21600: module.fail_json(msg="The option 'metadata_token_ttl_seconds' must be set to a value between 1 and 21600.") - ec2_metadata_facts = Ec2Metadata(module).run() + ec2_metadata_facts = Ec2Metadata( + module, + ec2_metadata_token_uri=module.params["ec2_metadata_token_uri"], + ec2_metadata_uri=module.params["ec2_metadata_uri"], + ec2_metadata_instance_tags_uri=module.params["ec2_metadata_instance_tags_uri"], + ec2_sshdata_uri=module.params["ec2_sshdata_uri"], + ec2_userdata_uri=module.params["ec2_userdata_uri"], + ec2_dynamicdata_uri=module.params["ec2_dynamicdata_uri"], + ).run() ec2_metadata_facts_result = dict(changed=False, ansible_facts=ec2_metadata_facts) module.exit_json(**ec2_metadata_facts_result) diff --git a/tests/unit/plugins/modules/test_ec2_metadata_facts.py b/tests/unit/plugins/modules/test_ec2_metadata_facts.py index 23ba850030e..cd9c3b2e3f9 100644 --- a/tests/unit/plugins/modules/test_ec2_metadata_facts.py +++ b/tests/unit/plugins/modules/test_ec2_metadata_facts.py @@ -99,3 +99,13 @@ def test__fetch_user_data_plain(m_fetch_url, ec2_instance): m_fetch_url.return_value = (io.BytesIO(user_data), {"status": 200}) assert ec2_instance._fetch("http://169.254.169.254/latest/user-data") == user_data.decode("utf-8") + + +@patch(module_name + ".fetch_url") +def test__custom_uri(m_fetch_url, ec2_instance): + module = MagicMock() + module.params = { + "ec2_metadata_token_uri": "http://example.com/ec2_metadata_token_uri" + } + custom = ec2_metadata_facts.Ec2Metadata(module) + assert custom.ec2_metadata_token_uri == "http://example.com/ec2_metadata_token_uri"