Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

mayorgatellez
Copy link

@mayorgatellez mayorgatellez commented Sep 6, 2025

SUMMARY

Fixes (#711).

In plugins/modules/mongodb_user.py, we have the following function call:

client = get_mongodb_client(module, directConnection=directConnection)

when it should be

client = get_mongodb_client(module, login_user, login_password, login_database, directConnection=directConnection)

Then, the values of the username and password needed to authenticate in the database are passed to MongoClient() as None values. As a consequence, the connection to the database fails.

This results in the error message, "Unable to connect to database: SCRAM-SHA-256 requires a username".

To make the function more robust, grab the login_user, login_password, and login_database from module.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

plugins/modules/mongodb_user.py

ADDITIONAL INFORMATION

Before:

TASK [Add MongoDB users from list of users as "user"] ******************************************************************************
failed: [dev] (item={'name': 'new.user', 'state': 'present', 'roles': [{'role': 'read', 'db': 'local'}, {'role': 'userAdminAnyDatabase', 'db': 'admin'}]}) => {"ansible_loop_var": "item", "changed": false, "item": {"name": "new.user", "roles": [{"db": "local", "role": "read"}, {"db": "admin", "role": "userAdminAnyDatabase"}], "state": "present"}, "msg": "Unable to connect to database: SCRAM-SHA-256 requires a username"}

After:

TASK [Add MongoDB users from list of users as "user"] ******************************************************************************
changed: [dev] => (item={'name': 'new.user', 'state': 'present', 'roles': [{'role': 'read', 'db': 'local'}, {'role': 'userAdminAnyDatabase', 'db': 'admin'}]})

In plugins/modules/mongodb_user.py, we have the following function call:

client = get_mongodb_client(module, directConnection=directConnection)

when it should be

client = get_mongodb_client(module, login_user, login_password, login_database, directConnection=directConnection)

Then, the values of the username as password needed to authenticate in the database are passed to MongoClient() as None values. As a consequence, the connection to the database fails.

This results in the error message, "Unable to connect to database: SCRAM-SHA-256 requires a username".

To make the function more robust, grab the login_user, login_password, and login_database from module.
@rhysmeister
Copy link
Collaborator

Hello @mayorgatellez,

Authentication is always performed in the mongo_auth function. Most of the modules use the following flow to connect to MongoDB...

    try:
        client = get_mongodb_client(module, directConnection=True)
        client = mongo_auth(module, client, directConnection=True)
    except Exception as excep:
        module.fail_json(msg='Unable to connect to MongoDB: %s' % to_native(excep))

This flow has basically evolved from MongoDB 3 and onwards. If you're having an issue here I think the problem lies elsewhere.

Rhys

@rhysmeister
Copy link
Collaborator

@mayorgatellez rebasing this should fix the CI. ansible-dev recently pushed to >= 3.12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants