Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ec2 metadata uri options #2364

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/2364-ec2-metadata-uri-options.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- ec2_metadata_facts - Add support to change instance metadata service endpoint uri for test
52 changes: 51 additions & 1 deletion plugins/modules/ec2_metadata_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/plugins/modules/test_ec2_metadata_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading