From 8d00ccf60fe8c5c6d10313c8cf4fa9007b9b0686 Mon Sep 17 00:00:00 2001 From: Rhys Campbell Date: Mon, 11 Mar 2019 10:58:39 +0100 Subject: [PATCH] Change listDatabases exception handling (#53605) --- .../modules/database/mongodb/mongodb_replicaset.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/database/mongodb/mongodb_replicaset.py b/lib/ansible/modules/database/mongodb/mongodb_replicaset.py index a8ed2a0bb43..13cb02d276e 100644 --- a/lib/ansible/modules/database/mongodb/mongodb_replicaset.py +++ b/lib/ansible/modules/database/mongodb/mongodb_replicaset.py @@ -376,11 +376,13 @@ def main(): try: client['admin'].command('listDatabases', 1.0) # if this throws an error we need to authenticate except Exception as excep: - if "not authorized on" not in str(excep) and "command listDatabases requires authentication" not in str(excep): + if "not authorized on" in str(excep) or "command listDatabases requires authentication" in str(excep): + if login_user is not None and login_password is not None: + client.admin.authenticate(login_user, login_password, source=login_database) + else: + raise excep + else: raise excep - if login_user is None or login_password is None: - raise excep - client.admin.authenticate(login_user, login_password, source=login_database) if len(replica_set) == 0: module.fail_json(msg="Parameter 'replica_set' must not be an empty string")