EC2 module not filtering tags from different vpc

We are experimenting with a staging and production environment each in their own vpc. It has been a struggle to use the EC2 module with this setup because despite applying instance filters to ec2. ini, the ‘count’ tags used in the ec2 module for provisioning count instances across vpcs if they have the same tag. Ths instance filters are using vpc ids, I can see this works when running a simple list of instances but the exact count setting in the EC2 module counts instances with tags across vpcs anyways.

I’ll try to be more clear on the issue. We have 8 tags/groups per environment (staging/production) in different vpcs that need services provisioned and updated in EC2:

webservers
databases
dashboards
etc…

Since you can not use a variable from /etc/ansible in the ‘hosts:’ setting in a playbook, we can either

  • use different group names for each environment (and hence need different plays per)
  • ideally, use the same tag_Name setting across environments and rely on the ec2.py/ini combo to filter for the right instances based on vpc subnet id

The second case is preferable, but despite filtering by vpc_id in ec2.py, the ‘exact_count’ setting in the ec2 module for provisioning looks at the number of instances tagged across vpcs.

Does anyone know of a way to make it only consider the instances that pass the instance filters from the dynamic inventory script?

Hi Gary, the tag names from the dynamic inventory are brought in as group names for the instances, so I would use group_vars/TAG_NAME to bring in those specific variables. Alternatively, you could use the include_vars module to include YAML files based on variables available (like the tag name).

Hope that helps!