---

- name: Creating node_exporter user group
  group: name="node_exporter"
  become: true

- name: Creating node_exporter user
  user:
    name: "node_exporter"
    group: "node_exporter"
    system: yes
    shell: "/sbin/nologin"
    comment: "node_exporter nologin User"
    createhome: "no"
    state: present

- name: Node exporter... Download Prometheus archive
  get_url:
    url:  "{{ node_exporter_url }}/{{ node_exporter_tarball }}.tar.gz"
    dest: /tmp/{{ node_exporter_tarball }}.tar.gz
    checksum: "sha256:{{ node_exporter_url }}/sha256sums.txt"
  register: _download_archive
  until: _download_archive is succeeded
  retries: 5
  delay: 2
  when: not node_exporter_skip_install

- name: Node exporter... unpack prometheus binaries
  unarchive:
    src: "/tmp/{{ node_exporter_tarball }}.tar.gz"
    dest: "/tmp"
    creates: "/tmp/{{ node_exporter_tarball }}/node_exporter"
    remote_src: true
  when: not node_exporter_skip_install

- name: Node exporter... Copy prometheus node exporter file to bin
  copy:
    src: "/tmp/{{ node_exporter_tarball }}/node_exporter"
    dest: "/usr/local/bin/node_exporter"
    owner: node_exporter
    group: node_exporter
    remote_src: yes
    mode: 0755
  when: not node_exporter_skip_install

- name: Delete node exporter tmp folder
  file:
    path: "/tmp/{{ node_exporter_tarball }}.tar.gz"
    state: absent
  when: not node_exporter_skip_install

- name: Node exporter... Copy systemd init file
  copy:
    dest: /etc/systemd/system/node_exporter.service
    src: files/node_exporter.service
    owner: root
    group: root
    mode: 0640

- name: Start node_exporter service
  service:
    name: node_exporter
    state: started
    enabled: yes

- name: Check if node exporter emits metrices
  uri:
    url: http://127.0.0.1:9100/metrics
    method: GET
    status_code: 200
