From 453a6f40477a9f573fefffe7602a3c6cfe65d2e6 Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Wed, 6 Jun 2018 16:55:20 -0400 Subject: [PATCH] Improve error message if policy_document does not exist (#40094) --- lib/ansible/modules/cloud/amazon/iam_policy.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/iam_policy.py b/lib/ansible/modules/cloud/amazon/iam_policy.py index d45dafabda5..7b31d400f57 100644 --- a/lib/ansible/modules/cloud/amazon/iam_policy.py +++ b/lib/ansible/modules/cloud/amazon/iam_policy.py @@ -293,13 +293,21 @@ def main(): policy_name = module.params.get('policy_name') skip = module.params.get('skip_duplicates') - if module.params.get('policy_document') is not None and module.params.get('policy_json') is not None: + policy_document = module.params.get('policy_document') + if policy_document is not None and module.params.get('policy_json') is not None: module.fail_json(msg='Only one of "policy_document" or "policy_json" may be set') - if module.params.get('policy_document') is not None: - with open(module.params.get('policy_document'), 'r') as json_data: - pdoc = json.dumps(json.load(json_data)) - json_data.close() + if policy_document is not None: + try: + with open(policy_document, 'r') as json_data: + pdoc = json.dumps(json.load(json_data)) + json_data.close() + except IOError: + if e.errno == 2: + module.fail_json( + msg='policy_document {0:!r} does not exist'.format(policy_document)) + else: + raise elif module.params.get('policy_json') is not None: pdoc = module.params.get('policy_json') # if its a string, assume it is already JSON