---

- name: Configure cluster name
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "cluster.name: {{ elasticsearch_cluster_name }}"
    regexp: "^cluster.name:"
    insertafter: "^# *cluster.name:"
  when: elasticsearch_cluster_name | default("", True) | length > 0
  tags:
    - config

- name: Configure node name
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "node.name: {{ elasticsearch_node_name }}"
    regexp: "^node.name:"
    insertafter: "^# *node.name:"
  tags:
    - config

- name: Configure network host
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "network.host: {{ elasticsearch_network_host  }}"
    regexp: "^network.host:"
    insertafter: "^# *network.host:"
  when: elasticsearch_network_host | default("", True) | length > 0
  tags:
    - config

- name: Configure network publish_host
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "network.publish_host: {{ elasticsearch_network_publish_host  }}"
    regexp: "^network.publish_host:"
    insertafter: "^network.host:"
  when: elasticsearch_network_publish_host | default("", True) | length > 0
  tags:
    - config

- name: Configure http publish_host
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "http.publish_host: {{ elasticsearch_http_publish_host  }}"
    regexp: "^http.publish_host:"
    insertafter: "^http.port:"
  when: elasticsearch_http_publish_host | default("", True) | length > 0
  tags:
    - config

- name: Configure discovery seed hosts
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "discovery.seed_hosts: {{ elasticsearch_discovery_seed_hosts | to_yaml }}"
    regexp: "^discovery.seed_hosts:"
  when: elasticsearch_discovery_seed_hosts | length > 0
  tags:
    - config

- name: Configure initial master nodes
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "cluster.initial_master_nodes: {{ elasticsearch_cluster_initial_master_nodes | to_yaml }}"
    regexp: "^cluster.initial_master_nodes:"
  when: elasticsearch_cluster_initial_master_nodes | length > 0
  tags:
    - config

- name: Configure RESTART_ON_UPGRADE
  lineinfile:
    dest: /etc/default/elasticsearch
    line: "RESTART_ON_UPGRADE={{ elasticsearch_restart_on_upgrade | bool | ternary('true','false') }}"
    regexp: "^RESTART_ON_UPGRADE="
    insertafter: "^# *RESTART_ON_UPGRADE="
  tags:
    - config

- name: JVM Heap size (min) is set
  lineinfile:
    dest: /etc/elasticsearch/jvm.options
    regexp: "^-Xms"
    line: "-Xms{{ elasticsearch_jvm_xms }}"
  tags:
    - config

- name: JVM Heap size (max) is set
  lineinfile:
    dest: /etc/elasticsearch/jvm.options
    regexp: "^-Xmx"
    line: "-Xmx{{ elasticsearch_jvm_xmx }}"
  tags:
    - config

- name: Configure cluster members
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "discovery.zen.ping.unicast.hosts: {{ elasticsearch_cluster_members }}"
    regexp: "^discovery.zen.ping.unicast.hosts:"
    insertafter: "^#discovery.zen.ping.unicast.hosts"
  when: elasticsearch_cluster_members | default("", True) | length > 0
  tags:
    - config

- name: Configure minimum master nodes
  lineinfile:
    dest: /etc/elasticsearch/elasticsearch.yml
    line: "discovery.zen.minimum_master_nodes: {{ elasticsearch_minimum_master_nodes }}"
    regexp: "^discovery.zen.minimum_master_nodes:"
    insertafter: "^#discovery.zen.minimum_master_nodes"
  when: elasticsearch_minimum_master_nodes | default("", True) | length > 0
  tags:
    - config
