---

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

- name: add beats repo
  apt_repository:
    repo: "deb https://artifacts.elastic.co/packages/oss-7.x/apt stable main"
  become: yes

- name: create filebeat log directory
  file:
    path: /var/log/filebeat
    state: directory
    mode: 0644
    owner: root
    group: root
  become: yes

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

# This will deploy the generated filebeat config file to
# the nodes + to a location in `/nfs/pipedream/config/production`
# where the config files for cloud burst nodes are stored
- name: copy filebeat config
  template:
    src: filebeat7.yml.j2
    dest: "{{item.dest}}"
    owner: root
    group: root
    mode: 0644
  with_items:
    - { dest: /etc/filebeat/filebeat.yml }
  become: yes
  register: config
  tags: reconfigure-filebeat

- name: bounce filebeat
  service:
    name: filebeat
    state: restarted
  become: yes
  when: config.changed
  tags: reconfigure-filebeat

- name: start filebeat
  service:
    name: filebeat
    state: started
  become: yes
  tags: reconfigure-filebeat
