From a13e7888e749cf61ea0b6b63def2c2776714bd4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Fri, 16 Apr 2021 14:11:29 -0400 Subject: [PATCH] aws2_ec2 inventory: populate ec2_tag_* hostvars This patch exposes a new `use_contrib_script_compatible_ec2_tag_keys` that can be used to reproduce a behior of the old `ec2.py` inventory script from contrib. Closes: #183 --- ...ory_use_contrib_script_compatible_ec2_tag_keys.yaml | 3 +++ plugins/inventory/aws_ec2.py | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 changelogs/fragments/331_aws_ec2_inventory_use_contrib_script_compatible_ec2_tag_keys.yaml diff --git a/changelogs/fragments/331_aws_ec2_inventory_use_contrib_script_compatible_ec2_tag_keys.yaml b/changelogs/fragments/331_aws_ec2_inventory_use_contrib_script_compatible_ec2_tag_keys.yaml new file mode 100644 index 00000000000..0835e16cd7f --- /dev/null +++ b/changelogs/fragments/331_aws_ec2_inventory_use_contrib_script_compatible_ec2_tag_keys.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: +- "aws_ec2 inventory - expose a new configuration key ``use_contrib_script_compatible_ec2_tag_keys`` to reproduce a behavior of the old ``ec2.py`` inventory script. With this option enabled, each tag is exposed using a ``ec2_tag_TAGNAME`` key (https://github.com/ansible-collections/amazon.aws/pull/331)." diff --git a/plugins/inventory/aws_ec2.py b/plugins/inventory/aws_ec2.py index b5d92ad4d1a..26a9e8bc107 100644 --- a/plugins/inventory/aws_ec2.py +++ b/plugins/inventory/aws_ec2.py @@ -76,6 +76,12 @@ which group names end up being used as. type: bool default: False + use_contrib_script_compatible_ec2_tag_keys: + description: + - Expose the host tags with ec2_tag_TAGNAME keys like the old ec2.py inventory script. + - The use of this feature is discouraged and we advise to migrate to the new ``tags`` structure. + type: bool + default: False ''' EXAMPLES = ''' @@ -564,6 +570,10 @@ def _add_hosts(self, hosts, group, hostnames): host = camel_dict_to_snake_dict(host, ignore_list=['Tags']) host['tags'] = boto3_tag_list_to_ansible_dict(host.get('tags', [])) + if self.get_option('use_contrib_script_compatible_ec2_tag_keys'): + for k, v in host['tags'].items(): + host["ec2_tag_%s" % k] = v + # Allow easier grouping by region host['placement']['region'] = host['placement']['availability_zone'][:-1]