Create user in mongodb with ansible playbook

775 views
Skip to first unread message

Rayees Namathponnan

unread,
Oct 31, 2016, 8:57:19 AM10/31/16
to Ansible Project
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 

- 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


Reply all
Reply to author
Forward
0 new messages