---

# Parameters:
#   playbook inventory
#     stage := the name of the stage (e.g. dev, int, qa, prod)
#     tenant := object with tenant related data
#       key := 
#     cluster := object with cluster specific data (optional)
#       key := 
#       size := 
#       service := 
#       features := 
#     data := object with action specific data (optional)
#       ...

#############################################################
#   Creating inventory dynamically for given cluster
#############################################################

- hosts: localhost
  gather_facts: false
  connection: local

  pre_tasks:
    - name: "Check if ansible version is at least {{ ansible_minimal_version }}"
      assert:
        that:
          - ansible_version.string is version(ansible_minimal_version, ">=")
        msg: "The ansible version has to be at least {{ ansible_minimal_version }}"

#     add virtual server to load stage specific variables as context
    - name: "Add <{{ stage }}-virtual-host-to-read-groups-vars> to hosts"
      add_host:
        name: "{{ stage }}-virtual-host-to-read-groups-vars"
        groups:
        - "stage_{{ stage }}"
      changed_when: False

  tasks:
    - name: "Add postgres servers to hosts if necessary"
      add_host:
        name: "{{ stage }}-postgres-01"
        groups:
        - "{{ item }}"
        - "stage_{{ stage }}"
      changed_when: False
      with_items: "{{ ['hcloud'] + ['stage_' + stage ] + [cluster.service] + (cluster.features | default([])) }}"
      when: item in ['connect', 'management_connect', 'keycloak', 'webdav', 'gitea', 'pdns']

    - name: "Add maria servers to hosts if necessary"
      add_host:
        name: "{{ stage }}-maria-01"
        groups:
        - "{{ item }}"
        - "stage_{{ stage }}"
      changed_when: False
      with_items: "{{ ['hcloud'] + ['stage_' + stage ] + [cluster.service] + (cluster.features | default([])) }}"
      when: item in ['connect_wordpress']
