Menu
Grafana Cloud Developer resources Infrastructure as code Ansible Install Grafana Agent on a Linux host using Ansible
Grafana Cloud

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

Install Grafana Agent binary using Ansible

This Ansible playbook installs the latest version of Grafana Agent Binary 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.

  1. Create a file named grafana-agent.yml and add the following:
yaml
- name: Install Grafana Agent
  hosts: all
  become: true

  vars:
    grafana_cloud_api_key: <Your Cloud Access Policy token> # Example - glc_eyJrIjoiYjI3NjI5MGQxZTcyOTIxYTc0MDgzMGVhNDhlODNhYzA5OTk2Y2U5YiIsIm4iOiJhbnNpYmxldGVzdCIsImlkIjo2NTI5
    logs_username: <loki-username> # Example - 411478
    loki_url: <loki-push-url> # Example - https://logs-prod-017.grafana.net/loki/api/v1/push
  tasks:
    - name: Install Grafana Agent
      ansible.builtin.include_role:
        name: grafana_agent
      vars:
        grafana_agent_logs_config:
          configs:
            - clients:
                - basic_auth:
                    password: '{{ grafana_cloud_api_key }}'
                    username: '{{ logs_username }}'
                  url: '{{ loki_url }}'
              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
  1. Replace the following field values:

    • <Your Cloud Access Policy token> with a token from the Cloud Access Policy you created in the Grafana Cloud portal.
    • <loki-username> with the Loki Username
    • <loki-push-url> with the push endpoint URL of Loki Instance

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.

shell
ansible-playbook grafana-agent.yml

Validate

  1. The Grafana Agent service on the Linux machine should be active and running. You should see a similar output:
shell
$ 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
  1. In a Grafana Cloud stack, click Explore in the left-side menu.

  2. 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"}

    Loki Logs

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 the Grafana Ansible collection, see its GitHub repository or its documentation.