Vendor Galaxy Roles and Collections
Some checks failed
/ Ansible Lint (push) Failing after 5m45s
/ Ansible Lint (pull_request) Failing after 4m59s

This commit is contained in:
Stefan Bethke 2026-02-06 22:07:16 +01:00
commit 2aed20393f
3553 changed files with 387444 additions and 2 deletions

View file

@ -0,0 +1,72 @@
|![](https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/Warning.svg/156px-Warning.svg.png) | This Ansible role is now in maintenance mode only. We recommend using the Grafana Alloy Role for future deployments and updates.
|---|---|
# Ansible Role for Grafana Agent
Ansible Role to deploy Grafana Agent on Linux hosts. Using this Role, Grafana Agent can be deployed on RedHat, Ubuntu, Debian, CentOS
and Fedora linux distributions.
## Requirements
Please ensure that `curl` is intalled on Ansible controller.
To use this role, You need a YAML file having the Grafana Agent configuration.
## Role Variables
All variables which can be overridden are stored in [./defaults/main.yaml](./defaults/main.yaml) file as well as in table below.
| Variable | Default | Description |
| :------ | :------ | :--------- |
| `grafana_agent_version` | `latest` | version of the agent to install |
| `grafana_agent_base_download_url` | `https://github.com/{{ _grafana_agent_github_org }}/{{ _grafana_agent_github_repo }}/releases/download` | base download url. Github or mirror to download from |
| `grafana_agent_install_dir` | `/opt/grafana-agent/bin` | directory to install the binary to |
| `grafana_agent_binary` | `grafana-agent` | name to use for the binary |
| `grafana_agent_config_dir` | `/etc/grafana-agent` | directory to store the configuration files in |
| `grafana_agent_config_filename` | `config.yaml` | name of the configuration file for the agent |
| `grafana_agent_env_file` | `service.env` | name of the environment file loaded by the system unit file |
| `grafana_agent_service_extra` | "" | dictionary of additional custom settings for the systemd service file |
| `grafana_agent_local_tmp_dir` | `/tmp/grafana-agent` | temporary directory to create on the controller/localhost where the archive will be downloaded to |
| `grafana_agent_data_dir` | `/var/lib/grafana-agent` | the data directory to create for the wal and positions |
| `grafana_agent_wal_dir` | `"{{ grafana_agent_data_dir }}/data"` | wal directory to use, should be a sub-folder of grafana_agent_data_dir, will automatically be created when the agent starts |
| `grafana_agent_positions_dir` | `"{{ grafana_agent_data_dir }}/data"` | positions directory to use, should be a sub-folder of grafana_agent_data_dir, will automatically be created when the agent starts |
| `grafana_agent_mode` | `static` | mode to run Grafana Agent in. Can be "flow" or "static", [Flow Docs](https://grafana.com/docs/agent/latest/flow/) |
| `grafana_agent_user` | `grafana-agent` | os user to create for the agent to run as |
| `grafana_agent_user_group` | `grafana-agent` | os user group to create for the agent |
| `grafana_agent_user_groups` | `[]` | Configurable user groups that the Grafana agent can be put in so that it can access logs |
| `grafana_agent_user_shell` | `/usr/sbin/nologin` | the shell for the user |
| `grafana_agent_user_createhome` | `false` | whether or not to create a home directory for the user |
| `grafana_agent_local_binary_file` | `""` | full path to the local binary if already downloaded or built on the controller, this should only be set, if ansible is not downloading the binary and you have manually downloaded the binary |
| `grafana_agent_flags_extra` | see [./defaults/main.yaml](./defaults/main.yaml) | dictionary of additional command-line flags, run grafana-agent --help for a complete list. [Docs](https://grafana.com/docs/agent/latest/configuration/flags/) |
| `grafana_agent_env_vars` | `{}` | sets Environment variables in the systemd service configuration. |
| `grafana_agent_env_file_vars` | `{}` | dictionary of key/pair values to write to the environment file that is loaded by the service, with the flag `--config.expand-env=true` any generated config files will support the expansion of environment variables at runtime by referencing ${ENVVAR}. be aware of boolean values, when output they will result in the proper-cased string "True" and "False" |
| `grafana_agent_provisioned_config_file` | `""` | path to a config file on the controller that will be used instead of the provided configs below if specified. |
| `grafana_agent_server_config` | see [./defaults/main.yaml](./defaults/main.yaml) | Configures the server of the Agent used to enable self-scraping, [Docs](https://grafana.com/docs/agent/latest/configuration/server-config/) |
| `grafana_agent_metrics_config` | see [./defaults/main.yaml](./defaults/main.yaml) | Configures metric collection, [Docs](https://grafana.com/docs/agent/latest/configuration/metrics-config/) |
| `grafana_agent_logs_config` | see [./defaults/main.yaml](./defaults/main.yaml) | Configures logs collection, [Docs](https://grafana.com/docs/agent/latest/configuration/logs-config/) |
| `grafana_agent_traces_config` | see [./defaults/main.yaml](./defaults/main.yaml) | Configures traces collection, [Docs](https://grafana.com/docs/agent/latest/configuration/traces-config/) |
| `grafana_agent_integrations_config` | see [./defaults/main.yaml](./defaults/main.yaml) | Configures integrations for the agent, [Docs](https://grafana.com/docs/agent/latest/configuration/integrations/) |
## OS Support
The Grafana Agent role has been tested on below Operating Systems
- Ubuntu 22.10, Ubunutu 22.04 LTS, Ubunutu 20.04 LTS, Ubunutu 18.04 LTS
- Fedora 37, Fedora 36
- Debian 11, Debian 10, Debian 9
- CentOS 9 Stream, CentOS 8 Stream, CentOS 7
- AlmaLinux 9, AlmaLinux 8
- RockyLinux 9, RockyLinux 8
## Example Playbooks
See [examples](../../examples)
## License
See [LICENSE](https://github.com/grafana/grafana-ansible-collection/blob/main/LICENSE)
## Author Information
- [Grafana Labs](https://github.com/grafana)
- [Ishan Jain](https://github.com/ishanjainn)
- [Aaron Benton](https://github.com/bentonam)
- [Vitaly Zhuravlev](https://github.com/v-zhuravlev)

View file

@ -0,0 +1,196 @@
---
# version of the agent to install
grafana_agent_version: latest
# base download url. Github or mirror to download from
grafana_agent_base_download_url: "https://github.com/{{ _grafana_agent_github_org }}/{{ _grafana_agent_github_repo }}/releases/download"
# directory to install the binary to
grafana_agent_install_dir: /opt/grafana-agent/bin
# name to use for the binary
grafana_agent_binary: grafana-agent
# directory to store the configuration files in
grafana_agent_config_dir: /etc/grafana-agent
# name of the configuration file for the agent
grafana_agent_config_filename: config.yaml
# name of the environment file loaded by the system unit file
grafana_agent_env_file: service.env
# dictionary of additional custom settings for the systemd service file
grafana_agent_service_extra: {}
# temporary directory to create on the controller/localhost where the archive will be downloaded to
grafana_agent_local_tmp_dir: /tmp/grafana-agent
# data directory to create for the wal and positions
grafana_agent_data_dir: /var/lib/grafana-agent
# wal directory to use, should be a sub-folder of grafana_agent_data_dir, will automatically be created when the agent starts
grafana_agent_wal_dir: "{{ grafana_agent_data_dir }}/wal"
# positions directory to use, should be a sub-folder of grafana_agent_data_dir, will automatically be created when the agent starts
grafana_agent_positions_dir: "{{ grafana_agent_data_dir }}/positions"
# mode to run Grafana Agent in. Can be "flow" or "static".
# Docs: https://grafana.com/docs/agent/latest/flow/
grafana_agent_mode: static
# os user to create for the agent to run as
grafana_agent_user: grafana-agent
# os user group to create for the agent
grafana_agent_user_group: grafana-agent
# Configurable user groups that the grafana agent can be put in so that it can access logs
# (See https://github.com/grafana/grafana-ansible-collection/issues/40)
grafana_agent_user_groups: []
# the shell for the user
grafana_agent_user_shell: /usr/sbin/nologin
# whether or not to create a home directory for the user
grafana_agent_user_createhome: false
# full path to the local binary if already downloaded or built on the controller
# this should only be set, if ansible is not downloading the binary and you have
# manually downloaded the binary
grafana_agent_local_binary_file: ""
# dictionary of additional command-line flags, run grafana-agent --help for a complete list
# Docs: https://grafana.com/docs/agent/latest/static/configuration/flags/
grafana_agent_flags_extra:
config.expand-env: 'true'
config.enable-read-api: 'false'
server.register-instrumentation: 'true'
server.http.address: 127.0.0.1:12345
server.grpc.address: 127.0.0.1:12346
# sets Environment variables in the systemd service configuration.
grafana_agent_env_vars: {}
# dictionary of key/pair values to write to the environment file that is loaded by the service, with the flag --config.expand-env=true
# any generated config files will support the expansion of environment variables at runtime by referencing ${ENVVAR}.
# be aware of boolean values, when output they will result in the proper-cased string "True" and "False"
grafana_agent_env_file_vars: {}
# path to a config file on the controller that will be used instead of the provided configs below if specified.
grafana_agent_provisioned_config_file: ""
#################################################################################################
# Configures the server of the Agent used to enable self-scraping #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/static/configuration/server-config/
# the entire dictionary value for this object is copied to the server: block in the config file
grafana_agent_server_config:
# Log only messages with the given severity or above. Supported values [debug,
# info, warn, error]. This level affects logging for all Agent-level logs, not
# just the HTTP and gRPC server.
#
# Note that some integrations use their own loggers which ignore this
# setting.
log_level: info
#################################################################################################
# Configures metric collection #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/static/configuration/metrics-config/
# the entire dictionary value for this object is copied to the metrics: block in the config file
grafana_agent_metrics_config:
# Configure values for all Prometheus instances
# Docs: https://grafana.com/docs/agent/latest/static/configuration/metrics-config/#global_config
global:
# How frequently should Prometheus instances scrape.
scrape_interval: 1m
# How long to wait before timing out a scrape from a target.
scrape_timeout: 10s
# A dictionary of key/pair static labels to add for all metrics.
external_labels: {}
# Default set of remote_write endpoints. If an instance doesn't define any
# remote_writes, it will use this list.
# Docs: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write
remote_write: []
# The list of Prometheus instances to launch with the agent.
# Docs: https://grafana.com/docs/agent/latest/static/configuration/metrics-config/#metrics_instance_config
configs: []
# - name: name-of-scrape-job
# # Docs: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config
# scrape_configs: []
# # Optional list of remote_write targets, if not specified metrics.global.remote_write is used
# # Docs: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write
# remote_write: []
# Configure the directory used by instances to store their WAL.
#
# The Grafana Agent assumes that all folders within wal_directory are managed by
# the agent itself.
wal_directory: "{{ grafana_agent_wal_dir }}"
# Configures how long ago an abandoned (not associated with an instance) WAL
# may be written to before being eligible to be deleted
wal_cleanup_age: 12h
# Configures how often checks for abandoned WALs to be deleted are performed.
# A value of 0 disables periodic cleanup of abandoned WALs
wal_cleanup_period: 30m
#################################################################################################
# Configures logs collection #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/static/configuration/logs-config/
# the entire dictionary value for this object is copied to the logs: block in the config file
grafana_agent_logs_config:
# Directory to store Loki Promtail positions files in. Positions files are
# required to read logs, and are used to store the last read offset of log
# sources. The positions files will be stored in
# <positions_directory>/<logs_instance_config.name>.yml.
#
# Optional only if every config has a positions.filename manually provided.
#
# This directory will be automatically created if it doesn't exist.
positions_directory: "{{ grafana_agent_positions_dir }}"
# Configure values for all Loki Promtail instances.
global:
# Docs: https://grafana.com/docs/agent/latest/static/configuration/logs-config/#logs_instance_config
clients: []
#################################################################################################
# Configures traces collection #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/static/configuration/traces-config/
# the entire dictionary value for this object is copied to the traces: block in the config file
grafana_agent_traces_config:
# Docs: https://grafana.com/docs/agent/latest/static/configuration/traces-config/#traces_instance_config
configs: []
#################################################################################################
# Configures integrations for the agent #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/static/configuration/integrations/
# the entire dictionary value for this object is copied to the integrations: block in the config file
grafana_agent_integrations_config:
# Automatically collect metrics from enabled integrations. If disabled,
# integrations will be run but not scraped and thus not remote_written. Metrics
# for integrations will be exposed at /integrations/<integration_key>/metrics
# and can be scraped by an external process.
scrape_integrations: true
# Controls the Agent integration
agent:
# Enables the Agent integration, allowing the Agent to automatically
# collect and send metrics about itself.
enabled: true
# Allows for relabeling labels on the target.
relabel_configs: []
# Relabel metrics coming from the integration, allowing to drop series
# from the integration that you don't care about.
metric_relabel_configs: []
# Controls the node_exporter integration
# Docs: https://grafana.com/docs/agent/latest/static/configuration/integrations/node-exporter-config/
node_exporter:
enabled: true

View file

@ -0,0 +1,12 @@
---
- name: Restart Grafana Agent
become: true
ansible.builtin.systemd:
name: grafana-agent
state: restarted
daemon_reload: true
listen: "restart grafana-agent"
- name: Check Grafana Agent is started properly
ansible.builtin.include_tasks: ga-started.yaml
listen: "restart grafana-agent"

View file

@ -0,0 +1,25 @@
---
dependencies: []
galaxy_info:
role_name: grafana_agent
author: Ishan Jain
description: Ansible Role to deploy Grafana Agent on Linux hosts.
license: "GPL-3.0-or-later"
min_ansible_version: "2.11"
platforms:
- name: Fedora
versions:
- "all"
- name: Debian
versions:
- "all"
- name: Ubuntu
versions:
- "all"
- name: EL
versions:
- "all"
galaxy_tags:
- grafana
- observability

View file

@ -0,0 +1,56 @@
---
# these tasks are ran in both install and configure, as directories could have changed
- name: Configure directories
ansible.builtin.import_tasks: install/directories.yaml
- name: Create a symbolic link
ansible.builtin.file:
src: "{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }}"
dest: "/usr/local/bin/{{ grafana_agent_binary }}"
owner: root
group: root
state: link
- name: Overwrite/Create Grafana Agent service
ansible.builtin.template:
src: grafana-agent.service.j2
dest: "{{ _grafana_agent_systemd_dir }}/{{ _grafana_agent_systemd_unit }}"
owner: root
group: root
mode: 0644
notify: "restart grafana-agent"
- name: Create the Service Environment file
ansible.builtin.template:
src: EnvironmentFile.j2
dest: "{{ grafana_agent_config_dir }}/{{ grafana_agent_env_file }}"
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0640
notify: "restart grafana-agent"
- name: Create Grafana Agent config
ansible.builtin.template:
src: config.yaml.j2
dest: "{{ grafana_agent_config_dir }}/{{ grafana_agent_config_filename }}"
force: true
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0640
notify: "restart grafana-agent"
when: grafana_agent_provisioned_config_file | length == 0
- name: Create Grafana Agent River Config if flow mode for Grafana Agent
ansible.builtin.shell: "AGENT_MODE=flow {{ grafana_agent_install_dir }}/grafana-agent convert -f static {{ grafana_agent_config_dir }}/{{ grafana_agent_config_filename }} -o {{ grafana_agent_config_dir }}/{{ grafana_agent_config_filename }}"
notify: "restart grafana-agent"
when: grafana_agent_provisioned_config_file | length == 0
- name: Copy Grafana Agent config
ansible.builtin.copy:
src: "{{ grafana_agent_provisioned_config_file }}"
dest: "{{ grafana_agent_config_dir }}/{{ grafana_agent_config_filename }}"
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0640
notify: "restart grafana-agent"
when: grafana_agent_provisioned_config_file | length > 0

View file

@ -0,0 +1,29 @@
---
- name: Health check Grafana Agent
ansible.builtin.uri:
url: "{{ _grafana_agent_healthcheck_endpoint }}"
follow_redirects: none
method: GET
register: _result
failed_when: false
until: _result.status == 200
retries: 3
delay: 5
changed_when: false
when: not ansible_check_mode
- name: Check system logs if Grafana Agent is not started
when: not ansible_check_mode and _result.status != 200
block:
- name: Run journalctl
ansible.builtin.shell:
cmd: "journalctl -u grafana-agent -b -n20 --no-pager"
register: journal_ret
changed_when: false
- name: Output Grafana agent logs
ansible.builtin.debug:
var: journal_ret.stdout_lines
- name: Rise alerts
ansible.builtin.assert:
that: false
fail_msg: "Service grafana-agent hasn't started."

View file

@ -0,0 +1,19 @@
---
# user and group creation
- name: Configure user groups
ansible.builtin.import_tasks: install/user-group.yaml
# directory creation
- name: Configure directories
ansible.builtin.import_tasks: install/directories.yaml
# download and install agent
- name: Download and install Grafana Agent
ansible.builtin.import_tasks: install/download-install.yaml
when: (grafana_agent_local_binary_file is not defined) or (grafana_agent_local_binary_file | length == 0)
# local install of agent
- name: Local install of Grafana Agent
ansible.builtin.import_tasks:
file: install/local-install.yaml
when: __grafana_agent_local_install

View file

@ -0,0 +1,26 @@
---
- name: Create install directories
block:
- name: Create Grafana Agent install directory
ansible.builtin.file:
path: "{{ grafana_agent_install_dir }}"
state: directory
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0770
- name: Create Grafana Agent conf directory
ansible.builtin.file:
path: "{{ grafana_agent_config_dir }}"
state: directory
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0770
- name: Create Grafana Agent data directory
ansible.builtin.file:
path: "{{ grafana_agent_data_dir }}"
state: directory
owner: root
group: "{{ grafana_agent_user_group }}"
mode: 0775

View file

@ -0,0 +1,48 @@
---
- name: Download Grafana Agent binary to controller (localhost)
block:
- name: Create Grafana Agent temp directory
become: false
ansible.builtin.file:
path: "{{ grafana_agent_local_tmp_dir }}"
state: directory
mode: 0751
delegate_to: localhost
check_mode: false
run_once: true
- name: Download Grafana Agent archive to local folder
become: false
ansible.builtin.get_url:
url: "{{ _grafana_agent_download_url }}"
dest: "{{ grafana_agent_local_tmp_dir }}/grafana-agent_{{ _grafana_agent_cpu_arch }}_{{ grafana_agent_version }}.zip"
mode: 0664
register: _download_archive
until: _download_archive is succeeded
retries: 5
delay: 2
delegate_to: localhost
check_mode: false
run_once: true
- name: Extract grafana-agent.zip
become: false
ansible.builtin.unarchive:
src: "{{ grafana_agent_local_tmp_dir }}/grafana-agent_{{ _grafana_agent_cpu_arch }}_{{ grafana_agent_version }}.zip"
dest: "{{ grafana_agent_local_tmp_dir }}"
remote_src: false
delegate_to: localhost
run_once: true
- name: Set local path
ansible.builtin.set_fact:
__grafana_agent_local_binary_file: "{{ grafana_agent_local_tmp_dir }}/{{ grafana_agent_binary }}"
- name: Propagate downloaded binary
ansible.builtin.copy:
src: "{{ grafana_agent_local_tmp_dir }}/{{ _grafana_agent_download_binary_file }}"
dest: "{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }}"
mode: 0755
owner: root
group: root
when: not ansible_check_mode

View file

@ -0,0 +1,10 @@
---
- name: Install from local
block:
- name: "Propagate local binary {{ grafana_agent_local_binary_file }}"
ansible.builtin.copy:
src: "{{ grafana_agent_local_binary_file }}"
dest: "{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }}"
mode: 0755
owner: root
group: root

View file

@ -0,0 +1,38 @@
---
- name: Grafana Agent group creation
block:
- name: "Check if the group exists ({{ grafana_agent_user_group }})"
ansible.builtin.getent:
database: group
key: "{{ grafana_agent_user_group }}"
fail_key: false
- name: Set whether not the user group exists
ansible.builtin.set_fact:
__grafana_agent_user_group_exists: "{{ ansible_facts.getent_group[grafana_agent_user_group] is not none }}"
- name: Add user group "{{ grafana_agent_user_group }}"
ansible.builtin.group:
name: "{{ grafana_agent_user_group }}"
system: true
state: present
when: not __grafana_agent_user_group_exists and grafana_agent_user_group != 'root'
- name: Grafana Agent user group exists
ansible.builtin.debug:
msg: |-
The user group \"{{ grafana_agent_user_group }}\" already exists and will not be modified,
if modifying permissions please perform a separate task
when: __grafana_agent_user_group_exists
- name: Grafana Agent user creation
block:
- name: Add user "{{ grafana_agent_user }}"
ansible.builtin.user:
name: "{{ grafana_agent_user }}"
comment: "Grafana Agent account"
groups: "{{ [ grafana_agent_user_group ] + grafana_agent_user_groups }}"
system: true
shell: "{{ grafana_agent_user_shell }}"
createhome: "{{ grafana_agent_user_createhome }}"
when: grafana_agent_user != 'root'

View file

@ -0,0 +1,48 @@
---
- name: Preflight tasks
ansible.builtin.include_tasks:
file: preflight.yaml
apply:
become: true
tags:
- grafana_agent_install
- grafana_agent_configure
- grafana_agent_run
tags:
- grafana_agent_install
- grafana_agent_configure
- grafana_agent_run
- name: Install tasks
ansible.builtin.include_tasks:
file: install.yaml
apply:
become: true
tags:
- grafana_agent_install
tags:
- grafana_agent_install
- name: Configuration tasks
ansible.builtin.include_tasks:
file: configure.yaml
apply:
become: true
tags:
- grafana_agent_configure
tags:
- grafana_agent_configure
- name: Flush handlers
ansible.builtin.meta: flush_handlers
- name: Ensure Grafana Agent is started and enabled on boot
become: true
ansible.builtin.systemd:
name: grafana-agent
enabled: true
state: started
tags:
- grafana_agent_install
- grafana_agent_configure
- grafana_agent_run

View file

@ -0,0 +1,12 @@
---
- name: Preflight variable checks
ansible.builtin.import_tasks: preflight/vars.yaml
- name: Systemd variable checks
ansible.builtin.import_tasks: preflight/systemd.yaml
- name: Install variable checks
ansible.builtin.import_tasks: preflight/install.yaml
- name: Download variable checks
ansible.builtin.import_tasks: preflight/download.yaml

View file

@ -0,0 +1,36 @@
---
- name: Get Grafana Agent version from Github
when: grafana_agent_version == 'latest' and not __grafana_agent_local_install
block:
- name: Get the latest published Grafana Agent # noqa command-instead-of-module
ansible.builtin.shell: |
curl -s https://api.github.com/repos/{{ _grafana_agent_github_org }}/{{ _grafana_agent_github_repo }}/releases/latest \
| grep -m 1 tag_name \
| cut -d '"' -f 4 | cut -c 2-
changed_when: false
run_once: true
delegate_to: localhost
become: false
register: _grafana_agent_version_request
- name: Fail if cannot get Grafana Agent Version
ansible.builtin.fail:
msg: Issue getting the Grafana Agent Version
when: _grafana_agent_version_request == ""
- name: Set the Grafana Agent version
ansible.builtin.set_fact:
grafana_agent_version: "{{ _grafana_agent_version_request.stdout }}"
- name: Grafana Agent version to download
ansible.builtin.debug:
var: grafana_agent_version
- name: Set the Grafana Agent download URL
ansible.builtin.set_fact:
_grafana_agent_download_url: |-
{{ grafana_agent_base_download_url }}/v{{ grafana_agent_version }}/{{ _grafana_agent_download_archive_file }}
- name: Grafana Agent download URL
ansible.builtin.debug:
var: _grafana_agent_download_url

View file

@ -0,0 +1,72 @@
---
- name: Default to non-local install
ansible.builtin.set_fact:
__grafana_agent_local_install: false
- name: Fail when grafana_agent_local_binary_file is defined but the file doesn't exist
when: grafana_agent_local_binary_file is defined and grafana_agent_local_binary_file | length > 0
block:
- name: Check if grafana_agent_local_binary_file exists
ansible.builtin.stat:
path: "{{ grafana_agent_local_binary_file }}"
register: __grafana_agent_local_binary_check
become: false
delegate_to: localhost
check_mode: false
- name: Fail when the grafana_agent_local_binary_file does not exist
ansible.builtin.fail:
msg: "The grafana_agent_local_binary_file ({{ grafana_agent_local_binary_file }}) was specified but does not exist"
when: not __grafana_agent_local_binary_check.stat.exists
- name: Change to local install
ansible.builtin.set_fact:
__grafana_agent_local_install: true
- name: Fail when grafana_agent_provisioned_config_file is defined but the file doesn't exist
when: grafana_agent_provisioned_config_file is defined and grafana_agent_provisioned_config_file | length > 0
block:
- name: Check if grafana_agent_provisioned_config_file exists
ansible.builtin.stat:
path: "{{ grafana_agent_provisioned_config_file }}"
register: __grafana_agent_provisioned_config_file_check
become: false
delegate_to: localhost
check_mode: false
- name: Fail when the grafana_agent_provisioned_config_file does not exist
ansible.builtin.fail:
msg: "The grafana_agent_provisioned_config_file ({{ grafana_agent_provisioned_config_file }}) was specified but does not exist"
when: not __grafana_agent_provisioned_config_file_check.stat.exists
- name: Check if grafana_agent is already installed on the host
ansible.builtin.stat:
path: "{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }}"
register: __grafana_agent_is_installed
check_mode: false
- name: Is Grafana Agent already installed on the host
ansible.builtin.debug:
var: __grafana_agent_is_installed.stat.exists
- name: Install checks
when: __grafana_agent_is_installed.stat.exists and not __grafana_agent_local_install
block:
- name: Gather currently installed grafana-agent version from the host
ansible.builtin.shell:
cmd: |
{{ grafana_agent_install_dir }}/{{ grafana_agent_binary }} --version | \
head -n 1 | \
awk '{ print $3; }' | \
cut -d 'v' -f 2
changed_when: false
register: __grafana_agent_current_version_output
check_mode: false
- name: Set Grafana Agent installed version for the host
ansible.builtin.set_fact:
__grafana_agent_installed_version: "{{ __grafana_agent_current_version_output.stdout }}"
- name: Grafana Agent installed version on host
ansible.builtin.debug:
var: __grafana_agent_installed_version

View file

@ -0,0 +1,28 @@
---
- name: Assert usage of systemd as an init system
ansible.builtin.assert:
that: ansible_facts['service_mgr'] == 'systemd'
msg: This role only works with systemd
- name: Get systemd version # noqa command-instead-of-module
ansible.builtin.command: systemctl --version
changed_when: false
check_mode: false
register: __systemd_version
- name: Set systemd version fact
ansible.builtin.set_fact:
grafana_agent_systemd_version: "{{ __systemd_version.stdout_lines[0] | regex_replace('^systemd\\s(\\d+).*$', '\\1') }}"
- name: Fail when _grafana_agent_systemd_dir the directory doesn't exist
block:
- name: Check if _grafana_agent_systemd_dir exists
ansible.builtin.stat:
path: "{{ _grafana_agent_systemd_dir }}"
register: ___grafana_agent_systemd_dir_check
check_mode: false
- name: Fail when the _grafana_agent_systemd_dir directory does not exist
ansible.builtin.fail:
msg: "The _grafana_agent_systemd_dir ({{ _grafana_agent_systemd_dir }}) does not exist"
when: not ___grafana_agent_systemd_dir_check.stat.exists

View file

@ -0,0 +1,72 @@
---
# Performs initial variable validation
- name: Fail when variables are not defined
ansible.builtin.fail:
msg: "The {{ item }} property must be set"
when: ( vars[item] is not defined )
with_items:
- grafana_agent_version
- grafana_agent_install_dir
- grafana_agent_binary
- grafana_agent_config_dir
- grafana_agent_config_filename
- grafana_agent_env_file
- grafana_agent_local_tmp_dir
- grafana_agent_wal_dir
- grafana_agent_positions_dir
- grafana_agent_mode
- _grafana_agent_systemd_dir
- _grafana_agent_systemd_unit
- grafana_agent_user
- grafana_agent_user_group
- grafana_agent_user_shell
- grafana_agent_user_createhome
- grafana_agent_local_binary_file
- grafana_agent_flags_extra
- grafana_agent_env_vars
- grafana_agent_env_file_vars
- grafana_agent_provisioned_config_file
- grafana_agent_metrics_config
- grafana_agent_logs_config
- grafana_agent_traces_config
- grafana_agent_integrations_config
- name: Fail when variables do not have a length
ansible.builtin.fail:
msg: "The {{ item }} property must be valued"
when: ( vars[item] | string | length == 0 )
with_items:
- grafana_agent_version
- grafana_agent_install_dir
- grafana_agent_binary
- grafana_agent_config_dir
- grafana_agent_config_filename
- grafana_agent_env_file
- grafana_agent_local_tmp_dir
- grafana_agent_wal_dir
- grafana_agent_positions_dir
- grafana_agent_mode
- _grafana_agent_systemd_dir
- _grafana_agent_systemd_unit
- grafana_agent_user
- grafana_agent_user_group
- grafana_agent_user_shell
- grafana_agent_user_createhome
- name: Fail when variables are not a number
ansible.builtin.fail:
msg: "The {{ item }} property must be number"
when: ( vars[item] is defined and vars[item] is not number)
with_items: []
- name: Fail when flags are not a boolean
ansible.builtin.fail:
msg: "The {{ item }} property must be a boolean true or false"
when: ( vars[item] | bool | string | lower ) not in ['true', 'false']
with_items:
- grafana_agent_user_createhome
- name: Fail when the agent mode is not "flow" or "static"
ansible.builtin.fail:
msg: "The grafana_agent_mode property must be a boolean 'flow' or 'static'"
when: grafana_agent_mode not in ['flow', 'static']

View file

@ -0,0 +1,9 @@
{{ ansible_managed | comment }}
# Grafana Agent Environment File
AGENT_MODE={{ grafana_agent_mode }}
GOMAXPROCS={{ ansible_facts['processor_vcpus']|default(ansible_facts['processor_count']) }}
{% for key, value in grafana_agent_env_file_vars.items() %}
{{key}}={{value}}
{% endfor %}

View file

@ -0,0 +1,39 @@
---
{{ ansible_managed | comment }}
#################################################################################################
# Configures the server of the Agent used to enable self-scraping #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/configuration/server-config/
server:
{{ grafana_agent_server_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }}
#################################################################################################
# Configures metric collection #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/configuration/metrics-config/
metrics:
{{ grafana_agent_metrics_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }}
#################################################################################################
# Configures logs collection #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/configuration/logs-config/
logs:
{{ grafana_agent_logs_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }}
{% if grafana_agent_mode == 'static' %}
#################################################################################################
# Configures traces collection #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/configuration/traces-config/
traces:
{{ grafana_agent_traces_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }}
{% endif %}
#################################################################################################
# Configures integrations for the agent #
#################################################################################################
# Docs: https://grafana.com/docs/agent/latest/configuration/integrations/
integrations:
{{ grafana_agent_integrations_config | to_nice_yaml(indent=2,sort_keys=False) | indent(2) }}

View file

@ -0,0 +1,65 @@
{{ ansible_managed | comment }}
[Unit]
Description=Grafana Agent
Documentation=https://grafana.com/docs/agent/latest/
After=network-online.target
[Service]
Type=simple
User={{ grafana_agent_user }}
Group={{ grafana_agent_user_group }}
WorkingDirectory={{ grafana_agent_data_dir }}
{% for key, value in grafana_agent_env_vars.items() %}
Environment={{key}}={{value}}
{% endfor %}
EnvironmentFile={{ grafana_agent_config_dir }}/{{ grafana_agent_env_file}}
{% for key, value in grafana_agent_service_extra.items() %}
{{ key }}={{ value }}
{% endfor %}
{% if grafana_agent_mode == 'flow' %}
ExecStart={{ grafana_agent_install_dir }}/{{ grafana_agent_binary }} run \
{% for flag, flag_value in grafana_agent_flags_extra.items() %}
{% if not flag_value %}
--{{ flag }} \
{% elif flag_value is string %}
--{{ flag }}={{ flag_value }} \
{% elif flag_value is sequence %}
{% for flag_value_item in flag_value %}
--{{ flag }}={{ flag_value_item }} \
{% endfor %}
{% endif %}
{% endfor %}
{{ grafana_agent_config_dir }}/{{ grafana_agent_config_filename }}
{% else %}
ExecStart={{ grafana_agent_install_dir }}/{{ grafana_agent_binary }} \
{% for flag, flag_value in grafana_agent_flags_extra.items() %}
{% if not flag_value %}
--{{ flag }} \
{% elif flag_value is string %}
--{{ flag }}={{ flag_value }} \
{% elif flag_value is sequence %}
{% for flag_value_item in flag_value %}
--{{ flag }}={{ flag_value_item }} \
{% endfor %}
{% endif %}
{% endfor %}
--config.file={{ grafana_agent_config_dir }}/{{ grafana_agent_config_filename }}
{% endif %}
SyslogIdentifier=grafana-agent
Restart=always
{% if grafana_agent_systemd_version | int >= 232 %}
ProtectSystem=strict
ProtectControlGroups=true
ProtectKernelModules=true
ProtectKernelTunables=yes
{% else %}
ProtectSystem=full
{% endif %}
ReadWritePaths=/tmp {{ grafana_agent_data_dir }} {{ grafana_agent_positions_dir }} {{ grafana_agent_wal_dir }}
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,29 @@
---
_grafana_agent_github_org: grafana
_grafana_agent_github_repo: agent
# set the go cpu arch
_download_cpu_arch_map:
i386: '386'
x86_64: amd64
aarch64: arm64
armv7l: armv7
armv6l: armv6
_grafana_agent_cpu_arch: "{{ _download_cpu_arch_map[ansible_facts['architecture']] | default(ansible_facts['architecture']) }}"
# set the go os family
_grafana_agent_os_family: "{{ ansible_facts['system'] | lower }}"
# set the name of the archive file to download
_grafana_agent_download_archive_file: "grafana-agent-{{ _grafana_agent_os_family }}-{{ _grafana_agent_cpu_arch }}.zip"
# set the name of the binary file
_grafana_agent_download_binary_file: "grafana-agent-{{ _grafana_agent_os_family }}-{{ _grafana_agent_cpu_arch }}"
# systemd info
_grafana_agent_systemd_dir: /lib/systemd/system/
_grafana_agent_systemd_unit: grafana-agent.service
# Server http address, used in self health check after start
_grafana_agent_healthcheck_endpoint: "http://{{ grafana_agent_flags_extra['server.http.address'] if grafana_agent_flags_extra['server.http.address'] is defined else '127.0.0.1:12345' }}/-/ready"