---
-
  name: "Block to Allow Loading of Variables without running task"
  when: run_tasks
  block:
    -
      name: "Including setup-RedHat.yml {{ elasticsearch_version }}"
      include_tasks: setup-RedHat.yml
      when: ansible_os_family == 'RedHat'
    
    -
      name: "Including setup-Debian.yml {{ elasticsearch_version }}[0]"
      include_tasks: setup-Debian.yml
      when: ansible_os_family == 'Debian'
    
    -
      name: "Install Elasticsearch {{ elasticsearch_version }}"
      package:
        name: "{{ elasticsearch_package }}"
        state: "{{ elasticsearch_package_state }}"
    
    - name: "Configure Elasticsearch 6 or below {{ elasticsearch_version }}"
      template:
        src: "{{ item | basename }}.j2"
        dest: "{{ item }}"
        owner: root
        group: elasticsearch
        mode: 0660
      with_items:
        - /etc/elasticsearch/elasticsearch.yml
        - /etc/elasticsearch/jvm.options
      notify: restart elasticsearch
      when: elasticsearch_version[0] | int < 7
    
    - name: Configure Elasticsearch 7+.
      template:
        src: "{{ item | basename }}.j2"
        dest: "{{ item }}"
        owner: root
        group: elasticsearch
        mode: 0660
      with_items:
        - /etc/elasticsearch/elasticsearch.yml
        - /etc/elasticsearch/jvm.options.d/heap.options
      notify: restart elasticsearch
      when: elasticsearch_version[0] | int >= 7
    
    - name: Force a restart if configuration has changed.
      meta: flush_handlers
    
    - name: Start Elasticsearch.
      service:
        name: elasticsearch
        state: "{{ elasticsearch_service_state }}"
        enabled: "{{ elasticsearch_service_enabled }}"
    
    - name: Make sure Elasticsearch is running before proceeding.
      wait_for:
        host: "{{ elasticsearch_network_host }}"
        port: "{{ elasticsearch_http_port }}"
        delay: 3
        timeout: 300
