From 46a6c28bb01bcd4d53263d1d94452016804bbae6 Mon Sep 17 00:00:00 2001 From: Youhua Li Date: Wed, 13 Nov 2019 20:07:35 -0800 Subject: [PATCH] use ~/.one/one_auth as default of ONE_AUTH file location (#64535) * better excpetion handling when one_auth file is missing * use Exception instead of BaseException --- .../modules/cloud/opennebula/one_vm.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/cloud/opennebula/one_vm.py b/lib/ansible/modules/cloud/opennebula/one_vm.py index 14c0b5feb6a..521dfe74295 100644 --- a/lib/ansible/modules/cloud/opennebula/one_vm.py +++ b/lib/ansible/modules/cloud/opennebula/one_vm.py @@ -1300,15 +1300,16 @@ def get_connection_info(module): if not username: if not password: authfile = os.environ.get('ONE_AUTH') - if authfile is not None: - try: - authstring = open(authfile, "r").read().rstrip() - username = authstring.split(":")[0] - password = authstring.split(":")[1] - except BaseException: - module.fail_json(msg="Could not read ONE_AUTH file") - else: - module.fail_json(msg="No Credentials are set") + if authfile is None: + authfile = os.path.join(os.environ.get("HOME"), ".one", "one_auth") + try: + authstring = open(authfile, "r").read().rstrip() + username = authstring.split(":")[0] + password = authstring.split(":")[1] + except (OSError, IOError): + module.fail_json(msg=("Could not find or read ONE_AUTH file at '%s'" % authfile)) + except Exception: + module.fail_json(msg=("Error occurs when read ONE_AUTH file at '%s'" % authfile)) if not url: module.fail_json(msg="Opennebula API url (api_url) is not specified") from collections import namedtuple