diff --git a/roles/alloy/README.md b/roles/alloy/README.md index 5953c010..88540241 100644 --- a/roles/alloy/README.md +++ b/roles/alloy/README.md @@ -14,22 +14,22 @@ Available variables with their default values are listed below (`defaults/main.y | Variable Name | Description | Default Value | |-----------------------|----------------------------------------------------------------------|---------------------------------------------------------------------| -| `grafana_alloy_version` | The version of Grafana Alloy to be installed. | "1.0.0" | -| `grafana_alloy_arch_mapping` | A mapping of common architecture names to Grafana Alloy binaries. | `{'x86_64': 'amd64', 'aarch64': 'arm64', 'armv7l': 'armhf', 'i386': 'i386', 'ppc64le': 'ppc64le'}` | -| `grafana_alloy_arch` | The architecture of the current machine. | Based on `ansible_architecture` lookup, defaults to 'amd64'. | -| `grafana_alloy_binary_url` | URL to Grafana Alloy binary for the specific version and architecture. | Constructed URL based on `version` and `arch` variables. | -| `grafana_alloy_service_name` | The name to be used for the Grafana Alloy service. | "alloy" | -| `grafana_alloy_installation_dir` | Directory where Grafana Alloy is to be installed. | "/etc/alloy" | -| `grafana_alloy_environment_file` | Name of the environment file for the Grafana Alloy service. | "service.env" | -| `grafana_alloy_config_dir` | Directory for Grafana Alloy configuration. | "/etc/alloy" | -| `grafana_alloy_config_file` | Configuration file name for Grafana Alloy. | "config.river" | -| `grafana_alloy_service_user` | User under which the Grafana Alloy service will run. | "alloy" | -| `grafana_alloy_service_group` | Group under which the Grafana Alloy service will run. | "alloy" | -| `grafana_alloy_working_dir` | Working directory for the Grafana Alloy service. | "/etc/alloy" | -| `grafana_alloy_env_file_vars` | Additional environment variables to be set in the service environment file. | {} (Empty dictionary) | -| `grafana_alloy_flags_extra` | Extra flags to pass to the Alloy service. | {} (Empty dictionary) | -| `grafana_alloy_start_after_service` | Specify an optional dependency service Alloy should start after. | '' (Empty string) | -| `grafana_alloy_config` | Configuration template for Grafana Alloy. | Configuration script with Prometheus scrape and remote_write setup | +| `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) | +| `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 @@ -45,7 +45,7 @@ Including an example of how to use your role: ansible.builtin.include_role: name: grafana.grafana.alloy vars: - grafana_alloy_config: | + alloy_config: | prometheus.scrape "default" { targets = [{"__address__" = "localhost:12345"}] forward_to = [prometheus.remote_write.prom.receiver] diff --git a/roles/alloy/defaults/main.yml b/roles/alloy/defaults/main.yml index bedeb4f8..ff58428b 100644 --- a/roles/alloy/defaults/main.yml +++ b/roles/alloy/defaults/main.yml @@ -1,39 +1,39 @@ -grafana_alloy_version: "1.0.0" +alloy_version: "1.0.0" -grafana_alloy_arch_mapping: +alloy_arch_mapping: x86_64: amd64 aarch64: arm64 armv7l: armhf i386: i386 ppc64le: ppc64le -grafana_alloy_arch: "{{ grafnana_alloy_arch_mapping[ansible_architecture] | default('amd64') }}" +alloy_arch: "{{ alloy_arch_mapping[ansible_architecture] | default('amd64') }}" -grafana_alloy_binary_url: "https://github.com/grafana/alloy/releases/download/v{{ grafana_alloy_version }}/alloy-linux-{{ grafana_alloy_arch }}.zip" +alloy_binary_url: "https://github.com/grafana/alloy/releases/download/v{{ alloy_version }}/alloy-linux-{{ alloy_arch }}.zip" -grafana_alloy_service_name: "alloy" +alloy_service_name: "alloy" -grafana_alloy_working_dir: "/opt/alloy" +alloy_working_dir: "/opt/alloy" -grafana_alloy_installation_dir: "{{ grafana_alloy_working_dir }}/bin" +alloy_installation_dir: "{{ alloy_working_dir }}/bin" -grafana_alloy_environment_file: "service.env" +alloy_environment_file: "service.env" -grafana_alloy_config_dir: "/etc/alloy" +alloy_config_dir: "/etc/alloy" -grafana_alloy_config_file: "config.alloy" +alloy_config_file: "config.alloy" -grafana_alloy_service_user: "alloy" +alloy_service_user: "alloy" -grafana_alloy_service_group: "alloy" +alloy_service_group: "alloy" -grafana_alloy_env_file_vars: {} +alloy_env_file_vars: {} -grafana_alloy_flags_extra: {} +alloy_flags_extra: {} -grafana_alloy_start_after_service: '' +alloy_start_after_service: '' -grafana_alloy_config: | +alloy_config: | prometheus.scrape "default" { targets = [{"__address__" = "localhost:12345"}] forward_to = [prometheus.remote_write.prom.receiver] diff --git a/roles/alloy/handlers/main.yml b/roles/alloy/handlers/main.yml index 0defe097..70c09f5b 100644 --- a/roles/alloy/handlers/main.yml +++ b/roles/alloy/handlers/main.yml @@ -1,6 +1,6 @@ - name: Restart alloy ansible.builtin.systemd: - name: "{{ grafana_alloy_service_name }}" + name: "{{ alloy_service_name }}" state: restarted become: true listen: "Restart alloy" diff --git a/roles/alloy/tasks/configure.yml b/roles/alloy/tasks/configure.yml index 4697da5c..2e2c8c9a 100644 --- a/roles/alloy/tasks/configure.yml +++ b/roles/alloy/tasks/configure.yml @@ -9,19 +9,19 @@ - name: Create alloy config directory ansible.builtin.file: - path: "{{ grafana_alloy_config_dir }}" + path: "{{ alloy_config_dir }}" state: directory - owner: "{{ grafana_alloy_service_user }}" - group: "{{ grafana_alloy_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: "{{ grafana_alloy_config_dir }}/{{ grafana_alloy_config_file }}" - owner: "{{ grafana_alloy_service_user }}" - group: "{{ grafana_alloy_service_group }}" + dest: "{{ alloy_config_dir }}/{{ alloy_config_file }}" + owner: "{{ alloy_service_user }}" + group: "{{ alloy_service_group }}" mode: '0644' notify: Restart alloy become: true @@ -29,9 +29,9 @@ - name: Deploy alloy environment file ansible.builtin.template: src: environment.j2 - dest: "{{ grafana_alloy_config_dir }}/{{ grafana_alloy_environment_file }}" - owner: "{{ grafana_alloy_service_user }}" - group: "{{ grafana_alloy_service_group }}" + dest: "{{ alloy_config_dir }}/{{ alloy_environment_file }}" + owner: "{{ alloy_service_user }}" + group: "{{ alloy_service_group }}" mode: '0644' notify: Restart alloy become: true diff --git a/roles/alloy/tasks/install.yml b/roles/alloy/tasks/install.yml index 5acebaef..d39c97b1 100644 --- a/roles/alloy/tasks/install.yml +++ b/roles/alloy/tasks/install.yml @@ -6,55 +6,55 @@ - name: Create alloy group ansible.builtin.group: - name: "{{ grafana_alloy_service_group }}" + name: "{{ alloy_service_group }}" system: true become: true - name: Create alloy user ansible.builtin.user: - name: "{{ grafana_alloy_service_user }}" - group: "{{ grafana_alloy_service_group }}" + name: "{{ alloy_service_user }}" + group: "{{ alloy_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: "{{ grafana_alloy_binary_url }}" - dest: "/tmp/alloy-{{ grafana_alloy_version }}.zip" + url: "{{ alloy_binary_url }}" + dest: "/tmp/alloy-{{ alloy_version }}.zip" mode: '0755' become: true - register: grafana_alloy_download_result + register: alloy_download_result - name: Remove existing alloy binary ansible.builtin.file: - path: "{{ grafana_alloy_installation_dir }}" + path: "{{ alloy_installation_dir }}" state: absent become: true - when: grafana_alloy_download_result.changed + when: alloy_download_result.changed - name: Create alloy working directory ansible.builtin.file: - path: "{{ grafana_alloy_working_dir }}" + path: "{{ alloy_working_dir }}" state: directory mode: '0755' - owner: "{{ grafana_alloy_service_user }}" - group: "{{ grafana_alloy_service_group }}" + owner: "{{ alloy_service_user }}" + group: "{{ alloy_service_group }}" become: true - name: Create alloy installation directory ansible.builtin.file: - path: "{{ grafana_alloy_installation_dir }}" + path: "{{ alloy_installation_dir }}" state: directory mode: '0755' - owner: "{{ grafana_alloy_service_user }}" - group: "{{ grafana_alloy_service_group }}" + owner: "{{ alloy_service_user }}" + group: "{{ alloy_service_group }}" become: true - name: Extract alloy binary ansible.builtin.unarchive: - src: "/tmp/alloy-{{ grafana_alloy_version }}.zip" - dest: "{{ grafana_alloy_installation_dir }}" + src: "/tmp/alloy-{{ alloy_version }}.zip" + dest: "{{ alloy_installation_dir }}" remote_src: yes become: true register: extract_result diff --git a/roles/alloy/tasks/service.yml b/roles/alloy/tasks/service.yml index e72af376..62804d58 100644 --- a/roles/alloy/tasks/service.yml +++ b/roles/alloy/tasks/service.yml @@ -1,7 +1,7 @@ - name: Copy alloy systemd unit file ansible.builtin.template: src: alloy.service.j2 - dest: /etc/systemd/system/{{ grafana_alloy_service_name }}.service + dest: /etc/systemd/system/{{ alloy_service_name }}.service mode: '0644' become: true notify: Restart alloy @@ -16,7 +16,7 @@ - name: Ensure alloy service is enabled and running ansible.builtin.service: - name: "{{ grafana_alloy_service_name }}" + name: "{{ alloy_service_name }}" enabled: yes state: started become: true diff --git a/roles/alloy/templates/alloy.service.j2 b/roles/alloy/templates/alloy.service.j2 index b1b9a3e7..7178b875 100644 --- a/roles/alloy/templates/alloy.service.j2 +++ b/roles/alloy/templates/alloy.service.j2 @@ -6,13 +6,13 @@ After=network-online.target{{ ' ' + start_after_service if start_after_service i [Service] Restart=always -User={{ grafana_alloy_service_user }} -Group={{ grafana_alloy_service_group }} +User={{ alloy_service_user }} +Group={{ alloy_service_group }} Environment=HOSTNAME=%H -EnvironmentFile={{ grafana_alloy_config_dir }}/{{ grafana_alloy_environment_file }} -WorkingDirectory={{ grafana_alloy_working_dir }} -ExecStart={{ grafana_alloy_installation_dir }}/alloy-linux-{{ grafana_alloy_arch }} run \ -{% for flag, flag_value in grafana_alloy_flags_extra.items() %} +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 }} \ {% elif flag_value is string %} @@ -23,7 +23,7 @@ ExecStart={{ grafana_alloy_installation_dir }}/alloy-linux-{{ grafana_alloy_arch {% endfor %} {% endif %} {% endfor %} - $CUSTOM_ARGS --storage.path={{ grafana_alloy_working_dir }} $CONFIG_FILE + $CUSTOM_ARGS --storage.path={{ alloy_working_dir }} $CONFIG_FILE ExecReload=/usr/bin/env kill -HUP $MAINPID TimeoutStopSec=20s SendSIGKILL=no diff --git a/roles/alloy/templates/config.alloy.j2 b/roles/alloy/templates/config.alloy.j2 index 5246361b..8d63db6c 100644 --- a/roles/alloy/templates/config.alloy.j2 +++ b/roles/alloy/templates/config.alloy.j2 @@ -1 +1 @@ -{{ grafana_alloy_config }} +{{ alloy_config }} diff --git a/roles/alloy/templates/environment.j2 b/roles/alloy/templates/environment.j2 index 1bffbe1e..46363cf2 100644 --- a/roles/alloy/templates/environment.j2 +++ b/roles/alloy/templates/environment.j2 @@ -1,10 +1,10 @@ {{ ansible_managed | comment }} # Grafana Alloy Environment File -CONFIG_FILE="{{ grafana_alloy_config_dir }}/{{ grafana_alloy_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 grafana_alloy_env_file_vars.items() %} +{% for key, value in alloy_env_file_vars.items() %} {{key}}={{value}} {% endfor %}