From 6e13aadd09fa46567ac181791b57fe4c8a06b5f6 Mon Sep 17 00:00:00 2001 From: Helen Bailey Date: Wed, 20 Nov 2024 08:55:57 -0500 Subject: [PATCH 1/6] Add instance tags to ec2_metadata_facts return values --- plugins/modules/ec2_metadata_facts.py | 17 +++++++++++++++++ .../ec2_metadata_facts/playbooks/setup.yml | 2 +- .../playbooks/test_metadata.yml | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/plugins/modules/ec2_metadata_facts.py b/plugins/modules/ec2_metadata_facts.py index db955f0ae0d..a33ab6a7313 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,13 @@ def fetch_session_token(self, uri_token): token_data = None return to_text(token_data) + def get_instance_tags(self, tag_keys): + tags = {} + for key in tag_keys: + value = self._fetch("{}/{}".format(self.uri_instance_tags, key)) + 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 +677,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[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..a3db1d1ba85 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: 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..512cbabd1f0 100644 --- a/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml +++ b/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml @@ -13,6 +13,8 @@ - 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 @@ -29,5 +31,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 From f6ea0694207c980c9afe2a38cd3cdb11761824bb Mon Sep 17 00:00:00 2001 From: Helen Bailey Date: Wed, 20 Nov 2024 09:01:09 -0500 Subject: [PATCH 2/6] Add changelog fragment --- changelogs/fragments/20241120-ec2_metadata_facts-tags.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/20241120-ec2_metadata_facts-tags.yml 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). From 20eb1cb8ca9579ce21262ca4994c734d6f7bfe97 Mon Sep 17 00:00:00 2001 From: Helen Bailey Date: Thu, 21 Nov 2024 11:29:50 -0500 Subject: [PATCH 3/6] Update tag retrieval function --- plugins/modules/ec2_metadata_facts.py | 9 +++++---- .../ec2_metadata_facts/playbooks/test_metadata.yml | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/modules/ec2_metadata_facts.py b/plugins/modules/ec2_metadata_facts.py index a33ab6a7313..3eec3f80ec6 100644 --- a/plugins/modules/ec2_metadata_facts.py +++ b/plugins/modules/ec2_metadata_facts.py @@ -653,11 +653,12 @@ def fetch_session_token(self, uri_token): token_data = None return to_text(token_data) - def get_instance_tags(self, tag_keys): + def get_instance_tags(self, tag_keys, data): tags = {} for key in tag_keys: - value = self._fetch("{}/{}".format(self.uri_instance_tags, key)) - tags[key] = value + value = data.get("ansible_ec2_tags_instance_{}".format(key)) + if value is not None: + tags[key] = value return tags def run(self): @@ -677,7 +678,7 @@ 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) + instance_tags = self.get_instance_tags(instance_tags_keys, data) data[self._prefix % "instance_tags"] = instance_tags # Maintain old key for backwards compatibility 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 512cbabd1f0..d0787f1324e 100644 --- a/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml +++ b/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml @@ -15,6 +15,7 @@ - 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" - ansible_ec2_instance_tags_keys is defined - ansible_ec2_instance_tags_keys | length == 3 From 95dc08c9299f7e9bff78b0abc7b12f7357acf89c Mon Sep 17 00:00:00 2001 From: Helen Bailey Date: Mon, 25 Nov 2024 10:20:03 -0500 Subject: [PATCH 4/6] Update tests --- .../targets/ec2_metadata_facts/playbooks/test_metadata.yml | 2 ++ 1 file changed, 2 insertions(+) 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 d0787f1324e..61adfd8eb8a 100644 --- a/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml +++ b/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml @@ -16,6 +16,8 @@ - ansible_ec2_instance_tags is defined - ansible_ec2_instance_tags | length == 3 - ansible_ec2_instance_tags.snake_case_key == "a_snake_case_value" + - 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 From a162e08e3ee05078dac507425ecdbbf31f647d62 Mon Sep 17 00:00:00 2001 From: Helen Bailey Date: Mon, 2 Dec 2024 13:04:50 -0500 Subject: [PATCH 5/6] Update tests --- .../ec2_metadata_facts/playbooks/test_metadata.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 61adfd8eb8a..a75a11832c8 100644 --- a/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml +++ b/tests/integration/targets/ec2_metadata_facts/playbooks/test_metadata.yml @@ -14,12 +14,11 @@ - 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" - 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 + - '"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 @@ -35,6 +34,8 @@ - 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" + - 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' From 001356c384b36460950a97836d5d18ee7df7e21d Mon Sep 17 00:00:00 2001 From: Helen Bailey Date: Mon, 2 Dec 2024 14:13:52 -0500 Subject: [PATCH 6/6] Fix key eventual consistency bug in integration test --- .../targets/ec2_metadata_facts/playbooks/setup.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml b/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml index a3db1d1ba85..aaeda488a7d 100644 --- a/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml +++ b/tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml @@ -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 }}"