# Question: Dynamic inventory create group based on 2 tags

**URL:** <https://forum.ansible.com/t/question-dynamic-inventory-create-group-based-on-2-tags/36687>\
**Category:** AWX Project\
**Created:** [June 3, 2022, 1:43pm UTC](https://forum.ansible.com/t/question-dynamic-inventory-create-group-based-on-2-tags/36687 "2022-06-03T13:43:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Bouse](https://avatars.discourse-cdn.com/v4/letter/j/ba8739/32.png) [@Jeremy\_Bouse](https://forum.ansible.com/u/Jeremy_Bouse)\
**Post date:** [June 3, 2022, 1:43pm UTC](https://forum.ansible.com/t/question-dynamic-inventory-create-group-based-on-2-tags/36687/1 "2022-06-03T13:43:48Z")

</div>

I may be trying to over-complicate this and the solution is really simple and I’ve just overlooked it, but asking in case someone else has attempted this and has a solution.

I’m attempting to setup dynamic inventory of EC2 instances and they are currently tagged following a standard practice. I can easily use `- key: tags.X | lower` and `- key: tags.Y | lower` under `keyed_groups` and I get them grouped by the tags separately but what I really need is a group that is formatted underscore delimited like `tags.X_tags.Y | lower` if tags.Y is present otherwise omit it. I thought this would be possible as a jinja2 template but all attempts I’ve tried thus far have not produced the desired outcome.

TIA

---

<div class="post-metadata">

**Author:** ![AWX\_Project](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AWX\_Project](https://forum.ansible.com/u/AWX_Project)\
**Post date:** [June 9, 2022, 2:02pm UTC](https://forum.ansible.com/t/question-dynamic-inventory-create-group-based-on-2-tags/36687/2 "2022-06-09T14:02:18Z")

</div>

I don’t have an EC2 account handy to test the ec2 inventory plugin against but ansible has a ternary operator that you can use to make a simple if/then/else condition. Here is an example playbook:
