---
- name: Prod Environment Bootstrap (Common Roles)
  hosts: all
  become: yes
  roles:
    - role: base
      tags: [base]
    - role: hardening
      tags: [hardening]
    - role: docker
      tags: [docker]
    - role: node_dirs
      tags: [node_dirs]
    - role: storagebox
      tags: [storagebox]
    - role: storagebox_ssh_key
      tags: [storagebox_ssh_key]

- name: Swarm Infrastructure Setup (Prod HA)
  hosts: iklim-app-*
  become: yes
  serial: 1
  roles:
    - role: swarm
      tags: [swarm]

# Prod'da DB node'ları da worker olarak swarm'a katılır
- name: DB Nodes Swarm Join
  hosts: iklim-db-*
  become: yes
  roles:
    - role: swarm
      tags: [swarm]

# db-index label'ları Patroni node koordinasyonu için gereklidir; Swarm join tamamlandıktan sonra çalışır
- name: Add db-index Labels for Patroni
  hosts: iklim-app-01
  become: yes
  tags: [db_labels]
  tasks:
    - name: Label DB nodes with db-index
      ansible.builtin.command: >
        docker node update --label-add db-index={{ item.index }} {{ item.node }}
      loop:
        - { node: "iklim-db-01", index: "01" }
        - { node: "iklim-db-02", index: "02" }
        - { node: "iklim-db-03", index: "03" }
      ignore_errors: true

- name: DB Node Configuration (MongoDB Config)
  hosts: iklim-db-*
  become: yes
  roles:
    - role: db_stack
      tags: [db_stack]

- name: Act Runner Setup (App Nodes)
  hosts: iklim-app-*
  become: yes
  roles:
    - role: act_runner
      tags: [act_runner]
