|
814 | 814 | from ansible.module_utils.six.moves.urllib import parse as urlparse |
815 | 815 |
|
816 | 816 | from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule |
| 817 | +from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code |
| 818 | +from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_message |
817 | 819 | from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry |
818 | 820 | from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list |
819 | 821 | from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_tag_list |
@@ -1119,15 +1121,13 @@ def discover_security_groups(group, groups, parent_vpc_id=None, subnet_id=None, |
1119 | 1121 | if subnet_id is not None: |
1120 | 1122 | try: |
1121 | 1123 | sub = ec2.describe_subnets(aws_retry=True, SubnetIds=[subnet_id]) |
1122 | | - except botocore.exceptions.ClientError as e: |
1123 | | - if e.response['Error']['Code'] == 'InvalidGroup.NotFound': |
1124 | | - module.fail_json( |
1125 | | - "Could not find subnet {0} to associate security groups. Please check the vpc_subnet_id and security_groups parameters.".format( |
1126 | | - subnet_id |
1127 | | - ) |
| 1124 | + except is_boto3_error_code('InvalidGroup.NotFound'): |
| 1125 | + module.fail_json( |
| 1126 | + "Could not find subnet {0} to associate security groups. Please check the vpc_subnet_id and security_groups parameters.".format( |
| 1127 | + subnet_id |
1128 | 1128 | ) |
1129 | | - module.fail_json_aws(e, msg="Error while searching for subnet {0} parent VPC.".format(subnet_id)) |
1130 | | - except botocore.exceptions.BotoCoreError as e: |
| 1129 | + ) |
| 1130 | + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except |
1131 | 1131 | module.fail_json_aws(e, msg="Error while searching for subnet {0} parent VPC.".format(subnet_id)) |
1132 | 1132 | parent_vpc_id = sub['Subnets'][0]['VpcId'] |
1133 | 1133 |
|
@@ -1615,9 +1615,9 @@ def determine_iam_role(name_or_arn): |
1615 | 1615 | try: |
1616 | 1616 | role = iam.get_instance_profile(InstanceProfileName=name_or_arn, aws_retry=True) |
1617 | 1617 | return role['InstanceProfile']['Arn'] |
1618 | | - except botocore.exceptions.ClientError as e: |
1619 | | - if e.response['Error']['Code'] == 'NoSuchEntity': |
1620 | | - module.fail_json_aws(e, msg="Could not find instance_role {0}".format(name_or_arn)) |
| 1618 | + except is_boto3_error_code('NoSuchEntity'): |
| 1619 | + module.fail_json_aws(e, msg="Could not find instance_role {0}".format(name_or_arn)) |
| 1620 | + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except |
1621 | 1621 | module.fail_json_aws(e, msg="An error occurred while searching for instance_role {0}. Please try supplying the full ARN.".format(name_or_arn)) |
1622 | 1622 |
|
1623 | 1623 |
|
@@ -1697,15 +1697,12 @@ def ensure_present(existing_matches, changed, ec2, state): |
1697 | 1697 |
|
1698 | 1698 | def run_instances(ec2, **instance_spec): |
1699 | 1699 | try: |
1700 | | - return ec2.run_instances(aws_retry=True, **instance_spec) |
1701 | | - except botocore.exceptions.ClientError as e: |
1702 | | - if e.response['Error']['Code'] == 'InvalidParameterValue' and "Invalid IAM Instance Profile ARN" in e.response['Error']['Message']: |
1703 | | - # If the instance profile has just been created, it takes some time to be visible by ec2 |
1704 | | - # So we wait 10 second and retry the run_instances |
1705 | | - time.sleep(10) |
1706 | | - return ec2.run_instances(**instance_spec) |
1707 | | - else: |
1708 | | - raise e |
| 1700 | + return ec2.run_instances(**instance_spec) |
| 1701 | + except is_boto3_error_message('Invalid IAM Instance Profile ARN'): |
| 1702 | + # If the instance profile has just been created, it takes some time to be visible by ec2 |
| 1703 | + # So we wait 10 second and retry the run_instances |
| 1704 | + time.sleep(10) |
| 1705 | + return ec2.run_instances(**instance_spec) |
1709 | 1706 |
|
1710 | 1707 |
|
1711 | 1708 | def main(): |
|
0 commit comments