Skip to content

Commit

Permalink
Allow default regions list to use flexible credential types (ansible#…
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hertel authored and ansibot committed Feb 9, 2019
1 parent ad549e3 commit bcefd61
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/ansible/plugins/inventory/aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,32 @@ def _get_credentials(self):

return boto_params

def _get_connection(self, credentials, region='us-east-1'):
try:
connection = boto3.session.Session(profile_name=self.boto_profile).client('ec2', region, **credentials)
except (botocore.exceptions.ProfileNotFound, botocore.exceptions.PartialCredentialsError) as e:
if self.boto_profile:
try:
connection = boto3.session.Session(profile_name=self.boto_profile).client('ec2', region)
except (botocore.exceptions.ProfileNotFound, botocore.exceptions.PartialCredentialsError) as e:
raise AnsibleError("Insufficient credentials found: %s" % to_native(e))
else:
raise AnsibleError("Insufficient credentials found: %s" % to_native(e))
return connection

def _boto3_conn(self, regions):
'''
:param regions: A list of regions to create a boto3 client
Generator that yields a boto3 client and the region
'''

credentials = self._get_credentials()

if not regions:
try:
# as per https://boto3.amazonaws.com/v1/documentation/api/latest/guide/ec2-example-regions-avail-zones.html
client = boto3.client('ec2')
client = self._get_connection(credentials)
resp = client.describe_regions()
regions = [x['RegionName'] for x in resp.get('Regions', [])]
except botocore.exceptions.NoRegionError:
Expand All @@ -342,19 +357,8 @@ def _boto3_conn(self, regions):
if not regions:
raise AnsibleError('Unable to get regions list from available methods, you must specify the "regions" option to continue.')

credentials = self._get_credentials()

for region in regions:
try:
connection = boto3.session.Session(profile_name=self.boto_profile).client('ec2', region, **credentials)
except (botocore.exceptions.ProfileNotFound, botocore.exceptions.PartialCredentialsError) as e:
if self.boto_profile:
try:
connection = boto3.session.Session(profile_name=self.boto_profile).client('ec2', region)
except (botocore.exceptions.ProfileNotFound, botocore.exceptions.PartialCredentialsError) as e:
raise AnsibleError("Insufficient credentials found: %s" % to_native(e))
else:
raise AnsibleError("Insufficient credentials found: %s" % to_native(e))
connection = self._get_connection(credentials, region)
yield connection, region

def _get_instances_by_region(self, regions, filters, strict_permissions):
Expand Down

0 comments on commit bcefd61

Please sign in to comment.