Skip to content

Commit

Permalink
support ec2 metadata uri options
Browse files Browse the repository at this point in the history
  • Loading branch information
hitsumabushi committed Oct 25, 2024
1 parent e001f0e commit e39ae42
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
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"
}
default = ec2_metadata_facts.Ec2Metadata(module)
assert default.ec2_metadata_token_uri == "http://example.com/ec2_metadata_token_uri"

0 comments on commit e39ae42

Please sign in to comment.