Infrastructure as CodeAnsibleInstalling Grafana Agent on a Linux host using Ansible

Installing 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 use it to push logs to Grafana Cloud.


Before you begin, you should have the following available:

  • A Grafana Cloud account.
  • A Linux machine
  • Command line (terminal) access to that Linux machine
  • 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:

    - clients:
      - basic_auth:
          password: <Your API Key>
          username: <User>
      name: default
        filename: /tmp/positions.yaml
        - job_name: integrations/node_exporter_direct_scrape
            - targets:
                - localhost
                instance: hostname
                __path__: /var/log/*.log
                job: integrations/node_exporter
        sync_period: 10s

Install Grafana Agent binary using Ansible

This Ansible playbook installs the Grafana Agent binary version v0.25.0 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:

    - name: Install Grafana Agent on a linux host
      connection: local
      hosts: localhost
        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.25.0
        - name: Download Grafana Agent binary
            url: "{{ agent_version }}/agent-{{ linux_architecture }}.zip"
            dest: "/tmp/"
            mode: '0644'
        - name: Unarchive Grafana Agent binary
            src: "/tmp/"
            dest: "{{ agent_binary_location }}"
            remote_src: yes
            mode: '0755'
        - name: Create directory for Grafana Agent
            path: "{{ agent_config_location }}"
            state: directory
            mode: '0755'
        - name: Create config file for Grafana Agent
            src: "{{ agent_config_local_path }}"
            dest: "{{ agent_config_location }}/agent-config.yaml"
        - name: Add user 'grafana-agent'
            name: grafana-agent
            create_home: no
            shell: /bin/false
        - name: Create service file for Grafana Agent
            dest: "/etc/systemd/system/grafana-agent.service"
            content: |
              Description=Grafana Agent
              ExecStart={{ agent_binary_location }}/agent-{{ linux_architecture }} --config.file={{ agent_config_location }}/agent-config.yaml
        - name: Start Grafana Agent service
            daemon_reload: yes
            name: grafana-agent
            enabled: yes
            state: restarted
  2. 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


  1. The Grafana Agent service on the Linux machine should be active and running. 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
  1. In a Grafana Cloud stack, click the Explore icon (compass points) in the 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


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.