mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-04 07:30:20 -07:00
Merge pull request #5597 from laggyluke/mongodb_user_idempotence
Fix idempotence in mongodb_user module
This commit is contained in:
commit
aaaf9dbc43
1 changed files with 21 additions and 12 deletions
|
@ -188,19 +188,28 @@ def main():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client = MongoClient(login_host, int(login_port))
|
client = MongoClient(login_host, int(login_port))
|
||||||
if login_user is None and login_password is None:
|
|
||||||
mongocnf_creds = load_mongocnf()
|
|
||||||
if mongocnf_creds is not False:
|
|
||||||
login_user = mongocnf_creds['user']
|
|
||||||
login_password = mongocnf_creds['password']
|
|
||||||
elif login_password is None and login_user is not None:
|
|
||||||
module.fail_json(msg='when supplying login arguments, both login_user and login_password must be provided')
|
|
||||||
|
|
||||||
if login_user is not None and login_password is not None:
|
|
||||||
client.admin.authenticate(login_user, login_password)
|
|
||||||
|
|
||||||
except ConnectionFailure, e:
|
except ConnectionFailure, e:
|
||||||
module.fail_json(msg='unable to connect to database, check login_user and login_password are correct')
|
module.fail_json(msg='unable to connect to database, check login_host and login_port are correct')
|
||||||
|
|
||||||
|
# try to authenticate as a target user to check if it already exists
|
||||||
|
try:
|
||||||
|
client[db_name].authenticate(user, password)
|
||||||
|
if state == 'present':
|
||||||
|
module.exit_json(changed=False, user=user)
|
||||||
|
except OperationFailure:
|
||||||
|
if state == 'absent':
|
||||||
|
module.exit_json(changed=False, user=user)
|
||||||
|
|
||||||
|
if login_user is None and login_password is None:
|
||||||
|
mongocnf_creds = load_mongocnf()
|
||||||
|
if mongocnf_creds is not False:
|
||||||
|
login_user = mongocnf_creds['user']
|
||||||
|
login_password = mongocnf_creds['password']
|
||||||
|
elif login_password is None and login_user is not None:
|
||||||
|
module.fail_json(msg='when supplying login arguments, both login_user and login_password must be provided')
|
||||||
|
|
||||||
|
if login_user is not None and login_password is not None:
|
||||||
|
client.admin.authenticate(login_user, login_password)
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
if password is None:
|
if password is None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue