From 01b6f02a80d904f2b9bec1874d6cae93561a977e Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Tue, 9 Oct 2018 13:21:50 -0400 Subject: [PATCH] [2.7] ec2_group - fix VPC precedence for security group targets (#45787) (#45815) * 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 (cherry picked from commit 8d2df9be52768d3acb65a45edad87ea7da2dcff2) * Fix ec2_group for EC2-Classic accounts (#46242) * Fix ec2_group for EC2-Classic accounts * changelog (cherry picked from commit 9efc3dc761413db1f58fa90822430b6209bff6ef) * Merge changelogs --- .../fragments/fix_ec2_group_target_vpc_precedence.yaml | 7 +++++++ lib/ansible/modules/cloud/amazon/ec2_group.py | 3 +++ 2 files changed, 10 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 00000000000..9e27fc98935 --- /dev/null +++ b/changelogs/fragments/fix_ec2_group_target_vpc_precedence.yaml @@ -0,0 +1,7 @@ +--- +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. + - ec2_group - support EC2-Classic by not assuming security groups have VPCs. diff --git a/lib/ansible/modules/cloud/amazon/ec2_group.py b/lib/ansible/modules/cloud/amazon/ec2_group.py index fe6984ae756..b266972b307 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.get('VpcId') and group['VpcId'] == vpc_id) + groups.update(vpc_wins) # maintain backwards compatibility by using the last matching group return security_groups[-1], groups return None, {}