Skip to content

Commit

Permalink
Merge branch 'main' into otel-collector-check-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanjainn authored Oct 15, 2024
2 parents 56af322 + 5489609 commit 297274e
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 69 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ collections:

## Roles included in the collection

This collection includes the following roles to help set up and manage Grafana, Grafana Agent, OpenTelemetry Collector, Loki, Mimir and Promtail:
This collection includes the following roles to help set up and manage Grafana, Grafana Agent, Alloy, OpenTelemetry Collector, Loki, Mimir and Promtail:

- **Grafana**: Installs and configures Grafana on your target hosts.
- **Grafana Agent**: Deploys and configures Grafana Agent, allowing for efficient metrics, logs, and trace data shipping to Grafana Cloud or other endpoints.
- **Alloy**: The replacement for Grafana Agent. Alloy can be used to collect traces, metrics, and logs.
- **OpenTelemetry Collector**: Sets up and configures the OpenTelemetry Collector, enabling advanced observability features through data collection and transmission.
- **Loki**: Deploy and manage Loki, the log aggregation system.
- **Mimir**: Deploy and manage Mimir, the scalable long-term storage for Prometheus.
Expand Down
36 changes: 17 additions & 19 deletions roles/alloy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This Ansible role to install and configure [Alloy](https://grafana.com/docs/allo

## Requirements

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

## Role Variables

Expand All @@ -14,24 +14,22 @@ Available variables with their default values are listed below (`defaults/main.y

| 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 binary is to be installed. Must be present. | "/usr/local/bin" |
| `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.alloy" |
| `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. | "/var/lib/alloy" |
| `env_file_vars` | Additional environment variables to be set in the service environment file. | {} (Empty dictionary) |
| `alloy_version` | The version of Grafana Alloy to be installed. | "1.0.0" |
| `alloy_arch_mapping` | A mapping of common architecture names to Grafana Alloy binaries. | `{'x86_64': 'amd64', 'aarch64': 'arm64', 'armv7l': 'armhf', 'i386': 'i386', 'ppc64le': 'ppc64le'}` |
| `alloy_arch` | The architecture of the current machine. | Based on `ansible_architecture` lookup, defaults to 'amd64'. |
| `alloy_binary_url` | URL to Grafana Alloy binary for the specific version and architecture. | Constructed URL based on `version` and `arch` variables. |
| `alloy_service_name` | The name to be used for the Grafana Alloy service. | "alloy" |
| `alloy_installation_dir` | Directory where Grafana Alloy is to be installed. | "/etc/alloy" |
| `alloy_environment_file` | Name of the environment file for the Grafana Alloy service. | "service.env" |
| `alloy_config_dir` | Directory for Grafana Alloy configuration. | "/etc/alloy" |
| `alloy_config_file` | Configuration file name for Grafana Alloy. | "config.river" |
| `alloy_service_user` | User under which the Grafana Alloy service will run. | "alloy" |
| `alloy_service_group` | Group under which the Grafana Alloy service will run. | "alloy" |
| `alloy_working_dir` | Working directory for the Grafana Alloy service. | "/etc/alloy" |
| `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 |
| `alloy_user_groups`. | Configurable user groups that the Grafana Alloy can be put in so that it can access logs. | `[]` |

| `alloy_start_after_service` | Specify an optional dependency service Alloy should start after. | '' (Empty string) |
| `alloy_config` | Configuration template for Grafana Alloy. | Configuration script with Prometheus scrape and remote_write setup |


## Example Playbook
Expand All @@ -47,7 +45,7 @@ Including an example of how to use your role:
ansible.builtin.include_role:
name: grafana.grafana.alloy
vars:
config: |
alloy_config: |
prometheus.scrape "default" {
targets = [{"__address__" = "localhost:12345"}]
forward_to = [prometheus.remote_write.prom.receiver]
Expand Down
31 changes: 16 additions & 15 deletions roles/alloy/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
version: "1.0.0"
alloy_version: "1.3.1"

arch_mapping:

alloy_arch_mapping:
x86_64: amd64
aarch64: arm64
armv7l: armhf
i386: i386
ppc64le: ppc64le

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

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

service_name: "alloy"
alloy_service_name: "alloy"

installation_dir: "/usr/local/bin"
alloy_working_dir: "/opt/alloy"

environment_file: "service.env"
alloy_installation_dir: "{{ alloy_working_dir }}/bin"

config_dir: "/etc/alloy"
alloy_environment_file: "service.env"

config_file: "config.alloy"
alloy_config_dir: "/etc/alloy"

service_user: "alloy"
alloy_config_file: "config.alloy"

service_group: "alloy"
alloy_service_user: "alloy"

working_dir: "/var/lib/alloy"
alloy_service_group: "alloy"

env_file_vars: {}
alloy_env_file_vars: {}

alloy_flags_extra: {}

start_after_service: ''
alloy_start_after_service: ''

config: |
alloy_config: |
prometheus.scrape "default" {
targets = [{"__address__" = "localhost:12345"}]
forward_to = [prometheus.remote_write.prom.receiver]
Expand Down
2 changes: 1 addition & 1 deletion roles/alloy/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- name: Restart alloy
ansible.builtin.systemd:
name: "{{ service_name }}"
name: "{{ alloy_service_name }}"
state: restarted
become: true
listen: "Restart alloy"
Expand Down
18 changes: 9 additions & 9 deletions roles/alloy/tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@

- name: Create alloy config directory
ansible.builtin.file:
path: "{{ config_dir }}"
path: "{{ alloy_config_dir }}"
state: directory
owner: "{{ service_user }}"
group: "{{ service_group }}"
owner: "{{ alloy_service_user }}"
group: "{{ alloy_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 }}"
dest: "{{ alloy_config_dir }}/{{ alloy_config_file }}"
owner: "{{ alloy_service_user }}"
group: "{{ alloy_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 }}"
dest: "{{ alloy_config_dir }}/{{ alloy_environment_file }}"
owner: "{{ alloy_service_user }}"
group: "{{ alloy_service_group }}"
mode: '0644'
notify: Restart alloy
become: true
38 changes: 28 additions & 10 deletions roles/alloy/tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,55 @@

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

- name: Create alloy user
ansible.builtin.user:
name: "{{ service_user }}"
groups: "{{ [ service_group ] + alloy_user_groups }}"
name: "{{ alloy_service_user }}"

Check warning on line 15 in roles/alloy/tasks/install.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

jinja[spacing]

Jinja2 spacing could be improved: {{ [ alloy_service_group ] + alloy_user_groups }} -> {{ \[alloy_service_group] + alloy_user_groups }}
group: "{{ [ alloy_service_group ] + alloy_user_groups }}"
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"
url: "{{ alloy_binary_url }}"
dest: "/tmp/alloy-{{ alloy_version }}.zip"
mode: '0755'
become: true
register: download_result
register: alloy_download_result

- name: Remove existing alloy binary

Check failure on line 29 in roles/alloy/tasks/install.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

no-handler

Tasks that run when changed should likely be handlers.
ansible.builtin.file:
path: "{{ installation_dir }}/alloy-linux-{{ arch }}"
path: "{{ alloy_installation_dir }}"
state: absent
become: true
when: download_result.changed
when: alloy_download_result.changed

- name: Create alloy working directory
ansible.builtin.file:
path: "{{ alloy_working_dir }}"
state: directory
mode: '0755'
owner: "{{ alloy_service_user }}"
group: "{{ alloy_service_group }}"
become: true

- name: Create alloy installation directory
ansible.builtin.file:
path: "{{ alloy_installation_dir }}"
state: directory
mode: '0755'
owner: "{{ alloy_service_user }}"
group: "{{ alloy_service_group }}"
become: true

- name: Extract alloy binary
ansible.builtin.unarchive:
src: "/tmp/alloy-{{ version }}.zip"
dest: "{{ installation_dir }}"
src: "/tmp/alloy-{{ alloy_version }}.zip"
dest: "{{ alloy_installation_dir }}"
remote_src: yes
become: true
register: extract_result
6 changes: 3 additions & 3 deletions roles/alloy/tasks/service.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
- name: Copy alloy systemd unit file
ansible.builtin.template:
src: alloy.service.j2
dest: /etc/systemd/system/{{ service_name }}.service
mode: "0644"
dest: /etc/systemd/system/{{ alloy_service_name }}.service
mode: '0644'
become: true
notify: Restart alloy

Expand All @@ -16,7 +16,7 @@

- name: Ensure alloy service is enabled and running
ansible.builtin.service:
name: "{{ service_name }}"
name: "{{ alloy_service_name }}"
enabled: yes
state: started
become: true
12 changes: 6 additions & 6 deletions roles/alloy/templates/alloy.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ After=network-online.target{{ ' ' + start_after_service if start_after_service i

[Service]
Restart=always
User={{ service_user }}
Group={{ service_group }}
User={{ alloy_service_user }}
Group={{ alloy_service_group }}
Environment=HOSTNAME=%H
EnvironmentFile={{ config_dir }}/{{ environment_file }}
WorkingDirectory={{ working_dir }}
ExecStart={{ installation_dir }}/alloy-linux-{{ arch }} run \
EnvironmentFile={{ alloy_config_dir }}/{{ alloy_environment_file }}
WorkingDirectory={{ alloy_working_dir }}
ExecStart={{ alloy_installation_dir }}/alloy-linux-{{ alloy_arch }} run \
{% for flag, flag_value in alloy_flags_extra.items() %}
{% if not flag_value %}
--{{ flag }} \
Expand All @@ -23,7 +23,7 @@ ExecStart={{ installation_dir }}/alloy-linux-{{ arch }} run \
{% endfor %}
{% endif %}
{% endfor %}
$CUSTOM_ARGS --storage.path={{ working_dir }} $CONFIG_FILE
$CUSTOM_ARGS --storage.path={{ alloy_working_dir }} $CONFIG_FILE
ExecReload=/usr/bin/env kill -HUP $MAINPID
TimeoutStopSec=20s
SendSIGKILL=no
Expand Down
2 changes: 1 addition & 1 deletion roles/alloy/templates/config.alloy.j2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ config }}
{{ alloy_config }}
4 changes: 2 additions & 2 deletions roles/alloy/templates/environment.j2
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{ ansible_managed | comment }}
# Grafana Alloy Environment File
CONFIG_FILE="{{ config_dir }}/{{ config_file }}"
CONFIG_FILE="{{ alloy_config_dir }}/{{ alloy_config_file }}"

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

{% for key, value in env_file_vars.items() %}
{% for key, value in alloy_env_file_vars.items() %}
{{key}}={{value}}
{% endfor %}
1 change: 1 addition & 0 deletions roles/loki/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
name: loki.service
state: restarted
enabled: true
when: not ansible_check_mode
5 changes: 4 additions & 1 deletion roles/loki/tasks/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
ansible.builtin.systemd:
name: loki.service
state: started
when: not ansible_check_mode

- name: Verify that Loki URL is responding
ansible.builtin.uri:
Expand All @@ -144,4 +145,6 @@
retries: 5
delay: 8
until: loki_verify_url_status_code.status == 200
when: loki_expose_port | bool
when:
- loki_expose_port | bool
- not ansible_check_mode
1 change: 1 addition & 0 deletions roles/promtail/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
name: promtail.service
state: restarted
enabled: true
when: not ansible_check_mode
7 changes: 6 additions & 1 deletion roles/promtail/tasks/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
become: false
delegate_to: localhost
run_once: true
check_mode: false
register: __github_latest_version

- name: Latest available Promtail version
Expand Down Expand Up @@ -89,6 +90,7 @@
when:
- promtail_scrape_configs | length > 0
- promtail_runtime_mode == "acl"
- not ansible_check_mode

- name: Get firewalld state
ansible.builtin.systemd:
Expand All @@ -112,6 +114,7 @@
ansible.builtin.systemd:
name: promtail.service
state: started
when: not ansible_check_mode

- name: Stat position file
ansible.builtin.stat:
Expand All @@ -136,4 +139,6 @@
retries: 5
delay: 8
until: promtail_verify_url_status_code.status == 200
when: promtail_expose_port | bool
when:
- promtail_expose_port | bool
- not ansible_check_mode

0 comments on commit 297274e

Please sign in to comment.