diff --git a/changelogs/fragments/fix_ec2_group_target_vpc_precedence.yaml b/changelogs/fragments/fix_ec2_group_target_vpc_precedence.yaml new file mode 100644 index 00000000000..68f24f49d12 --- /dev/null +++ b/changelogs/fragments/fix_ec2_group_target_vpc_precedence.yaml @@ -0,0 +1,6 @@ +--- +bugfixes: + - ec2_group - There can be multiple security groups with the same name in + different VPCs. Prior to 2.6 if a target group name was provided, the group + matching the name and VPC had highest precedence. Restore this behavior by + updated the dictionary with the groups matching the VPC last. diff --git a/lib/ansible/modules/cloud/amazon/ec2_group.py b/lib/ansible/modules/cloud/amazon/ec2_group.py index fe6984ae756..4bc53dd04aa 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_group.py +++ b/lib/ansible/modules/cloud/amazon/ec2_group.py @@ -855,6 +855,9 @@ def group_exists(client, module, vpc_id, group_id, name): if security_groups: groups = dict((group['GroupId'], group) for group in all_groups) groups.update(dict((group['GroupName'], group) for group in all_groups)) + if vpc_id: + vpc_wins = dict((group['GroupName'], group) for group in all_groups if group['VpcId'] == vpc_id) + groups.update(vpc_wins) # maintain backwards compatibility by using the last matching group return security_groups[-1], groups return None, {}