- name: Install Telegraf from URL [RHEL/CentOS]
  yum:
    name: "{{ telegraf_install_rpm_url }}"
    state: latest
  when: ansible_os_family == "RedHat"
  become: true
  become_user: root

- name: Download Telegraf package via URL [Debian/Ubuntu]
  get_url:
    url: "{{ telegraf_install_deb_url }}"
    dest: /tmp/telegraf-ansible-download.deb
  when: ansible_os_family == "Debian"
  become: true
  become_user: root

- name: Install Telegraf package
  apt:
    deb: /tmp/telegraf-ansible-download.deb
    state: latest
  when: ansible_os_family == "Debian"
  become: true
  become_user: root

- name: Get list of hardware counters for interfaces
  script: files/hw_counters.py
  register: hwcounters
  check_mode: no

- name: Set hardware counters in facts
  set_fact:
    hwcounterlist: "{{ hwcounters.stdout | from_json }}"

- name: Make a directory for extra files
  file:
    state: directory
    mode: 'u=rwx,g=rx,o=rx'
    owner: 'telegraf'
    group: 'telegraf'
    path: '/opt/telegraf/bin'
  become: true
  become_user: root

- name: copy mountstats plugin
  copy:
    mode: 'u=rwx,g=rx,o=rx'
    src: telegraf_mountstats.py
    dest: '/opt/telegraf/bin/telegraf_mountstats.py'
  become: true
  become_user: root

- name: copy slurmstats plugin
  copy:
    mode: 'u=rwx,g=rx,o=rx'
    src: telegraf_slurmstats.py
    dest: '/opt/telegraf/bin/telegraf_slurmstats.py'
  become: true
  become_user: root
#
- name: Install Telegraf config
  template:
    src: telegraf.conf.j2
    dest: /etc/telegraf/telegraf.conf
    owner: telegraf
    group: telegraf
    mode: '640'
  notify:
    - "restart telegraf"
  become: true
  become_user: root
  tags:
    - configuration

- name: Install multifile plugin for mlx hw_counters
  template:
    src: inputs.multifile_mlx.conf.j2
    dest: /etc/telegraf/telegraf.d/inputs.multifile_mlx.conf
    owner: telegraf
    group: telegraf
    mode: '640'
  notify:
    - "restart telegraf"
  become: true
  become_user: root
  tags:
    - configuration

- name: Install nvidia-smi plugin
  template:
    src: inputs.nvidia_smi.conf.j2
    dest: /etc/telegraf/telegraf.d/inputs.nvidia_smi.conf
    owner: telegraf
    group: telegraf
    mode: '640'
  notify:
    - "restart telegraf"
  become: true
  become_user: root
  tags:
    - configuration
    - gpu
  when: "'VisNodes' in group_names"