---

# Parameters:
#   playbook inventory
#     stage := the name of the stage (e.g. dev, int, qa, prod)
#     tenant_id := (unique key for the tenant, e.g. customer)
#     cluster_name := (business name for the cluster, e.g. product, department )
#     cluster_size := (WIP node count for the cluster)
#     cluster_service := (service to setup, e.g. 'connect', ...)
#   smardigo message callback
#     scope_id := (scope id of the management process)
#     process_instance_id := (process instance id of the management process)
#     smardigo_management_action := (smardigo management action anme of the management process)

#############################################################
#   Creating inventory dynamically for given parameters
#############################################################

- hosts: localhost
  gather_facts: false
  connection: local

  pre_tasks:
    - name: "Check if ansible version is at least 2.10.x"
      assert:
        that:
          - ansible_version.major >= 2
          - ansible_version.minor >= 10
        msg: "The ansible version has to be at least ({{ ansible_version.full }})"

#     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 hosts
      add_host:
        name: "{{ stage }}-{{ tenant_id }}-{{ cluster_name }}-{{ '%02d' | format(item|int) }}"
        groups:
        - "stage_{{ stage }}"
        - "{{ cluster_service }}"
        - hcloud
      with_sequence: start=1 end={{ cluster_size | default(1) }}
      changed_when: False

#############################################################
#   Creating servers for created inventory
#############################################################

- hosts: "stage_{{ stage }}:!{{ stage }}-virtual-host-to-read-groups-vars"
  serial: "{{ serial_number | default(5) }}"
  remote_user: root
  gather_facts: false

  pre_tasks:
    - name: Get all Firewalls from Hetzner
      uri:
        url: "https://api.hetzner.cloud/v1/firewalls"
        headers:
          accept: application/json
          authorization: Bearer {{ hetzner_authentication_ansible }}
        return_content: yes
      register: hetzner_firewalls_response
      delegate_to: 127.0.0.1
      tags:
        - update_networks

    - name: Save firewall entries as variable (fact)
      set_fact:
        hetzner_firewalls_response_json: "{{ hetzner_firewalls_response.json }}"
      tags:
        - update_networks

    - name: Parse firewall entries
      set_fact:
        firewall_records: "{{ hetzner_firewalls_response_json.firewalls | json_query(jmesquery) }}"
      vars:
        jmesquery: '[*].{id: id, name: name}'
      tags:
        - update_networks

    - name: Print firewall entries
      debug:
        msg: "{{ firewall_records }}"
      tags:
        - update_networks

  roles:
    - role: hcloud

#############################################################
#   Provisioning servers for created inventory
#############################################################

- hosts: "stage_{{ stage }}:!{{ stage }}-virtual-host-to-read-groups-vars"
  serial: "{{ serial_number | default(1) }}"
  remote_user: root
  vars:
    ansible_ssh_host: "{{ stage_server_domain }}"

  pre_tasks:
    - name: Remove outdated dependencies
      apt:
        name: [
          'docker',
          'docker-client',
          'docker-client-latest',
          'docker-common',
          'docker-latest',
          'docker-latest-logrotate',
          'docker-logrotate',
          'docker-engine',
          'smartmontools',
        ]
        state: 'absent'
      when: ansible_distribution == "Ubuntu"

    - name: "Import autodiscover pre-tasks"
      import_tasks: tasks/autodiscover_pre_tasks.yml
      become: false
      tags:
        - always

  roles:
    - role: ansible-role-docker
      when:
        - docker_enabled

    - role: common

    - role: filebeat
      when: filebeat_enabled | default(True)

    - role: node_exporter
      when: node_exporter_enabled | default(True)

    - role: traefik
      when: traefik_enabled | default(True)

#############################################################
#   Sending smardigo management message to process
#############################################################

- hosts: "{{ stage }}-virtual-host-to-read-groups-vars"
  serial: "{{ serial_number | default(1) }}"
  gather_facts: false
  connection: local
  run_once: true
  vars:
    connect_jwt_username: "{{ management_admin_username }}"

  tasks:
    - name: "Sending smardigo management message to <{{ smardigo_management_url }}>"
      include_tasks: tasks/smardigo_management_message.yml
