From bfb1ad6690b9758c6395ac28791d4f262ff58f10 Mon Sep 17 00:00:00 2001 From: THEBAULT Julien Date: Tue, 27 Sep 2016 16:05:54 +0200 Subject: [PATCH] Fix mongodb user compatibility check (#2731) - Check the compatibility asap --- .../extras/database/misc/mongodb_user.py | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/ansible/modules/extras/database/misc/mongodb_user.py b/lib/ansible/modules/extras/database/misc/mongodb_user.py index 4d628ef2202..651e45a082e 100644 --- a/lib/ansible/modules/extras/database/misc/mongodb_user.py +++ b/lib/ansible/modules/extras/database/misc/mongodb_user.py @@ -168,13 +168,26 @@ else: # def check_compatibility(module, client): - srv_info = client.server_info() - if LooseVersion(srv_info['version']) >= LooseVersion('3.2') and LooseVersion(PyMongoVersion) <= LooseVersion('3.2'): + """Check the compatibility between the driver and the database. + + See: https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#python-driver-compatibility + + Args: + module: Ansible module. + client (cursor): Mongodb cursor on admin database. + """ + loose_srv_version = LooseVersion(client.server_info()['version']) + loose_driver_version = LooseVersion(PyMongoVersion) + + if loose_srv_version >= LooseVersion('3.2') and loose_driver_version <= LooseVersion('3.2'): module.fail_json(msg=' (Note: you must use pymongo 3.2+ with MongoDB >= 3.2)') - elif LooseVersion(srv_info['version']) >= LooseVersion('3.0') and LooseVersion(PyMongoVersion) <= LooseVersion('2.8'): + + elif loose_srv_version >= LooseVersion('3.0') and loose_driver_version <= LooseVersion('2.8'): module.fail_json(msg=' (Note: you must use pymongo 2.8+ with MongoDB 3.0)') - elif LooseVersion(srv_info['version']) >= LooseVersion('2.6') and LooseVersion(PyMongoVersion) <= LooseVersion('2.7'): + + elif loose_srv_version >= LooseVersion('2.6') and loose_srv_version <= LooseVersion('2.7'): module.fail_json(msg=' (Note: you must use pymongo 2.7+ with MongoDB 2.6)') + elif LooseVersion(PyMongoVersion) <= LooseVersion('2.5'): module.fail_json(msg=' (Note: you must be on mongodb 2.4+ and pymongo 2.5+ to use the roles param)') @@ -332,6 +345,10 @@ def main(): client = MongoClient(**connection_params) + # NOTE: this check must be done ASAP. + # We doesn't need to be authenticated. + check_compatibility(module, client) + if login_user is None and login_password is None: mongocnf_creds = load_mongocnf() if mongocnf_creds is not False: @@ -350,8 +367,6 @@ def main(): except Exception, e: module.fail_json(msg='unable to connect to database: %s' % str(e)) - check_compatibility(module, client) - if state == 'present': if password is None and update_password == 'always': module.fail_json(msg='password parameter required when adding a user unless update_password is set to on_create')