---

- name: elasticsearch-install | Add repository keys
  apt_key: keyserver="{{item.server}}" id="{{item.key}}" state=present
  with_items: "{{ elasticsearch_apt_repo_keys }}"

- name: elasticsearch-install | Add repositories
  apt_repository: repo="{{item}}" update_cache=yes
  with_items: "{{ elasticsearch_apt_repos }}"

- name: elasticsearch-install | Check for Elasticsearch already installed
  stat: path={{elasticsearch_home}}/lib/elasticsearch-{{elasticsearch_version}}.jar
  register: version_exists

- name: elasticsearch-install | Download deb
  get_url: url={{elasticsearch_download_url}}/elasticsearch-{{elasticsearch_version}}.deb dest=/tmp/elasticsearch-{{elasticsearch_version}}.deb mode=0440
  when: not version_exists.stat.exists

- name: elasticsearch-install | Install Elasticsearch
  apt: deb=/tmp/elasticsearch-{{elasticsearch_version}}.deb dpkg_options=force-confnew
  when: not version_exists.stat.exists

- name: elasticsearch-install | Setup passlib
  apt: pkg=python-passlib
  when: elasticsearch_proxy_auth


---

- name: elasticsearch-install | Add repository keys
  apt_key: keyserver="{{item.server}}" id="{{item.key}}" state=present
  with_items: "{{ elasticsearch_apt_repo_keys }}"

- name: elasticsearch-install | Add repositories
  apt_repository: repo="{{item}}" update_cache=yes
  with_items: "{{ elasticsearch_apt_repos }}"

- name: elasticsearch-install | Check for Elasticsearch already installed
  stat: path={{elasticsearch_home}}/lib/elasticsearch-{{elasticsearch_version}}.jar
  register: version_exists

- name: elasticsearch-install | Download deb (legacy)
  get_url: url={{elasticsearch_download_url}}/elasticsearch-{{elasticsearch_version}}.deb dest=/tmp/elasticsearch-{{elasticsearch_version}}.deb mode=0440
  when: not version_exists.stat.exists and not elasticsearch_version == "7.1.1"

- name: elasticsearch-install | Install Elasticsearch (legacy)
  apt: deb=/tmp/elasticsearch-{{elasticsearch_version}}.deb dpkg_options=force-confnew
  when: not version_exists.stat.exists and not elasticsearch_version == "7.1.1"

- name: add apt key for elastic (modern)
  apt_key: url="https://artifacts.elastic.co/GPG-KEY-elasticsearch" state=present
  notify: elasticsearch restart
  when: not version_exists.stat.exists and elasticsearch_version == "7.1.1"

- name: add elastic 7 apt source list (modern)
  copy: content="deb https://artifacts.elastic.co/packages/7.x/apt stable main" dest="/etc/apt/sources.list.d/elastic-7.x.list"
  notify: elasticsearch restart
  when: not version_exists.stat.exists and elasticsearch_version == "7.1.1"

- name: elasticsearch-install | Install Elasticsearch (modern)
  apt: name="elasticsearch" state=latest
  when: not version_exists.stat.exists and elasticsearch_version == "7.1.1"

- name: elasticsearch-install | Setup passlib
  apt: pkg=python-passlib
  when: elasticsearch_proxy_auth


Install modern elasticsearch when previous doesn't exist or elasticsearch_version is customised
Install modern elasticsearch when previous doesn't exist or elasticsearch_version is customised

Signed-off-by: Peter Ansell <aaf1ec639befe0a570b950d5d6cbd0c6638a063e@yahoo.com>
