---
- name: Restart mongodb container
  vars:
    service_name: "mongodb"
    service: "{{ mongodb_services[service_name] }}"
    config_json: "{{ mongodb_config_jsons.results|selectattr('item.key', 'equalto', service_name)|first }}"
    mongodb_conf: "{{ mongodb_confs.results|selectattr('item.key', 'equalto', service_name)|first }}"
    mongodb_container: "{{ check_mongodb_containers.results|selectattr('item.key', 'equalto', service_name)|first }}"
  become: true
  kolla_docker:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ service.container_name }}"
    image: "{{ service.image }}"
    privileged: "{{ service.privileged | default(False) }}"
    volumes: "{{ service.volumes }}"
    dimensions: "{{ service.dimensions }}"
  when:
    - kolla_action != "config"
    - inventory_hostname in groups[service.group]
    - service.enabled | bool
    - config_json.changed | bool
      or mongodb_conf.changed | bool
      or mongodb_container.changed | bool
  notify:
    - Waiting for the mongodb startup
    - Checking current replication status

- name: Waiting for the mongodb startup
  wait_for: host={{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }} port={{ mongodb_port }}

- name: Checking current replication status
  command: "docker exec -t mongodb mongo {{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }} --quiet --eval rs.status().ok"
  register: mongodb_replication_status
  changed_when: false
  delegate_to: "{{ groups['mongodb'][0] }}"
  run_once: True

- include: "bootstrap_cluster.yml"
  when: mongodb_replication_status.stdout != "1"
