---
- name: "Importing kibana objects to <{{ shared_service_url_kibana }}>"
  include_tasks: import_service_ojects.yml
  loop: "{{ elastic_index_patterns }}"
  loop_control:
    label: "{{ elastic_index_pattern.id }}"
    loop_var: elastic_index_pattern
  when:
    - elastic_index_patterns is defined

- name: "Setting default index pattern"
  set_fact:
    elastic_default_index: "{{ elastic_index_patterns | first | map(attribute='id') | to_uuid }}"

- name: "Setting default index to <{{ elastic_default_index }}>"
  delegate_to: localhost
  uri:
    url: "{{ shared_service_url_kibana }}/s/{{ stage }}-{{ tenant_id }}/api/kibana/settings"
    method: POST
    status_code: [200]
    user: "{{ elastic_admin_username }}"
    password: "{{ elastic_admin_password }}"
    force_basic_auth: yes
    headers:
      kbn-xsrf: true
    body_format: json
    body:
      changes:
        defaultIndex: "{{ elastic_default_index }}"
  become: false
  when:
    - elastic_state == 'present'
    - elastic_index_patterns is defined
