This guide provides a full Ansible playbook to install Telegraf with repository setup and service management for Debian 10+, Ubuntu LTS, and RHEL 9+ compatible systems.
- name: Install Telegraf
hosts: telegraf
become: true
vars:
influxdb_version: "1.8"
telegraf_outputs:
- type: influxdb_v2
urls: ["http://influxdb:8086"]
token: "{{ influxdb_token }}"
organization: "{{ influxdb_org }}"
bucket: "{{ influxdb_bucket }}"
tasks:
- name: Install required packages (Debian/Ubuntu)
apt:
name:
- apt-transport-https
- gnupg
- curl
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Add InfluxData GPG key (Debian/Ubuntu)
apt_key:
url: https://repos.influxdata.com/influxdata-archive_compat.key
state: present
when: ansible_os_family == "Debian"
- name: Add InfluxData repository (Debian/Ubuntu)
apt_repository:
repo: "deb https://repos.influxdata.com/debian stable main"
state: present
filename: influxdata
when: ansible_os_family == "Debian"
- name: Install required packages (RHEL)
dnf:
name:
- yum-utils
state: present
when: ansible_os_family == "RedHat"
- name: Add InfluxData repository (RHEL)
yum_repository:
name: influxdata
description: InfluxData repository
baseurl: https://repos.influxdata.com/rhel
gpgcheck: 1
gpgkey: https://repos.influxdata.com/influxdata-archive_compat.key
enabled: 1
when: ansible_os_family == "RedHat"
- name: Install Telegraf
package:
name: telegraf
state: present
notify: restart telegraf
- name: Configure Telegraf outputs
lineinfile:
path: /etc/telegraf/telegraf.conf
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
loop:
- { regexp: '^#? \[\[outputs.influxdb_v2\]\]', line: '[[outputs.influxdb_v2]]' }
- { regexp: '^#? urls =', line: ' urls = ["{{ telegraf_outputs[0].urls[0] }}"]' }
- { regexp: '^#? token =', line: ' token = "{{ telegraf_outputs[0].token }}"' }
- { regexp: '^#? organization =', line: ' organization = "{{ telegraf_outputs[0].organization }}"' }
- { regexp: '^#? bucket =', line: ' bucket = "{{ telegraf_outputs[0].bucket }}"' }
notify: restart telegraf
- name: Enable and start Telegraf service
service:
name: telegraf
state: started
enabled: true
handlers:
- name: restart telegraf
service:
name: telegraf
state: restarted
/etc/telegraf/telegraf.confAny questions?
Feel free to contact us. Find all contact information on our contact page.