---
#--------------------------------------------------------------#
# Check MongoDB                                    [mongo_check]
#--------------------------------------------------------------#
- name: assert mongo identity
  tags: mongo_check
  assert:
    that:
      - mongo_cluster is defined and mongo_cluster != ''
      - mongo_seq is defined and mongo_seq|int >= 0
      - mongo_pgurl is defined and mongo_pgurl != ''
    fail_msg: variable 'mongo_cluster' & 'mongo_seq' & 'mongo_pgurl' are required for mongo playbook


#--------------------------------------------------------------#
# MongoDB User                                      [mongo_user]
#--------------------------------------------------------------#
# - dbsu user and group - #
- name: create os user mongod
  tags: mongo_dbsu
  block:
    - name: create os group mongod
      group: name=mongod state=present
    - name: create os user mongod
      user: name=mongod home=/var/lib/mongod group=mongod


#--------------------------------------------------------------#
# Install MongoDB                                [mongo_install]
#--------------------------------------------------------------#
# FerretDB is a MongoDB wire protocol compatible database based on PostgreSQL.
- name: install mongo/ferretdb
  tags: mongo_install
  environment: "{{ proxy_env | default({}) }}"
  package: name=ferretdb2 state=present


#--------------------------------------------------------------#
# Purge MongoDB                                    [mongo_purge]
#--------------------------------------------------------------#
# purge with ./mongo.yml -e mongo_purge=true -t mongo_purge
- name: purge mongo/ferretdb cluster
  tags: mongo_purge
  ignore_errors: true
  when: mongo_purge is defined and mongo_purge|bool
  block:

    - name: shutdown mongo/ferretdb server service
      systemd: name=ferretdb state=stopped enabled=no daemon_reload=yes

    - name: purge mongo/ferretdb systemd service
      file:
        path: "{{ systemd_dir }}/ferretdb.service"
        state: absent

    - name: purge mongo/ferretdb config file
      file: path="{{ item }}" state=absent
      loop: [ /etc/default/ferretdb, /var/lib/mongod/ca.crt, /var/lib/mongod/server.crt, /var/lib/mongod/server.key ]

    - name: remove mongo/ferret register from victoria target
      tags: [ mongo_deregister, deregister , rm_metrics ]

      delegate_to: '{{ item }}'
      loop: '{{ groups["infra"]|default([]) }}'
      file:
        path: "/infra/targets/mongo/{{ mongo_cluster }}-{{ mongo_seq }}.yml"
        state: absent


#--------------------------------------------------------------#
# Config MongoDB                                  [mongo_config]
#--------------------------------------------------------------#
- name: render mongo/ferretdb config
  tags: mongo_config
  block:

    - name: render mongo/ferretdb config
      template:
        src: ferretdb.env
        dest: /etc/default/ferretdb
        owner: root
        group: mongod
        mode: 0644

    - name: create mongo/ferretdb systemd service
      template:
        src: ferretdb.svc
        dest: "{{ systemd_dir }}/ferretdb.service"


#--------------------------------------------------------------#
# Config MongoDB SSL Certs                          [mongo_cert]
#--------------------------------------------------------------#
- import_tasks: cert.yml
  when: mongo_ssl_enabled|bool
  tags: [ mongo_config, mongo_cert ]
  vars: { mongo_instance: "{{ mongo_cluster }}-{{ mongo_seq }}" }


#--------------------------------------------------------------#
# Launch MongoDB                                  [mongo_launch]
#--------------------------------------------------------------#
- name: launch mongo/ferretdb server
  tags: mongo_launch
  block:
    - name: launch mongo/ferretdb server service
      systemd: name=ferretdb state=restarted enabled=yes daemon_reload=yes
    - name: wait for mongo/ferretdb server online
      wait_for: host=127.0.0.1 port={{ mongo_port|default('27017') }} state=started timeout=10


#--------------------------------------------------------------#
# Register MongoDB                              [mongo_register]
#--------------------------------------------------------------#
- name: register mongo/ferretdb as victoria target
  tags: [ mongo_register, register , add_metrics ]
  ignore_errors: true
  delegate_to: '{{ item }}'
  loop: '{{ groups["infra"]|default([]) }}'
  copy:
    dest: "/infra/targets/mongo/{{ mongo_cluster }}-{{ mongo_seq }}.yml"
    owner: victoria
    group: infra
    mode: '0640'
    content: |
      # {{ mongo_cluster }}-{{ mongo_seq }} @ {{ inventory_hostname }}
      - labels: { ip: {{ inventory_hostname }} , ins: {{ mongo_cluster }}-{{ mongo_seq }} , cls: {{ mongo_cluster }} }
        targets: [ {{ inventory_hostname }}:{{ mongo_exporter_port }} ]

...