---

- name: add elasticsearch repository key
  apt_key:
    url: "http://packages.elasticsearch.org/GPG-KEY-elasticsearch"
    state: present
  become: yes

- name: add elasticsearch repo
  apt_repository:
    repo: "deb http://packages.elastic.co/elasticsearch/2.x/debian stable main"
  become: yes

- name: install elasticsearch
  apt:
    name: elasticsearch
    update_cache: yes
  become: yes

- name: copy elasticsearch config
  template:
    src: elasticsearch.yml
    dest: /etc/elasticsearch/elasticsearch.yml
    owner: root
    group: elasticsearch
    mode: u=rw,g=r,o=r
  become: yes
  register: config

- name: copy elasticsearch logging config
  template:
    src: logging.yml
    dest: /etc/elasticsearch/logging.yml
    owner: root
    group: elasticsearch
    mode: u=rw,g=r,o=r
  become: yes
  register: logging_config

- name: bounce elasticsearch
  service:
    name: elasticsearch
    state: restarted
  become: yes
  when: config.changed or logging_config.changed
  register: bounced

- name: start elasticsearch
  service:
    name: elasticsearch
    state: started
  become: yes
  when: not bounced.changed
