ansible-collections / amazon.aws

Ansible Collection for Amazon AWS
GNU General Public License v3.0
309 stars 340 forks source link

aws_ec2 inventory plugin - Filters does not support AND logic of multiple tag-key values #1354

Open viv-dev opened 1 year ago

viv-dev commented 1 year ago

Summary

It seems that there is no way to query ec2 instances via multiple tag-key filters that have AND logic since filters is a dict and include_filters treats all variations as OR conditions.

Issue Type

Bug Report

Component Name

amazon.aws.aws_ec2

Ansible Version

ansible [core 2.14.2]

Collection Versions

Collection Version
---------- -------
amazon.aws 5.1.0  

AWS SDK versions

Name: boto3
Version: 1.26.60

Name: botocore
Version: 1.29.60

Configuration

No response

OS / Environment

No response

Steps to Reproduce

plugin: amazon.aws.aws_ec2

hostnames:
  - tag:Name

filters:
  # Applies as OR logic
  # Because filters is a dict can't specify multiple separate tag-key filters
  tag-key:
    - Role
    - Team
plugin: amazon.aws.aws_ec2

hostnames:
  - tag:Name

include_filters:
  # Applies as OR logic
  - tag-key: Role
  - tag-key: Team
plugin: amazon.aws.aws_ec2

hostnames:
  - tag:Name

include_filters:
  # Also applies as OR logic
  - tag-key: 
      - Role
      - Team

Expected Results

That there is a way to apply AND logic for multiple tag keys as per the aws command below:

aws ec2 describe-instances --filter Name=tag-key,Values=Role Name=tag-key,Values=Team

This is in line with the AWS documentation which says that multiple filters with the same name are treated with the AND condition:

https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html#options

Actual Results

Both the filters version and include_filters return instances that have either the tag Role defined or Team defined, but not explicitly the ones that only have both.

Code of Conduct

ansibullbot commented 1 year ago

Files identified in the description: None

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help