From 184f3485cd66424dfcfab5022f57e037b18d41b7 Mon Sep 17 00:00:00 2001 From: Alexandru Constantin Cardas <33327716+cardasac@users.noreply.github.com> Date: Thu, 20 Jun 2024 18:30:12 +0200 Subject: [PATCH 1/4] Small fixes to format and fqdn --- roles/alloy/tasks/configure.yml | 6 +++--- roles/alloy/tasks/install.yml | 6 +++--- roles/alloy/tasks/main.yml | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/roles/alloy/tasks/configure.yml b/roles/alloy/tasks/configure.yml index 595f1839..50ac9b67 100644 --- a/roles/alloy/tasks/configure.yml +++ b/roles/alloy/tasks/configure.yml @@ -13,7 +13,7 @@ state: directory owner: "{{ service_user }}" group: "{{ service_group }}" - mode: '0755' + mode: "0755" become: true - name: Deploy alloy configuration file @@ -22,7 +22,7 @@ dest: "{{ config_dir }}/{{ config_file }}" owner: "{{ service_user }}" group: "{{ service_group }}" - mode: '0644' + mode: "0644" notify: Restart alloy become: true @@ -32,6 +32,6 @@ dest: "{{ config_dir }}/{{ environment_file }}" owner: "{{ service_user }}" group: "{{ service_group }}" - mode: '0644' + mode: "0644" notify: Restart alloy become: true diff --git a/roles/alloy/tasks/install.yml b/roles/alloy/tasks/install.yml index b78a9532..9107ac9e 100644 --- a/roles/alloy/tasks/install.yml +++ b/roles/alloy/tasks/install.yml @@ -13,16 +13,16 @@ - name: Create alloy user ansible.builtin.user: name: "{{ service_user }}" - groups: "{{ [ service_group ] + alloy_user_groups }}" + groups: "{{ [service_group] + alloy_user_groups }}" system: true - create_home: false # Appropriate for a system user, usually doesn't need a home directory + 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' + mode: "0755" become: true register: download_result diff --git a/roles/alloy/tasks/main.yml b/roles/alloy/tasks/main.yml index 49a6dd69..21c064ed 100644 --- a/roles/alloy/tasks/main.yml +++ b/roles/alloy/tasks/main.yml @@ -1,11 +1,11 @@ - name: Install alloy - include_tasks: install.yml + ansible.builtin.include_tasks: install.yml tags: [install] - name: Configure alloy - include_tasks: configure.yml + ansible.builtin.include_tasks: configure.yml tags: [configure] - name: Manage alloy service - include_tasks: service.yml + ansible.builtin.include_tasks: service.yml tags: [service] From 533ed0d0b87b9d18648964507febe2a7174e07a0 Mon Sep 17 00:00:00 2001 From: Alexandru Constantin Cardas <33327716+cardasac@users.noreply.github.com> Date: Thu, 20 Jun 2024 18:36:08 +0200 Subject: [PATCH 2/4] Add alloy prefix --- roles/alloy/defaults/main.yml | 30 +++++++++++++------------- roles/alloy/handlers/main.yml | 2 +- roles/alloy/tasks/configure.yml | 24 ++++++++++----------- roles/alloy/tasks/ga-started.yml | 4 ++-- roles/alloy/tasks/install.yml | 16 +++++++------- roles/alloy/tasks/service.yml | 4 ++-- roles/alloy/templates/alloy.service.j2 | 14 ++++++------ roles/alloy/templates/config.alloy.j2 | 2 +- roles/alloy/templates/environment.j2 | 4 ++-- 9 files changed, 50 insertions(+), 50 deletions(-) diff --git a/roles/alloy/defaults/main.yml b/roles/alloy/defaults/main.yml index fefc2555..b7c60eab 100644 --- a/roles/alloy/defaults/main.yml +++ b/roles/alloy/defaults/main.yml @@ -1,39 +1,39 @@ -version: "1.0.0" +alloy_version: "1.0.0" -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_installation_dir: "/usr/local/bin" -environment_file: "service.env" +alloy_environment_file: "service.env" -config_dir: "/etc/alloy" +alloy_config_dir: "/etc/alloy" -config_file: "config.alloy" +alloy_config_file: "config.alloy" -service_user: "alloy" +alloy_service_user: "alloy" -service_group: "alloy" +alloy_service_group: "alloy" -working_dir: "/var/lib/alloy" +alloy_working_dir: "/var/lib/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] diff --git a/roles/alloy/handlers/main.yml b/roles/alloy/handlers/main.yml index 2c30728c..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: "{{ 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 50ac9b67..db2f5e30 100644 --- a/roles/alloy/tasks/configure.yml +++ b/roles/alloy/tasks/configure.yml @@ -1,27 +1,27 @@ - name: Create alloy working directory ansible.builtin.file: - path: "{{ working_dir }}" + path: "{{ alloy_working_dir }}" state: directory - owner: "{{ service_user }}" - group: "{{ service_group }}" + owner: "{{ alloy_service_user }}" + group: "{{ alloy_service_group }}" mode: "0755" become: true - 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 @@ -29,9 +29,9 @@ - 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 diff --git a/roles/alloy/tasks/ga-started.yml b/roles/alloy/tasks/ga-started.yml index d535d816..cf744098 100644 --- a/roles/alloy/tasks/ga-started.yml +++ b/roles/alloy/tasks/ga-started.yml @@ -17,7 +17,7 @@ block: - name: Run journalctl ansible.builtin.shell: - cmd: "journalctl -u {{ service_name }} -b -n20 --no-pager" + cmd: "journalctl -u {{ alloy_service_name }} -b -n20 --no-pager" register: journal_ret changed_when: false - name: Output Grafana Alloy logs @@ -26,4 +26,4 @@ - name: Raise alerts ansible.builtin.assert: that: false - fail_msg: "Service {{ service_name }} hasn't started." + fail_msg: "Service {{ alloy_service_name }} hasn't started." diff --git a/roles/alloy/tasks/install.yml b/roles/alloy/tasks/install.yml index 9107ac9e..5a6bd2d1 100644 --- a/roles/alloy/tasks/install.yml +++ b/roles/alloy/tasks/install.yml @@ -6,37 +6,37 @@ - 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 }}" + groups: "{{ [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 - name: Remove existing alloy binary ansible.builtin.file: - path: "{{ installation_dir }}/alloy-linux-{{ arch }}" + path: "{{ alloy_installation_dir }}/alloy-linux-{{ alloy_arch }}" state: absent become: true when: download_result.changed - 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 diff --git a/roles/alloy/tasks/service.yml b/roles/alloy/tasks/service.yml index e4e87f12..ce63a0e6 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/{{ 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: "{{ 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 78626e3e..469111ec 100644 --- a/roles/alloy/templates/alloy.service.j2 +++ b/roles/alloy/templates/alloy.service.j2 @@ -2,16 +2,16 @@ 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 '' }} +After=network-online.target{{ ' ' + alloy_start_after_service if alloy_start_after_service is defined else '' }} [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 }} \ @@ -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 diff --git a/roles/alloy/templates/config.alloy.j2 b/roles/alloy/templates/config.alloy.j2 index 26278f36..8d63db6c 100644 --- a/roles/alloy/templates/config.alloy.j2 +++ b/roles/alloy/templates/config.alloy.j2 @@ -1 +1 @@ -{{ config }} +{{ alloy_config }} diff --git a/roles/alloy/templates/environment.j2 b/roles/alloy/templates/environment.j2 index 5d6de6a6..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="{{ 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 %} From cd0c596363d0e7e29fd9abbdb46ca2c0f28f9e1a Mon Sep 17 00:00:00 2001 From: Alexandru Constantin Cardas <33327716+cardasac@users.noreply.github.com> Date: Thu, 20 Jun 2024 19:20:36 +0200 Subject: [PATCH 3/4] Update README --- roles/alloy/README.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/roles/alloy/README.md b/roles/alloy/README.md index 39646ebf..c7118768 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 | |-----------------------|----------------------------------------------------------------------|---------------------------------------------------------------------| -| `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.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. | "/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 `alloy_version` and `alloy_arch` variables. | +| `alloy_service_name` | The name to be used for the Grafana Alloy service. | "alloy" | +| `alloy_installation_dir` | Directory where Grafana Alloy binary is to be installed. Must be present. | "/usr/local/bin" | +| `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. | "/var/lib/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_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 | | `alloy_user_groups`. | Configurable user groups that the Grafana Alloy can be put in so that it can access logs. | `[]` | @@ -47,7 +47,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] From 65b0cce7a9166d05a25e8d9fe62254f5bd919d15 Mon Sep 17 00:00:00 2001 From: Alexandru Constantin Cardas <33327716+cardasac@users.noreply.github.com> Date: Wed, 26 Jun 2024 17:29:42 +0200 Subject: [PATCH 4/4] Fix readme conflict --- roles/alloy/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/alloy/README.md b/roles/alloy/README.md index c7118768..80b269c6 100644 --- a/roles/alloy/README.md +++ b/roles/alloy/README.md @@ -22,7 +22,7 @@ Available variables with their default values are listed below (`defaults/main.y | `alloy_installation_dir` | Directory where Grafana Alloy binary is to be installed. Must be present. | "/usr/local/bin" | | `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_config_file` | Configuration file name for Grafana Alloy. | "config.alloy" | | `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. | "/var/lib/alloy" |