---
- name: Install elasticsearch
  apt:
    name: elasticsearch
    state: present

- name: Configure Elasticsearch.
  template:
    src: "etc/elasticsearch/{{ item }}"
    dest: "/etc/elasticsearch/{{ item }}"
    owner: root
    group: elasticsearch
    mode: 0660
  with_items:
    - jvm.options
    - elasticsearch.yml
  notify: restart elasticsearch

- meta: flush_handlers

- name: Start Elasticsearch.
  service:
    name: elasticsearch
    state: started
    enabled: yes

- name: Make sure Elasticsearch is running before proceeding.
  wait_for:
    host: "{{ elasticsearch_network_host }}"
    port: "{{ elasticsearch_http_port }}"
    delay: 3
    timeout: 300

- name: Set index-level settings
  when: elasticsearch_index_settings is defined
  uri:
    method: PUT
    url: http://localhost:9200/_settings
    body_format: json  # This will convert yaml data to a json string
    body:
      index: "{{ elasticsearch_index_settings }}"

---
- name: Install elasticsearch
  apt:
    name: elasticsearch
    state: present

- name: Configure Elasticsearch.
  template:
    src: "etc/elasticsearch/{{ item }}"
    dest: "/etc/elasticsearch/{{ item }}"
    owner: root
    group: elasticsearch
    mode: 0660
  with_items:
    - jvm.options
    - elasticsearch.yml
  notify: restart elasticsearch

- meta: flush_handlers

- name: Start Elasticsearch.
  service:
    name: elasticsearch
    state: started
    enabled: yes

- name: Make sure Elasticsearch is running before proceeding.
  wait_for:
    host: "{{ elasticsearch_network_host }}"
    port: "{{ elasticsearch_http_port }}"
    delay: 3
    timeout: 300

- name: Set index-level settings
  when: elasticsearch_index_settings is defined
  uri:
    method: PUT
    url: http://localhost:9200/_template/all
    body_format: json  # This will convert yaml data to a json string
    body:
      template: '*'
      order: 0
      settings: "{{ elasticsearch_index_settings }}"

Convert elasticsearch index settings to a template
Convert elasticsearch index settings to a template

Setting cluster index settings via /_settings is not available until after
at least one index has been created. So instead, create an index template
that applies to all indices to set the index settings.

Signed-off-by: K Jonathan Harker <4a32a1ac4cf9729ec1fbb65946748ef0085119ad@ibm.com>
