From f7f3a22be52cdb0cfc8e62a191d2b1166019f54b Mon Sep 17 00:00:00 2001 From: hitsumabushi Date: Sat, 26 Oct 2024 03:16:56 +0900 Subject: [PATCH 1/3] support ec2 metadata uri options --- plugins/modules/ec2_metadata_facts.py | 52 ++++++++++++++++++- .../modules/test_ec2_metadata_facts.py | 10 ++++ 2 files changed, 61 insertions(+), 1 deletion(-) 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..c64ec219626 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" + } + default = ec2_metadata_facts.Ec2Metadata(module) + assert default.ec2_metadata_token_uri == "http://example.com/ec2_metadata_token_uri" From 49fa60955804541fc1b5adf6ab88c614f9af3b36 Mon Sep 17 00:00:00 2001 From: hitsumabushi Date: Sat, 26 Oct 2024 03:58:30 +0900 Subject: [PATCH 2/3] fix var name --- tests/unit/plugins/modules/test_ec2_metadata_facts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/plugins/modules/test_ec2_metadata_facts.py b/tests/unit/plugins/modules/test_ec2_metadata_facts.py index c64ec219626..cd9c3b2e3f9 100644 --- a/tests/unit/plugins/modules/test_ec2_metadata_facts.py +++ b/tests/unit/plugins/modules/test_ec2_metadata_facts.py @@ -107,5 +107,5 @@ def test__custom_uri(m_fetch_url, ec2_instance): module.params = { "ec2_metadata_token_uri": "http://example.com/ec2_metadata_token_uri" } - default = ec2_metadata_facts.Ec2Metadata(module) - assert default.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" From 54865338fdbfa1083f9dde6561850223e630902b Mon Sep 17 00:00:00 2001 From: hitsumabushi Date: Fri, 1 Nov 2024 17:36:28 +0900 Subject: [PATCH 3/3] add change log fragments for this pr --- changelogs/fragments/2364-ec2-metadata-uri-options.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/2364-ec2-metadata-uri-options.yml 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