- hosts:
    - elasticsearch_servers
  tasks:
    - name: Create elasticsearch user
      become: yes
      user:
        name: elasticsearch
        system: yes
        state: "{{ elasticsearch__state }}"
        shell: /bin/bash
    - become: yes
      file:
        path: "{{ elasticsearch__data_dir }}"
        state: directory
        owner: elasticsearch
        group: elasticsearch
        mode: u=rwx,go=rx

    - name: /etc/default/elasticsearch
      become: yes
      copy:
        dest: /etc/default/elasticsearch
        content: ""
        force: no
    - become: yes
      lineinfile:
        dest: /etc/default/elasticsearch
        line: "ES_PATH_CONF=/etc/elasticsearch"
        regexp: "ES_PATH_CONF"

- hosts:
    - elasticsearch_servers
  roles:
    - ansible-elasticsearch
  vars:
    es_instance_name: "node1"
    es_data_dirs:
      - "{{ elasticsearch__data_dir }}"
    es_config:
      http.port: "{{ elasticsearch__http_port }}"
      transport.tcp.port: "{{ elasticsearch__tcp_port }}"
      discovery.zen.ping.unicast.hosts: "localhost:9301"
    es_api_basic_auth_username: admin
    es_api_basic_auth_password: admin
  tasks:
    - name: enable elasticsearch
      tags: elasticsearch
      systemd:
        name: elasticsearch
        state: started
        enabled: yes

    - tags: kibana
      become: yes
      block:
        - apt:
            name: kibana
            install_recommends: false
        - lineinfile:
            path: /etc/kibana/kibana.yml
            #elasticsearch.url: "http://localhost:9200"
            regexp: "elasticsearch\\.url"
            line: 'elasticsearch.url: "http://localhost:{{ elasticsearch__http_port }}"'
          notify: restart kibana
        - name: enable kibana
          systemd:
            name: kibana
            state: started
            enabled: yes

  handlers:
    - name: restart kibana
      become: yes
      systemd:
        name: kibana
        state: restarted