From 8d2df9be52768d3acb65a45edad87ea7da2dcff2 Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Tue, 18 Sep 2018 15:33:19 -0400 Subject: [PATCH] ec2_group - fix VPC precedence for security group targets (#45787) Update the dictionary with the preferred values last to get the right order of VPC precedence Fixes #45782 --- .../fragments/fix_ec2_group_target_vpc_precedence.yaml | 6 ++++++ lib/ansible/modules/cloud/amazon/ec2_group.py | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 changelogs/fragments/fix_ec2_group_target_vpc_precedence.yaml 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 0000000000..68f24f49d1 --- /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 fe6984ae75..4bc53dd04a 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, {}