# EC2 dynamic inventory, filter using variable?

**URL:** <https://forum.ansible.com/t/ec2-dynamic-inventory-filter-using-variable/43728>\
**Category:** Get Help\
**Tags:** aws\
**Created:** [July 4, 2025, 1:48pm UTC](https://forum.ansible.com/t/ec2-dynamic-inventory-filter-using-variable/43728 "2025-07-04T13:48:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [July 4, 2025, 1:48pm UTC](https://forum.ansible.com/t/ec2-dynamic-inventory-filter-using-variable/43728/1 "2025-07-04T13:48:28Z")

</div>

Hi, I’m working my way through a migration to EC2 dynamic inventory.  
I have everything working, but I’m a bit stuck on how to filter the found instances.

If I have this in `aws_ec2.py`, things work (i.e. I get the right instances):

```yaml
plugin: amazon.aws.aws_ec2
regions:
  - "{{ lookup('ansible.builtin.env', 'AWS_DEFAULT_REGION') }}"
filters:
  tag:Environment: dev

```

I test this by:

```shell
ansible-inventory -i aws_ec2.py --list

```

But I’d like to use a variable for the environment. I thought that would be easy:

```yaml
filters:
  tag:Environment: "{{ env }}"

```

and then doing:

```shell
ansible-inventory -e env=dev -i aws_ec2.py --list

```

But now I don’t get any instances…

Any ideas if this is possible at all…?

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [July 9, 2025, 7:25pm UTC](https://forum.ansible.com/t/ec2-dynamic-inventory-filter-using-variable/43728/2 "2025-07-09T19:25:29Z")

</div>

Wouldn’t it be something like `ansible-inventory -e 'Environment="dev"' -i aws_ec2.py --list`
