diff --git a/changelogs/fragments/20241120-ec2_metadata_facts-tags.yml b/changelogs/fragments/20241120-ec2_metadata_facts-tags.yml new file mode 100644 index 00000000000..86ffd91b68e --- /dev/null +++ b/changelogs/fragments/20241120-ec2_metadata_facts-tags.yml @@ -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). diff --git a/plugins/modules/ec2_metadata_facts.py b/plugins/modules/ec2_metadata_facts.py index db955f0ae0d..3eec3f80ec6 100644 --- a/plugins/modules/ec2_metadata_facts.py +++ b/plugins/modules/ec2_metadata_facts.py @@ -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. @@ -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 @@ -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"] diff --git a/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml b/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml index aa83255d444..aaeda488a7d 100644 --- a/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml +++ b/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml @@ -9,7 +9,7 @@ hosts: localhost collections: - - amzon.aws + - amazon.aws - community.aws vars: @@ -29,6 +29,7 @@ - ansible.builtin.include_role: name: ../setup_sshkey + - ansible.builtin.include_role: name: ../setup_ec2_facts @@ -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 }}" diff --git a/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml b/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml index 158fda20ac5..a75a11832c8 100644 --- a/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml +++ b/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml @@ -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 @@ -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'