Repository: ansible/ansible

Issue:
`mongodb_user` module doesn't allow user to be added via localhost exception
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
`mongodb_user`

##### ANSIBLE VERSION
```
ansible 2.2.0.0
```

##### OS / ENVIRONMENT
Ubuntu 16.04 (master and target)
MongoDB 3.0.14
PyMongo 3.0.3

##### SUMMARY
When [auth](https://docs.mongodb.com/manual/tutorial/enable-authentication/) is enabled, MongoDB allows the first user to be created from localhost (called the [localhost exception](https://docs.mongodb.com/v3.2/core/security-users/#localhost-exception)). This fails when I attempt to add the first user with the `mongodb_user` module.

However, I _am_ able to successfully add a user from the Python shell with the following code:

```
from pymongo import MongoClient
client = MongoClient(host="127.0.0.1", port=27017)
db = client['admin']
db.add_user('admin', 'hello', roles=['root'])
```

##### STEPS TO REPRODUCE
- Enable auth by adding the following to `/etc/mongod.conf`:
```
security:
  authorization: enabled
```
- Restart mongod and completely nuke the database with `service mongod stop && rm -rf /var/mongo/mongodb/* && sleep 2 && service mongod start`

- Run this task:
```yaml
- name: create super user
  mongodb_user:
    name: "admin"
    password: "hello"
    database: "admin"
    roles: root
```

##### EXPECTED RESULTS
The task should succeed and a root user should be created.

##### ACTUAL RESULTS
I get the following error when the task runs:
```
TASK [mongo : create super user] **********************************
fatal: [104.155.154.237]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to add or update user: command SON([('createUser', 'admin'), ('pwd', u'f890e3e7958aae1dbfff364ce10af0fc'), ('digestPassword', False), ('roles', ['root'])]) on namespace admin.$cmd failed: not authorized on admin to execute command { createUser: \"admin\", pwd: \"xxx\", digestPassword: false, roles: [ \"root\" ] }"}
```

##### PATCH
https://github.com/ansible/ansible/pull/22792


Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/database/mongodb/mongodb_user.py b/lib/ansible/modules/database/mongodb/mongodb_user.py
--- a/lib/ansible/modules/database/mongodb/mongodb_user.py
+++ b/lib/ansible/modules/database/mongodb/mongodb_user.py
@@ -431,11 +431,12 @@ def main():
             module.fail_json(msg='password parameter required when adding a user unless update_password is set to on_create')
 
         try:
-            uinfo = user_find(client, user, db_name)
-            if update_password != 'always' and uinfo:
-                password = None
-                if not check_if_roles_changed(uinfo, roles, db_name):
-                    module.exit_json(changed=False, user=user)
+            if update_password != 'always':
+                uinfo = user_find(client, user, db_name)
+                if uinfo:
+                    password = None
+                    if not check_if_roles_changed(uinfo, roles, db_name):
+                        module.exit_json(changed=False, user=user)
 
             if module.check_mode:
                 module.exit_json(changed=True, user=user)


Test patch: