Allow default regions list to use flexible credential types (#51451)

This commit is contained in:
Sloane Hertel 2019-02-08 23:22:56 -06:00 committed by ansibot
parent ad549e375a
commit bcefd61437

View file

@ -316,6 +316,19 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
return boto_params 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): def _boto3_conn(self, regions):
''' '''
:param regions: A list of regions to create a boto3 client :param regions: A list of regions to create a boto3 client
@ -323,10 +336,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
Generator that yields a boto3 client and the region Generator that yields a boto3 client and the region
''' '''
credentials = self._get_credentials()
if not regions: if not regions:
try: try:
# as per https://boto3.amazonaws.com/v1/documentation/api/latest/guide/ec2-example-regions-avail-zones.html # 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() resp = client.describe_regions()
regions = [x['RegionName'] for x in resp.get('Regions', [])] regions = [x['RegionName'] for x in resp.get('Regions', [])]
except botocore.exceptions.NoRegionError: except botocore.exceptions.NoRegionError:
@ -342,19 +357,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
if not regions: if not regions:
raise AnsibleError('Unable to get regions list from available methods, you must specify the "regions" option to continue.') 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: for region in regions:
try: connection = self._get_connection(credentials, region)
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))
yield connection, region yield connection, region
def _get_instances_by_region(self, regions, filters, strict_permissions): def _get_instances_by_region(self, regions, filters, strict_permissions):