---
- name: Ensure the key is present
  apt_key:
    url: http://packages.elasticsearch.org/GPG-KEY-elasticsearch
- name: Add repository
  apt_repository:
    repo: 'deb http://packages.elasticsearch.org/elasticsearch/1.1/debian stable main'
    state: present
  notify:
  - Update apt
- name: Ensure elasticsearch is installed
  apt:
    pkg: elasticsearch
    state: latest
- name: Copy elasticsearch configuration
  copy:
    src: elasticsearch.yml
    dest: /etc/elasticsearch/elasticsearch.yml
- name: Copy elasticsearch nginx proxy configuration
  copy:
    src: elasticsearch.proxy.conf
    dest: /etc/nginx/available/elasticsearch.proxy.conf
- name: Ensure the host is correctly written in the conf
  lineinfile:
    dest: /etc/nginx/available/elasticsearch.proxy.conf
    insertafter: "  listen   8080;"
    line: "    server_name  {{ monitoring_server }};"
- name: Create htpasswd for elasticsearch
  htpasswd:
    path: /etc/elasticsearch/elasticsearch.htpasswd
    name: "{{ user }}"
    password: "{{ lookup('password', '/tmp/ansible.password.elasticsearch_proxy length=64') }}"
    owner: root
    group: www-data
    mode: 0640
  notify:
    - Restart Nginx
- name: Link Nginx proxy conf
  shell: ln -sf /etc/nginx/available/elasticsearch.proxy.conf /etc/nginx/enabled/elasticsearch.proxy.conf
- name: Start elasticsearch
  service:
    name: elasticsearch
    state: restarted
    enabled: yes

---
- name: Ensure the key is present
  apt_key:
    url: http://packages.elasticsearch.org/GPG-KEY-elasticsearch
- name: Add repository
  apt_repository:
    repo: 'deb http://packages.elasticsearch.org/elasticsearch/1.1/debian stable main'
    state: present
  notify:
  - Update apt
- name: Ensure elasticsearch is installed
  apt:
    pkg: elasticsearch
    state: latest
- name: Copy elasticsearch configuration
  copy:
    src: elasticsearch.yml
    dest: /etc/elasticsearch/elasticsearch.yml
- name: Copy elasticsearch nginx proxy configuration
  copy:
    src: elasticsearch.proxy.conf
    dest: /etc/nginx/available/elasticsearch.proxy.conf
- name: Ensure the host is correctly written in the conf
  lineinfile:
    dest: /etc/nginx/available/elasticsearch.proxy.conf
    insertafter: "  listen   8080;"
    line: "    server_name  {{ monitoring_server }};"
- name: Remove htpasswd file
  file:
    path: /etc/elasticsearch/elasticsearch.htpasswd
    state: absent
- name: Create htpasswd for elasticsearch
  htpasswd:
    path: /etc/elasticsearch/elasticsearch.htpasswd
    name: "{{ user }}"
    password: "{{ lookup('password', '/tmp/ansible.password.elasticsearch_proxy length=64') }}"
    owner: root
    group: www-data
    mode: 0640
  notify:
    - Restart Nginx
- name: Link Nginx proxy conf
  shell: ln -sf /etc/nginx/available/elasticsearch.proxy.conf /etc/nginx/enabled/elasticsearch.proxy.conf
- name: Start elasticsearch
  service:
    name: elasticsearch
    state: restarted
    enabled: yes

Remove htpasswd before putting a new one
Remove htpasswd before putting a new one
