Install Grafana Agent on a Linux host using Ansible
This guide will show you how to install the Grafana Agent on a Linux host using Ansible and to use it to push logs to Grafana Cloud.
Before you begin
Before you begin, you should have the following available:
- A Grafana Cloud account.
- A Linux machine
- Command line (terminal) access to that Linux machine with
unzip
binary installed - Account permissions sufficient to install and use the Grafana Agent on the Linux machine
- Ansible installed on the Linux machine
Create a Grafana Agent configuration file
For this guide, we will configure the agent to collect and send logs from the Linux machine. The Agent configuration should look like this:
logs:
configs:
- clients:
- basic_auth:
password: <Your Grafana.com API Key>
username: <User>
url: https://logs-prod3.grafana.net/loki/api/v1/push
name: default
positions:
filename: /tmp/positions.yaml
scrape_configs:
- job_name: integrations/node_exporter_direct_scrape
static_configs:
- targets:
- localhost
labels:
instance: hostname
__path__: /var/log/*.log
job: integrations/node_exporter
target_config:
sync_period: 10s
Install Grafana Agent binary using Ansible
This Ansible playbook installs the Grafana Agent binary version v0.32.1 and also creates a systemd service to manage the Grafana agent.
It creates a new user named grafana-agent
on the Linux machine for running the Grafana Agent.
- Create a file named
grafana-agent.yml
and add the following:
- name: Install Grafana Agent on a linux host
connection: local
hosts: localhost
vars:
agent_binary_location: <agent-binary-location> # Example /usr/local/bin
agent_config_location: <agent-config-location> # Example /etc/grafana-cloud
linux_architecture: <linux-architecture> # Example linux-amd64
agent_config_local_path: <agent-config-local-path> # Example linux-agent-config.yml
agent_version: 0.32.1
tasks:
- name: Download Grafana Agent binary
get_url:
url: "https://github.com/grafana/agent/releases/download/v{{ agent_version }}/grafana-agent-{{ linux_architecture }}.zip"
dest: "/tmp/agent-linux.zip"
mode: '0644'
- name: Unarchive Grafana Agent binary
unarchive:
src: "/tmp/agent-linux.zip"
dest: "{{ agent_binary_location }}"
remote_src: yes
mode: '0755'
- name: Create directory for Grafana Agent
file:
path: "{{ agent_config_location }}"
state: directory
mode: '0755'
- name: Create config file for Grafana Agent
copy:
src: "{{ agent_config_local_path }}"
dest: "{{ agent_config_location }}/agent-config.yaml"
- name: Add user 'grafana-agent'
user:
name: grafana-agent
create_home: no
shell: /bin/false
- name: Create service file for Grafana Agent
copy:
dest: "/etc/systemd/system/grafana-agent.service"
content: |
[Unit]
Description=Grafana Agent
[Service]
User=grafana-agent
ExecStart={{ agent_binary_location }}/grafana-agent-{{ linux_architecture }} --config.file={{ agent_config_location }}/agent-config.yaml
Restart=always
[Install]
WantedBy=multi-user.target
- name: Start Grafana Agent service
systemd:
daemon_reload: yes
name: grafana-agent
enabled: yes
state: restarted
Replace the following field values:
<agent-binary-location>
with the path where the Grafana Agent binary will be stored on the Linux machine.<agent-config-location>
with the path where the Grafana Agent configuration will be stored on the Linux machine.<linux-architecture>
with the architecture of your Linux machine.<agent-config-local-path>
with the local path to the Grafana Agent configuration.
Run the Ansible playbook on the Linux machine
In the Linux machine’s terminal, run the following command from the directory where the Ansible playbook is located.
ansible-playbook grafana-agent.yml
Validate
- The Grafana Agent service on the Linux machine should be
active
andrunning
. You should see a similar output:
$ sudo systemctl status grafana-agent.service
grafana-agent.service - Grafana Agent
Loaded: loaded (/etc/systemd/system/grafana-agent.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-07-20 09:56:15 UTC; 36s ago
Main PID: 3176 (agent-linux-amd)
Tasks: 8 (limit: 515)
Memory: 92.5M
CPU: 380ms
CGroup: /system.slice/grafana-agent.service
└─3176 /usr/local/bin/agent-linux-amd64 --config.file=/etc/grafana-cloud/agent-config.yaml
In a Grafana Cloud stack, click Explore in the left-side menu.
At the top of the page, use the dropdown menu to select your Loki logs data source. In the Log Browser, run the query
{job="integrations/node_exporter"}
Summary
In this guide, you installed the Grafana Agent on a Linux node using Ansible and use it to pushed logs to Grafana Cloud.
To learn more about managing Grafana using Infrastructure as Code, see Provisioning Grafana Cloud with infrastructure as code.