Manage OpenTelemetry Collectors at scale with Ansible

You can scale the deployment of OpenTelemetry Collector across multiple Linux hosts through Ansible, to function both as gateways and agents within your observability architecture. Using the OpenTelemetry Collector in this dual capacity enables a robust collection and forwarding of metrics, traces, and logs to analysis and visualization platforms.

We outline a strategy for deploying and managing the OpenTelemetry Collector’s scalable instances throughout your infrastructure using Ansible. In the following example, we’ll use Grafana as the target backend for metrics.


Before we begin, make sure you meet the following requirements:

  • Ansible installed on your base system
  • SSH access to two or more Linux hosts
  • Prometheus configured to gather your metrics

Install the Grafana Ansible collection

The OpenTelemetry Collector role is provided through the Grafana Ansible collection as of release 4.0.

To install the Grafana Ansible collection, run this command:

ansible-galaxy collection install grafana.grafana

Create an Ansible inventory file

Next, gather the IP addresses and URLs associated with your Linux hosts and create an inventory file.

  1. Create an Ansible inventory file.

    An Ansible inventory, which resides in a file named inventory, lists each host IP on a separate line, like this (8 hosts shown):    # hostname = ubuntu-01    # hostname = ubuntu-02    # hostname = centos-01    # hostname = centos-02    # hostname = debian-01    # hostname = debian-02    # hostname = fedora-01    # hostname = fedora-02
  2. Create an ansible.cfg file within the same directory as inventory, with the following values:

    inventory = inventory  # Path to the inventory file
    private_key_file = ~/.ssh/id_rsa   # Path to private SSH Key

Use the OpenTelemetry Collector Ansible role

Next, define an Ansible playbook to apply your chosen or created OpenTelemetry Collector role across your hosts.

Create a file named deploy-opentelemetry.yml in the same directory as your ansible.cfg and inventory files:

- name: Install OpenTelemetry Collector
  hosts: all
  become: true

    - name: Install OpenTelemetry Collector
        name: opentelemetry_collectorr
            collection_interval: 60s
              cpu: {}
              disk: {}
              load: {}
              filesystem: {}
              memory: {}
              network: {}
              paging: {}
                mute_process_name_error: true
                mute_process_exe_error: true
                mute_process_io_error: true
              processes: {}

            detectors: [env, system]
            timeout: 2s
              hostname_sources: [os]
            error_mode: ignore
              - context: datapoint
                  - set(attributes["deployment.environment"],
                  - set(attributes["service.version"],

            endpoint: https://<prometheus-url>/api/prom/push
              Authorization: 'Basic <base64-encoded-username:password>'

              receivers: [hostmetrics]
              exporters: [prometheusremotewrite]

The previous configuration would provision the OpenTelemetry Collector to collect metrics from the Linux host.

Running the Ansible playbook

Deploy the OpenTelemetry Collector across your hosts by running the following command:

ansible-playbook deploy-opentelemetry.yml

Check your metrics in the backend

After your OpenTelemetry Collectors start sending metrics to Prometheus, follow these steps to visualize them in Grafana:

Set up Grafana

  1. Install Docker: Make sure Docker is installed on your system.

  2. Run Grafana Docker Container: Start a Grafana server with the following command, which fetches the latest Grafana image:

    docker run -d -p 3000:3000 --name=grafana grafana/grafana
  3. Access Grafana: Open http://localhost:3000 in your web browser. The default login username and password are both admin.

  4. Change passwords when prompted on first login – pick a secure one!

For other installation methods and more detailed instructions, refer to the official Grafana documentation.

Add Prometheus as a data source

  1. In Grafana, navigate to Connections > Data Sources.
  2. Click Add data source and select Prometheus.
  3. In the settings, enter your Prometheus URL, for example, http://<your_prometheus_host>, along with any other necessary details.
  4. Select Save & Test.

Explore your metrics

  1. Go to the Explore page

  2. In the Query editor, select your data source and enter the following query

    100 - (avg by (cpu) (irate(system_cpu_time{state="idle"}[5m])) * 100)

    This query calculates the average percentage of CPU time not spent in the “idle” state, across each CPU core, over the last 5 minutes.

  3. Explore other metrics and create dashboards to gain insights into your system’s performance.

This blog post illustrated how you can configure and deploy multiple OpenTelemetry Collectors across various Linux hosts with the help of Ansible, as well as visualize collected telemetry in Grafana. Incase you find this useful, GitHub repository for OpenTelemetry Collector role for detailed configuration options. If you have questions, You can connect with me using my contact details at my GitHub profile @ishanjainn.