[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 8d2df9be52)

* Fix ec2_group for EC2-Classic accounts (#46242)

* Fix ec2_group for EC2-Classic accounts

* changelog

(cherry picked from commit 9efc3dc761)

* Merge changelogs
This commit is contained in:
Sloane Hertel 2018-10-09 13:21:50 -04:00 committed by Toshio Kuratomi
parent ff1aa50fdb
commit 01b6f02a80
2 changed files with 10 additions and 0 deletions

View file

@ -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.

View file

@ -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, {}