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

Add instance tags to ec2_metadata_facts return values #2398

Open
wants to merge 4 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
2 changes: 2 additions & 0 deletions changelogs/fragments/20241120-ec2_metadata_facts-tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_metadata_facts - add ``ansible_ec2_instance_tags`` to return values (https://github.com/ansible-collections/amazon.aws/pull/2398).
18 changes: 18 additions & 0 deletions plugins/modules/ec2_metadata_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@
description: The purchasing option of the instance.
type: str
sample: "on-demand"
ansible_ec2_instance_tags:
description:
- The dict of tags for the instance.
- Returns empty dict if access to tags (InstanceMetadataTags) in instance metadata is not enabled.
type: dict
sample: {"tagKey1": "tag value 1", "tag_key2": "tag value 2"}
version_added: 9.1.0
ansible_ec2_instance_tags_keys:
description:
- The list of tags keys of the instance.
Expand Down Expand Up @@ -646,6 +653,14 @@ def fetch_session_token(self, uri_token):
token_data = None
return to_text(token_data)

def get_instance_tags(self, tag_keys, data):
tags = {}
for key in tag_keys:
value = data.get("ansible_ec2_tags_instance_{}".format(key))
if value is not None:
tags[key] = value
return tags

def run(self):
self._token = self.fetch_session_token(self.uri_token) # create session token for IMDS
self.fetch(self.uri_meta) # populate _data with metadata
Expand All @@ -663,6 +678,9 @@ def run(self):
instance_tags_keys = instance_tags_keys.split("\n") if instance_tags_keys != "None" else []
data[self._prefix % "instance_tags_keys"] = instance_tags_keys

instance_tags = self.get_instance_tags(instance_tags_keys, data)
data[self._prefix % "instance_tags"] = instance_tags

# Maintain old key for backwards compatibility
if "ansible_ec2_instance_identity_document_region" in data:
data["ansible_ec2_placement_region"] = data["ansible_ec2_instance_identity_document_region"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
hosts: localhost

collections:
- amzon.aws
- amazon.aws
- community.aws

vars:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
- ansible_ec2_placement_availability_zone == availability_zone
- ansible_ec2_security_groups == resource_prefix+"-sg"
- ansible_ec2_user_data == "None"
- ansible_ec2_instance_tags is defined
- ansible_ec2_instance_tags | length == 3
- ansible_ec2_instance_tags.snake_case_key == "a_snake_case_value"
hakbailey marked this conversation as resolved.
Show resolved Hide resolved
- ansible_ec2_instance_tags.camelCaseKey == "aCamelCaseValue"
- ansible_ec2_instance_tags.Name == resource_prefix+"-ec2-metadata-facts"
- ansible_ec2_instance_tags_keys is defined
- ansible_ec2_instance_tags_keys | length == 3

Expand All @@ -29,5 +34,7 @@
- ansible_ec2_placement_availability_zone == availability_zone
- ansible_ec2_security_groups == resource_prefix+"-sg"
- ansible_ec2_user_data == "None"
- ansible_ec2_instance_tags is defined
- ansible_ec2_instance_tags | length == 3
- ansible_ec2_instance_tags_keys is defined
- ansible_ec2_instance_tags_keys | length == 3
Loading