diff --git a/changelogs/fragments/968-sanity.yml b/changelogs/fragments/968-sanity.yml new file mode 100644 index 00000000000..ecc2ba61f68 --- /dev/null +++ b/changelogs/fragments/968-sanity.yml @@ -0,0 +1,8 @@ +minor_changes: +- s3_object - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/968). +- aws_caller_info - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/968). +- cloudformation - avoid catching ``Exception``, catch more specific errors instead (https://github.com/ansible-collections/amazon.aws/pull/968). +- ec2_metadata_facts - avoid catching ``Exception``, catch more specific errors instead (https://github.com/ansible-collections/amazon.aws/pull/968). +- ec2_security_group - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/968). +- ec2_vpc_endpoint - avoid catching ``Exception``, catch more specific errors instead (https://github.com/ansible-collections/amazon.aws/pull/968). +- ec2_vpc_nat_gateway - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/968). diff --git a/plugins/action/s3_object.py b/plugins/action/s3_object.py index 91e98793f44..a78dd0bed15 100644 --- a/plugins/action/s3_object.py +++ b/plugins/action/s3_object.py @@ -56,7 +56,7 @@ def run(self, tmp=None, task_vars=None): try: source = self._loader.get_real_file(self._find_needle('files', source), decrypt=False) new_module_args['src'] = source - except AnsibleFileNotFound as e: + except AnsibleFileNotFound: # module handles error message for nonexistent files new_module_args['src'] = source except AnsibleError as e: diff --git a/plugins/module_utils/elbv2.py b/plugins/module_utils/elbv2.py index 5a654db9933..533fd75e23d 100644 --- a/plugins/module_utils/elbv2.py +++ b/plugins/module_utils/elbv2.py @@ -87,7 +87,7 @@ def _prune_ForwardConfig(action): # Remove the redundant ForwardConfig newAction = action.copy() - del(newAction["ForwardConfig"]) + del newAction["ForwardConfig"] newAction["TargetGroupArn"] = arn return newAction diff --git a/plugins/module_utils/waf.py b/plugins/module_utils/waf.py index fe4c306f4f6..226dca920b5 100644 --- a/plugins/module_utils/waf.py +++ b/plugins/module_utils/waf.py @@ -137,7 +137,7 @@ def get_rule(client, module, rule_id): if predicate['Type'] in match_sets: predicate.update(match_sets[predicate['Type']](client, predicate['DataId'])) # replaced by Id from the relevant MatchSet - del(predicate['DataId']) + del predicate['DataId'] return rule diff --git a/plugins/modules/aws_caller_info.py b/plugins/modules/aws_caller_info.py index b079285e73e..32b77d92486 100644 --- a/plugins/modules/aws_caller_info.py +++ b/plugins/modules/aws_caller_info.py @@ -94,7 +94,7 @@ def main(): caller_info['account_alias'] = response['AccountAliases'][0] else: caller_info['account_alias'] = '' - except (BotoCoreError, ClientError) as e: + except (BotoCoreError, ClientError): # The iam:ListAccountAliases permission is required for this operation to succeed. # Lacking this permission is handled gracefully by not returning the account_alias. pass diff --git a/plugins/modules/cloudformation.py b/plugins/modules/cloudformation.py index dd642b9d273..3b4d4312f32 100644 --- a/plugins/modules/cloudformation.py +++ b/plugins/modules/cloudformation.py @@ -399,7 +399,7 @@ def create_stack(module, stack_params, cfn, events_limit): response = cfn.create_stack(aws_retry=True, **stack_params) # Use stack ID to follow stack state in case of on_create_failure = DELETE result = stack_operation(module, cfn, response['StackId'], 'CREATE', events_limit, stack_params.get('ClientRequestToken', None)) - except Exception as err: + except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as err: module.fail_json_aws(err, msg="Failed to create stack {0}".format(stack_params.get('StackName'))) if not result: module.fail_json(msg="empty result") @@ -459,7 +459,7 @@ def create_changeset(module, stack_params, cfn, events_limit): 'NOTE that dependencies on this stack might fail due to pending changes!'] except is_boto3_error_message('No updates are to be performed.'): result = dict(changed=False, output='Stack is already up-to-date.') - except Exception as err: + except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as err: module.fail_json_aws(err, msg='Failed to create change set') if not result: @@ -482,7 +482,7 @@ def update_stack(module, stack_params, cfn, events_limit): result = stack_operation(module, cfn, stack_params['StackName'], 'UPDATE', events_limit, stack_params.get('ClientRequestToken', None)) except is_boto3_error_message('No updates are to be performed.'): result = dict(changed=False, output='Stack is already up-to-date.') - except Exception as err: + except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as err: module.fail_json_aws(err, msg="Failed to update stack {0}".format(stack_params.get('StackName'))) if not result: module.fail_json(msg="empty result") @@ -510,7 +510,7 @@ def stack_operation(module, cfn, stack_name, operation, events_limit, op_token=N try: stack = get_stack_facts(module, cfn, stack_name, raise_errors=True) existed.append('yes') - except Exception: + except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError): # If the stack previously existed, and now can't be found then it's # been deleted successfully. if 'yes' in existed or operation == 'DELETE': # stacks may delete fast, look in a few ways. @@ -783,7 +783,7 @@ def main(): cfn.delete_stack(aws_retry=True, StackName=stack_params['StackName'], RoleARN=stack_params['RoleARN']) result = stack_operation(module, cfn, stack_params['StackName'], 'DELETE', module.params.get('events_limit'), stack_params.get('ClientRequestToken', None)) - except Exception as err: + except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as err: module.fail_json_aws(err) module.exit_json(**result) diff --git a/plugins/modules/ec2_metadata_facts.py b/plugins/modules/ec2_metadata_facts.py index 91f7860eeaf..0fd7cb57dd5 100644 --- a/plugins/modules/ec2_metadata_facts.py +++ b/plugins/modules/ec2_metadata_facts.py @@ -524,11 +524,11 @@ def fetch(self, uri, recurse=True): self._data['%s' % (new_uri)] = sg_fields else: try: - dict = json.loads(content) + json_dict = json.loads(content) self._data['%s' % (new_uri)] = content - for (key, value) in dict.items(): + for (key, value) in json_dict.items(): self._data['%s:%s' % (new_uri, key.lower())] = value - except Exception: + except json.JSONDecodeError: self._data['%s' % (new_uri)] = content # not a stringified JSON string def fix_invalid_varnames(self, data): diff --git a/plugins/modules/ec2_security_group.py b/plugins/modules/ec2_security_group.py index edcfae04c7e..dccc2e075c0 100644 --- a/plugins/modules/ec2_security_group.py +++ b/plugins/modules/ec2_security_group.py @@ -759,7 +759,7 @@ def get_target_from_rule(module, client, rule, name, group, groups, vpc_id): # the model on their end. try: auto_group = get_security_groups_with_backoff(client, Filters=ansible_dict_to_boto3_filter_list(filters)).get('SecurityGroups', [])[0] - except IndexError as e: + except IndexError: module.fail_json(msg="Could not create or use existing group '{0}' in rule. Make sure the group exists".format(group_name)) except ClientError as e: module.fail_json_aws( diff --git a/plugins/modules/ec2_vpc_endpoint.py b/plugins/modules/ec2_vpc_endpoint.py index f8a744edea7..9c764f5d824 100644 --- a/plugins/modules/ec2_vpc_endpoint.py +++ b/plugins/modules/ec2_vpc_endpoint.py @@ -336,7 +336,7 @@ def create_vpc_endpoint(client, module): try: with open(module.params.get('policy_file'), 'r') as json_data: policy = json.load(json_data) - except Exception as e: + except (OSError, json.JSONDecodeError) as e: module.fail_json(msg=str(e), exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) diff --git a/plugins/modules/ec2_vpc_nat_gateway.py b/plugins/modules/ec2_vpc_nat_gateway.py index 49440e8fef0..e65ce2fd6b3 100644 --- a/plugins/modules/ec2_vpc_nat_gateway.py +++ b/plugins/modules/ec2_vpc_nat_gateway.py @@ -444,15 +444,15 @@ def get_eip_allocation_id_by_address(client, module, eip_address): else: allocation_id = allocation.get('AllocationId') - except is_boto3_error_code('InvalidAddress.Malformed') as e: + except is_boto3_error_code('InvalidAddress.Malformed'): module.fail_json(msg='EIP address {0} is invalid.'.format(eip_address)) - except is_boto3_error_code('InvalidAddress.NotFound') as e: # pylint: disable=duplicate-except + except is_boto3_error_code('InvalidAddress.NotFound'): # pylint: disable=duplicate-except msg = ( "EIP {0} does not exist".format(eip_address) ) allocation_id = None except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except - module.fail_json_aws(e) + module.fail_json_aws(e, msg="Unable to describe EIP") return allocation_id, msg