Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ansible role for Alloy #169

Merged
merged 9 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/agent_flow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- name: Install agent_flow
hosts: all
become: true

tasks:
- name: Install agent_flow
ansible.builtin.include_role:
name: grafana.grafana.agent_flow
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"
}
}
}
69 changes: 69 additions & 0 deletions roles/agent_flow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Ansible Role for Agent

This Ansible role to install and configure the Agent, 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 the Grafana Agent to be installed. | "0.40.3" |
| `arch_mapping` | A mapping of common architecture names to Grafana Agent 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 the Grafana Agent binary for the specific version and arch. | Constructed URL based on `version` and `arch` variables. |
| `service_name` | The name to be used for the Grafana Agent service. | "agent_flow" |
| `installation_dir` | Directory where the Grafana Agent is to be installed. | "/etc/agent_flow" |
| `environment_file` | Name of the environment file for the Grafana Agent service. | "service.env" |
| `config_dir` | Directory for the Grafana Agent configuration. | "/etc/agent_flow" |
| `config_file` | Configuration file name for the Grafana Agent. | "config.river" |
| `service_user` | User under which the Grafana Agent service will run. | "agent_flow" |
| `service_group` | Group under which the Grafana Agent service will run. | "agent_flow" |
| `working_dir` | Working directory for the Grafana Agent service. | "/etc/agent_flow" |
| `env_file_vars` | Additional environment variables to be set in the service env file. | {} (Empty dictionary) |
| `config` | Configuration template for the Grafana Agent. | Configuration script with prometheus scrape and remote_write setup |


## Example Playbook

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

tasks:
- name: Install agent_flow
ansible.builtin.include_role:
name: grafana.grafana.agent_flow
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"
}
}
}
```

## License

See [LICENSE](https://github.com/grafana/grafana-ansible-collection/blob/main/LICENSE)

## Author Information

- [Ishan Jain](https://github.com/ishanjainn)
43 changes: 43 additions & 0 deletions roles/agent_flow/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: "0.40.3"

Check failure on line 1 in roles/agent_flow/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 agent_flow_ as a prefix. (vars: version)

arch_mapping:

Check failure on line 3 in roles/agent_flow/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 agent_flow_ 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/agent_flow/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 agent_flow_ as a prefix. (vars: arch)

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

Check failure on line 12 in roles/agent_flow/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 agent_flow_ as a prefix. (vars: binary_url)

service_name: "agent_flow"

Check failure on line 14 in roles/agent_flow/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 agent_flow_ as a prefix. (vars: service_name)

installation_dir: "/etc/agent_flow"

Check failure on line 16 in roles/agent_flow/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 agent_flow_ as a prefix. (vars: installation_dir)

environment_file: "service.env"

Check failure on line 18 in roles/agent_flow/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 agent_flow_ as a prefix. (vars: environment_file)

config_dir: "/etc/agent_flow"

Check failure on line 20 in roles/agent_flow/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 agent_flow_ as a prefix. (vars: config_dir)

config_file: "config.river"

Check failure on line 22 in roles/agent_flow/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 agent_flow_ as a prefix. (vars: config_file)

service_user: "agent_flow"

service_group: "agent_flow"

working_dir: "/etc/agent_flow"

env_file_vars: {}

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/agent_flow/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: Restart agent_flow
ansible.builtin.systemd:
name: "{{ service_name }}"
state: restarted
become: true
24 changes: 24 additions & 0 deletions roles/agent_flow/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 agent_flow
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/agent_flow/tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- name: Create agent_flow config directory
ansible.builtin.file:
path: "{{ config_dir }}"
state: directory
owner: "{{ service_user }}"
group: "{{ service_group }}"
mode: '0755'
become: true

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

- name: Deploy agent_flow environment file
ansible.builtin.template:
src: environment.j2
dest: "{{ config_dir }}/{{ environment_file }}"
owner: "{{ service_user }}"
group: "{{ service_group }}"
mode: '0644'
notify: Restart agent_flow
become: true
51 changes: 51 additions & 0 deletions roles/agent_flow/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 agent_flow group
ansible.builtin.group:
name: "{{ service_group }}"
system: true
become: true

- name: Create agent_flow 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 agent_flow binary
ansible.builtin.get_url:
url: "{{ binary_url }}"
dest: "/tmp/agent_flow-{{ version }}.zip"
mode: '0755'
become: true
register: download_result

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

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

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

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

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

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

- name: Ensure agent_flow service is enabled and running
ansible.builtin.service:
name: "{{ service_name }}"
enabled: yes
state: started
become: true
20 changes: 20 additions & 0 deletions roles/agent_flow/templates/agent_flow.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Unit]
Description=Vendor-neutral programmable observability pipelines.
Wants=network-online.target
After=network-online.target

[Service]
Restart=always
User={{ service_user }}
Group={{ service_group }}
Environment=HOSTNAME=%H
Environment="AGENT_MODE=flow"
EnvironmentFile={{ installation_dir }}/{{ environment_file }}
WorkingDirectory={{ working_dir }}
ExecStart={{ installation_dir }}/grafana-agent-linux-{{ arch }} run $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/agent_flow/templates/config.river.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ config }}
10 changes: 10 additions & 0 deletions roles/agent_flow/templates/environment.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{ ansible_managed | comment }}
# Grafana Agent 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 %}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ service:
{% if otel_collector_connectors is defined and otel_collector_connectors | length > 0 %}
connectors:
{{ otel_collector_connectors | to_nice_yaml(indent=2) | indent(2, true) }}
{% endif %}
{% endif %}
Loading