---

- name: Configure mongodb
  template: src=mongod.conf.j2 dest=/etc/mongod.conf owner=root group=root mode=0644
  notify: mongodb restart

- name: Configure log rotation
  template: src=logrotate.conf.j2 dest=/etc/logrotate.d/mongodb.conf
  when: mongodb_logrotate

- name: Create mongodb user
  user: name={{mongodb_user}} group={{mongodb_user}}

- name: Configure database directory
  file: state=directory path={{ mongodb_conf_dbpath }} owner={{mongodb_user}} group={{mongodb_user}} mode=0755

- name: Configure log directory
  file: state=directory path={{ mongodb_conf_logpath | dirname }} owner={{mongodb_user}} group={{mongodb_user}} mode=0755

- name: Run mongoshell commands
  command: mongo {{ item.key }} --eval "{{ item.value|join('\n') }}"
  with_dict: mongodb_shell

- name: Ensure mongodb is started
  service: name={{ mongodb_daemon_name }} state=started enabled=yes
  changed_when: False

---

- name: Configure mongodb
  template: src=mongod.conf.j2 dest=/etc/mongod.conf owner=root group=root mode=0644
  notify: mongodb restart

- name: Configure log rotation
  template: src=logrotate.conf.j2 dest=/etc/logrotate.d/mongodb.conf
  when: mongodb_logrotate

- name: Create mongodb user
  user: name={{mongodb_user}} group={{mongodb_user}}

- name: Configure database directory
  file: state=directory path={{ mongodb_conf_dbpath }} owner={{mongodb_user}} group={{mongodb_user}} mode=0755

- name: Configure log directory
  file: state=directory path={{ mongodb_conf_logpath | dirname }} owner={{mongodb_user}} group={{mongodb_user}} mode=0755

- name: Ensure mongodb is started
  service: name={{ mongodb_daemon_name }} state=started enabled=yes
  changed_when: False

- name: Flush handlers to restart mongodb if necessary
  meta: flush_handlers

- name: Run mongoshell commands
  command: mongo {{ item.key }} --eval "{{ item.value|join('\n') }}"
  with_dict: mongodb_shell

Make sure mongodb is restarted before shell commands are executed
Make sure mongodb is restarted before shell commands are executed

This changes the order of the tasks executed for configuring mongodb.
Make sure mongodb is enabled and started before any shell commands are
executed.  Additionally, flush handlers before executing shell commands.
This makes sure that mongodb was restarted if necessary before any shell
commands are executed.
