# Create user in mongodb with ansible playbook

**URL:** <https://forum.ansible.com/t/create-user-in-mongodb-with-ansible-playbook/22917>\
**Category:** Ansible Project\
**Created:** [October 28, 2016, 11:09pm UTC](https://forum.ansible.com/t/create-user-in-mongodb-with-ansible-playbook/22917 "2016-10-28T23:09:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rayees\_Namathponnan](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@Rayees\_Namathponnan](https://forum.ansible.com/u/Rayees_Namathponnan)\
**Post date:** [October 28, 2016, 11:09pm UTC](https://forum.ansible.com/t/create-user-in-mongodb-with-ansible-playbook/22917/1 "2016-10-28T23:09:21Z")

</div>

Hi All,

I am using below ansible script to create admin user in mongoDB,

`

- name: Enable at boot and start mongodb  
service: name=mongod.service enabled=yes state=started

- name: Add the admin user to the replicaset  
mongodb\_user: database=admin name=admin password={{ mongo\_admin\_pass }} login\_port={{ mongod\_port }} roles=‘root’ state=present  
ignore\_errors: yes

- name: Create the file to initialize the replicaset  
template: src=repset\_init.j2 dest=/tmp/repset\_init.js

- name: Initialize the replication set on the primary  
shell: /usr/bin/mongo -u admin -p {{ mongo\_admin\_pass }} --authenticationDatabase admin /tmp/repset\_init.js  
run\_once: true

`

After creating admin user i want to create normal db user also, example user “gray” with permission to db graylog, see the script below, but its not allowing to create second user

`

```auto
- name: Create the unauthenticated mongodb configuration file
  template: src=mongod_unauthenticated.conf.j2 dest=/etc/mongod.conf

- name: Enable at boot and start mongodb
  service: name=mongod.service enabled=yes state=started

- name: Add the admin user to the replicaset
  mongodb_user: database=admin name=admin password={{ mongo_admin_pass }} login_port={{ mongod_port }} roles='root' state=present
  ignore_errors: yes

- name: Add the graylog user to the replicaset
  mongodb_user: database=graylog name=gray password=gray login_port={{ mongod_port }} roles='dbOwner' state=present
  ignore_errors: yes

- name: Create the file to initialize the replicaset
  template: src=repset_init.j2 dest=/tmp/repset_init.js

- name: Initialize the replication set on the primary
  shell: /usr/bin/mongo -u admin -p {{ mongo_admin_pass }} --authenticationDatabase admin /tmp/repset_init.js
  run_once: true

```

`
