Skip to content

Commit

Permalink
Add ansible role for Alloy (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanjainn authored Apr 10, 2024
1 parent 19e6258 commit 6a232c4
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/alloy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- name: Install alloy
hosts: all
become: true

tasks:
- name: Install alloy
ansible.builtin.include_role:
name: grafana.grafana.alloy
vars:
config: |
prometheus.scrape "default" {
targets = [{"__address__" = "localhost:12345"}]
forward_to = [prometheus.remote_write.prom.receiver]
}
prometheus.remote_write "prom" {
endpoint {
url = "https://prometheus-prod-13-prod-us-east-0.grafana.net/api/prom/push"
basic_auth {
username = "1493467"
password = "glc_eyJvIjoiNjUyOTkyIiwibiI6InN0YWNrLTg5MDA0My1obS13cml0ZS1hc2FzIiwiayI6IjIwME9NeThmWlFpMGlmQzBGMTlJNDdqSiIsIm0iOnsiciI6InByb2QtdXMtZWFzdC0wIn19"
}
}
}
71 changes: 71 additions & 0 deletions roles/alloy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Ansible Role for Alloy

This Ansible role to install and configure [Alloy](https://grafana.com/docs/alloy/latest/), which can be used to collect traces, metrics, and logs.

## Requirements

Please ensure that `curl` is intalled on Ansible controller.

## Role Variables

Available variables with their default values are listed below (`defaults/main.yml`):

## Role Variables

| Variable Name | Description | Default Value |
|-----------------------|----------------------------------------------------------------------|---------------------------------------------------------------------|
| `version` | The version of Grafana Alloy to be installed. | "1.0.0" |
| `arch_mapping` | A mapping of common architecture names to Grafana Alloy binaries. | `{'x86_64': 'amd64', 'aarch64': 'arm64', 'armv7l': 'armhf', 'i386': 'i386', 'ppc64le': 'ppc64le'}` |
| `arch` | The architecture of the current machine. | Based on `ansible_architecture` lookup, defaults to 'amd64'. |
| `binary_url` | URL to Grafana Alloy binary for the specific version and architecture. | Constructed URL based on `version` and `arch` variables. |
| `service_name` | The name to be used for the Grafana Alloy service. | "alloy" |
| `installation_dir` | Directory where Grafana Alloy is to be installed. | "/etc/alloy" |
| `environment_file` | Name of the environment file for the Grafana Alloy service. | "service.env" |
| `config_dir` | Directory for Grafana Alloy configuration. | "/etc/alloy" |
| `config_file` | Configuration file name for Grafana Alloy. | "config.river" |
| `service_user` | User under which the Grafana Alloy service will run. | "alloy" |
| `service_group` | Group under which the Grafana Alloy service will run. | "alloy" |
| `working_dir` | Working directory for the Grafana Alloy service. | "/etc/alloy" |
| `env_file_vars` | Additional environment variables to be set in the service environment file. | {} (Empty dictionary) |
| `alloy_flags_extra` | Extra flags to pass to the Alloy service. | {} (Empty dictionary) |
| `start_after_service` | Specify an optional dependency service Alloy should start after. | '' (Empty string) |
| `config` | Configuration template for Grafana Alloy. | Configuration script with Prometheus scrape and remote_write setup |


## Example Playbook

Including an example of how to use your role:
```yaml
- name: Install alloy
hosts: all
become: true

tasks:
- name: Install alloy
ansible.builtin.include_role:
name: grafana.grafana.alloy
vars:
config: |
prometheus.scrape "default" {
targets = [{"__address__" = "localhost:12345"}]
forward_to = [prometheus.remote_write.prom.receiver]
}
prometheus.remote_write "prom" {
endpoint {
url = "https://prometheus-prod-13-prod-us-east-0.grafana.net/api/prom/push"
basic_auth {
username = "149xxx"
password = "glc_xxx"
}
}
}
```
## License
See [LICENSE](https://github.com/grafana/grafana-ansible-collection/blob/main/LICENSE)
## Author Information
- [Ishan Jain](https://github.com/ishanjainn)
45 changes: 45 additions & 0 deletions roles/alloy/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: "1.0.0"

Check failure on line 1 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: version)

arch_mapping:

Check failure on line 3 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: arch_mapping)
x86_64: amd64
aarch64: arm64
armv7l: armhf
i386: i386
ppc64le: ppc64le

arch: "{{ arch_mapping[ansible_architecture] | default('amd64') }}"

Check failure on line 10 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: arch)

binary_url: "https://github.com/grafana/alloy/releases/download/v{{ version }}/alloy-linux-{{ arch }}.zip"

Check failure on line 12 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: binary_url)

service_name: "alloy"

Check failure on line 14 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: service_name)

installation_dir: "/etc/alloy"

Check failure on line 16 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: installation_dir)

environment_file: "service.env"

Check failure on line 18 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: environment_file)

config_dir: "/etc/alloy"

Check failure on line 20 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: config_dir)

config_file: "config.alloy"

Check failure on line 22 in roles/alloy/defaults/main.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

var-naming[no-role-prefix]

Variables names from within roles should use alloy_ as a prefix. (vars: config_file)

service_user: "alloy"

service_group: "alloy"

working_dir: "/etc/alloy"

env_file_vars: {}

alloy_flags_extra: {}

start_after_service: ''

config: |
prometheus.scrape "default" {
targets = [{"__address__" = "localhost:12345"}]
forward_to = [prometheus.remote_write.prom.receiver]
}
prometheus.remote_write "prom" {
endpoint {
url = "http://mimir:9009/api/v1/push"
}
}
5 changes: 5 additions & 0 deletions roles/alloy/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: Restart alloy
ansible.builtin.systemd:
name: "{{ service_name }}"
state: restarted
become: true
24 changes: 24 additions & 0 deletions roles/alloy/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
galaxy_info:
author: Ishan Jain
description: Role to Install and Configure Grafana Alloy
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
- monitoring
- opentelemetry
- telemetry
28 changes: 28 additions & 0 deletions roles/alloy/tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- name: Create alloy config directory
ansible.builtin.file:
path: "{{ config_dir }}"
state: directory
owner: "{{ service_user }}"
group: "{{ service_group }}"
mode: '0755'
become: true

- name: Deploy alloy configuration file
ansible.builtin.template:
src: config.alloy.j2
dest: "{{ config_dir }}/{{ config_file }}"
owner: "{{ service_user }}"
group: "{{ service_group }}"
mode: '0644'
notify: Restart alloy
become: true

- name: Deploy alloy environment file
ansible.builtin.template:
src: environment.j2
dest: "{{ config_dir }}/{{ environment_file }}"
owner: "{{ service_user }}"
group: "{{ service_group }}"
mode: '0644'
notify: Restart alloy
become: true
51 changes: 51 additions & 0 deletions roles/alloy/tasks/install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
- name: Install unzip
ansible.builtin.package:
name: unzip
state: present
become: true

- name: Create alloy group
ansible.builtin.group:
name: "{{ service_group }}"
system: true
become: true

- name: Create alloy user
ansible.builtin.user:
name: "{{ service_user }}"
group: "{{ service_group }}"
system: true
create_home: false # Appropriate for a system user, usually doesn't need a home directory
become: true

- name: Download alloy binary
ansible.builtin.get_url:
url: "{{ binary_url }}"
dest: "/tmp/alloy-{{ version }}.zip"
mode: '0755'
become: true
register: download_result

- name: Remove existing alloy installation directory
ansible.builtin.file:
path: "{{ installation_dir }}"
state: absent
become: true
when: download_result.changed

- name: Create alloy installation directory
ansible.builtin.file:
path: "{{ installation_dir }}"
state: directory
mode: '0755'
owner: "{{ service_user }}"
group: "{{ service_group }}"
become: true

- name: Extract alloy binary
ansible.builtin.unarchive:
src: "/tmp/alloy-{{ version }}.zip"
dest: "{{ installation_dir }}"
remote_src: yes
become: true
register: extract_result
11 changes: 11 additions & 0 deletions roles/alloy/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- name: Install alloy
include_tasks: install.yml
tags: [install]

- name: Configure alloy
include_tasks: configure.yml
tags: [configure]

- name: Manage alloy service
include_tasks: service.yml
tags: [service]
19 changes: 19 additions & 0 deletions roles/alloy/tasks/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- name: Copy alloy systemd unit file
ansible.builtin.template:
src: alloy.service.j2
dest: /etc/systemd/system/{{ service_name }}.service
mode: '0644'
become: true
notify: Restart alloy

- name: Reload systemd daemon to pick up changes
ansible.builtin.systemd:
daemon_reload: yes
become: true

- name: Ensure alloy service is enabled and running
ansible.builtin.service:
name: "{{ service_name }}"
enabled: yes
state: started
become: true
32 changes: 32 additions & 0 deletions roles/alloy/templates/alloy.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[Unit]
Description=Vendor-neutral programmable observability pipelines.
Documentation=https://grafana.com/docs/alloy/
Wants=network-online.target
After=network-online.target{{ ' ' + start_after_service if start_after_service is defined else '' }}

[Service]
Restart=always
User={{ service_user }}
Group={{ service_group }}
Environment=HOSTNAME=%H
EnvironmentFile={{ installation_dir }}/{{ environment_file }}
WorkingDirectory={{ working_dir }}
ExecStart={{ installation_dir }}/alloy-linux-{{ arch }} run \
{% for flag, flag_value in alloy_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 %}
$CUSTOM_ARGS --storage.path={{ working_dir }} $CONFIG_FILE
ExecReload=/usr/bin/env kill -HUP $MAINPID
TimeoutStopSec=20s
SendSIGKILL=no

[Install]
WantedBy=multi-user.target
1 change: 1 addition & 0 deletions roles/alloy/templates/config.alloy.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ config }}
10 changes: 10 additions & 0 deletions roles/alloy/templates/environment.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{ ansible_managed | comment }}
# Grafana Alloy Environment File
CONFIG_FILE="{{ config_dir }}/{{ config_file }}"

GOMAXPROCS={{ ansible_processor_vcpus|default(ansible_processor_count) }}
RESTART_ON_UPGRADE=true

{% for key, value in env_file_vars.items() %}
{{key}}={{value}}
{% endfor %}
3 changes: 3 additions & 0 deletions roles/grafana_agent/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
|![](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
Expand Down

0 comments on commit 6a232c4

Please sign in to comment.