ansible-role-mongodb/tasks/auth_initialization.yml

76 lines
2.3 KiB
YAML

---
- name: Move back mongod.conf
template: src=mongod_init.conf.j2 dest=/etc/mongod.conf owner=root group=root mode=0644
- name: Restart mongodb service
service: name={{ mongodb_daemon_name }} state=restarted
when: mongodb_manage_service
- name: wait MongoDB port is listening
wait_for: host=127.0.0.1 port="{{ mongodb_conf_port }}" delay=5 state=started
- name: get pid of mongodb for non daemon mode
shell: "pidof mongod"
register: pidof_mongod
when: mongodb_manage_service == false
ignore_errors: yes
- name: start mongodb daemon
shell: "LC_ALL=C /usr/bin/mongod --config /etc/mongod.conf --fork"
when: mongodb_manage_service == false and pidof_mongod.rc == 1
- name: create administrative user siteUserAdmin
mongodb_user:
database: admin
name: "{{ item.name }}"
password: "{{ item.password }}"
roles: "{{ item.roles }}"
login_port: "{{ mongodb_conf_port }}"
with_items:
- {
name: "{{ mongodb_user_admin_name }}",
password: "{{ mongodb_user_admin_password }}",
roles: "userAdminAnyDatabase"
}
- name: create administrative user siteRootAdmin
mongodb_user:
database: admin
name: "{{ item.name }}"
password: "{{ item.password }}"
roles: "{{ item.roles }}"
login_port: "{{ mongodb_conf_port }}"
with_items:
- {
name: "{{ mongodb_root_admin_name }}",
password: "{{ mongodb_root_admin_password }}",
roles: "root"
}
- name: create normal users
mongodb_user:
database: "{{ item.database }}"
name: "{{ item.name }}"
password: "{{ item.password }}"
roles: "{{ item.roles }}"
login_user: "{{ mongodb_user_admin_name }}"
login_password: "{{ mongodb_user_admin_password }}"
login_port: "{{ mongodb_conf_port }}"
with_items:
- "{{ mongodb_users }}"
when: mongodb_users is defined and mongodb_users
- name: Move back mongod.conf
template: src=mongod.conf.j2 dest=/etc/mongod.conf owner=root group=root mode=0644
- name: Restart mongodb service
service: name={{ mongodb_daemon_name }} state=restarted
when: mongodb_manage_service
- name: wait MongoDB port is listening
wait_for: host="{{ mongodb_conf_bind_ip }}" port="{{ mongodb_conf_port }}" delay=5 state=started
- name: stop mongodb if was not started
shell: "kill {{ pidof_mongod.stdout }}"
when: mongodb_manage_service == false and pidof_mongod.rc == 0