From 25181e1b7021430f495f249dc3b9adf37ae3afd3 Mon Sep 17 00:00:00 2001 From: Lujeni Date: Sat, 15 Feb 2020 14:10:27 +0000 Subject: [PATCH] mongodb_user: Ensure to close database connection (closes #59353) (#65665) --- lib/ansible/modules/database/mongodb/mongodb_user.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/database/mongodb/mongodb_user.py b/lib/ansible/modules/database/mongodb/mongodb_user.py index ce4d150cead..e504bebc855 100644 --- a/lib/ansible/modules/database/mongodb/mongodb_user.py +++ b/lib/ansible/modules/database/mongodb/mongodb_user.py @@ -424,7 +424,11 @@ def main(): user_add(module, client, db_name, user, password, roles) except Exception as e: module.fail_json(msg='Unable to add or update user: %s' % to_native(e), exception=traceback.format_exc()) - + finally: + try: + client.close() + except Exception: + pass # Here we can check password change if mongo provide a query for that : https://jira.mongodb.org/browse/SERVER-22848 # newuinfo = user_find(client, user, db_name) # if uinfo['role'] == newuinfo['role'] and CheckPasswordHere: @@ -435,7 +439,11 @@ def main(): user_remove(module, client, db_name, user) except Exception as e: module.fail_json(msg='Unable to remove user: %s' % to_native(e), exception=traceback.format_exc()) - + finally: + try: + client.close() + except Exception: + pass module.exit_json(changed=True, user=user)