---
- name: Converge
  hosts: all
  become: yes

  vars:
    admin_user: "admin"
    admin_user_password: "admin"

  roles:
    - role: mongodb_repository
      tags: molecule-idempotence-notest
    - role: mongodb_install
      when: "'config' in ansible_hostname"
      tags: molecule-idempotence-notest
    - role: mongodb_config
      when: "'config' in ansible_hostname"
      tags: molecule-idempotence-notest
    - role: mongodb_mongos
      when: "'config' not in ansible_hostname"

  tasks:

    - name: Install python stuff
      package:
        name: ["python-setuptools", "python-pip"]
      when: ansible_hostname == "config1"

    - name: Install pymongo
      pip:
        name: pymongo
      when: ansible_hostname == "config1"

    - name: Install MongoDB Shell
      package:
        name: mongodb-org-shell

    - name: Init config server replicaset
      community.mongodb.mongodb_replicaset:
        login_host: localhost
        login_port: "27019"
        replica_set: cfg
        validate: no
        members:
          - config1:27019
      when: ansible_hostname == "config1"

    - name: Restart the mongos service
      service:
        name: mongos
        state: started
      when: "'config' not in ansible_hostname"

    - name: Add MongoDB Admin User using localhost exception on the mongos
      community.mongodb.mongodb_user:
        name: "{{ admin_user }}"
        password: "{{ admin_user_password }}"
        login_port: "27019"
        database: admin
        roles:
          - "root"
        create_for_localhost_exception: /root/mongodb_admin.success
      when: ansible_hostname == "config1"
