Skip to content

Commit

Permalink
Add instance tags to ec2_metadata_facts return values (#2398)
Browse files Browse the repository at this point in the history
SUMMARY
Fixes #2293
A list of instance tag keys was added to the return values in version 5.5.0. This adds a new return value that includes the full key:value pairs for the instance tags.
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
ec2_metadata_facts

Reviewed-by: Mark Chappell
Reviewed-by: Helen Bailey <hebailey@redhat.com>
Reviewed-by: Alina Buzachis
Reviewed-by: Bikouo Aubin
  • Loading branch information
hakbailey authored Dec 3, 2024
1 parent 5a9d6aa commit 43387ec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
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
11 changes: 10 additions & 1 deletion tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml
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 All @@ -29,6 +29,7 @@

- ansible.builtin.include_role:
name: ../setup_sshkey

- ansible.builtin.include_role:
name: ../setup_ec2_facts

Expand Down Expand Up @@ -103,6 +104,14 @@
state: present
register: ec2_key_result

- amazon.aws.ec2_key_info:
filters:
fingerprint: "{{ fingerprint }}"
register: key_info
until: key_info.keypairs | length == 1
retries: 5
delay: 10

- name: Set facts to simplify use of extra resources
ansible.builtin.set_fact:
vpc_subnet_id: "{{ vpc_subnet_result.subnet.id }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
- 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.snake_case_key == "a_snake_case_value"
- ansible_ec2_instance_tags.camelCaseKey == "aCamelCaseValue"
- ansible_ec2_instance_tags_keys is defined
- ansible_ec2_instance_tags_keys | length == 3
- '"snake_case_key" in ansible_ec2_instance_tags_keys'
- '"camelCaseKey" in ansible_ec2_instance_tags_keys'

- name: Clear facts for another test
ansible.builtin.meta: clear_facts
Expand All @@ -29,5 +33,9 @@
- 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.snake_case_key == "a_snake_case_value"
- ansible_ec2_instance_tags.camelCaseKey == "aCamelCaseValue"
- ansible_ec2_instance_tags_keys is defined
- ansible_ec2_instance_tags_keys | length == 3
- '"snake_case_key" in ansible_ec2_instance_tags_keys'
- '"camelCaseKey" in ansible_ec2_instance_tags_keys'

0 comments on commit 43387ec

Please sign in to comment.