Skip to content

Commit

Permalink
Ability to set custom directory path for *.alloy config files (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
voidquark authored Nov 20, 2024
1 parent cb10061 commit 09c6fee
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion roles/alloy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This role is tailored for operating systems such as **RedHat**, **Rocky Linux**,
| `alloy_download_url_deb` | The default download URL for the Alloy deb package from GitHub. | `"https://github.com/grafana/alloy/releases/download/v{{ aloy_version }}/alloy-{{ aloy_version }}-1.{{ __alloy_arch }}.deb"` |
| `alloy_env_file_vars` | You can use environment variables to control the run-time behavior of Grafana Alloy. | `{}` |
| `alloy_systemd_override` | Systemd unit drop-in file used to override or extend the default configuration of a systemd unit. | `{}` |
| `alloy_config` | This is the configuration that sets up Alloy. Refer to the [configuration blocks](https://grafana.com/docs/alloy/latest/reference/config-blocks/) and [components](https://grafana.com/docs/alloy/latest/reference/components/) documentation for more details. By default, there is no configuration provided, and it is required for successful deployment. Without it, the deployment will fail. Since the purpose of using Alloy varies, no default configuration is provided. ⚠️ **This configuration is mandatory.** | `{}` |
| `alloy_config` | This is the configuration that sets up Alloy. Refer to the [configuration blocks](https://grafana.com/docs/alloy/latest/reference/config-blocks/) and [components](https://grafana.com/docs/alloy/latest/reference/components/) documentation for more details. Since the purpose of using Alloy varies, no default configuration is provided. ⚠️ **You must provide either `alloy_config` for single config or set `alloy_env_file_vars.CONFIG_FILE` for multi-config setup**. Note that if you use `alloy_env_file_vars.CONFIG_FILE`, the content of `alloy_config` will not be templated. It is expected that you manage the multi-config content using pre_tasks or with your own role. | `{}` |

## Dependencies

Expand Down
1 change: 1 addition & 0 deletions roles/alloy/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ alloy_user_groups: []

alloy_env_file_vars: {}
# alloy_env_file_vars:
# CONFIG_FILE: "/custom/path"
# CUSTOM_ARGS: "--server.http.listen-addr=0.0.0.0:12345 --stability.level=public-preview --feature.community-components.enabled=true"

alloy_systemd_override: {}
Expand Down
10 changes: 10 additions & 0 deletions roles/alloy/tasks/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,18 @@
owner: "root"
group: "root"
mode: "0644"
when:
- alloy_config | length > 0
- alloy_env_file_vars.CONFIG_FILE is not defined
notify: restart alloy

- name: Ensure that /etc/alloy/alloy.config is absent when a custom configuration file/dir is specified in alloy_env_file_vars.CONFIG_FILE
ansible.builtin.file:
path: "/etc/alloy/config.alloy"
state: absent
when:
- alloy_config | length < 1 or alloy_env_file_vars.CONFIG_FILE is defined

- name: Add the Alloy system user to additional group
ansible.builtin.user:
name: "alloy"
Expand Down
9 changes: 6 additions & 3 deletions roles/alloy/tasks/preflight.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
---
- name: Fail when alloy_config is empty
- name: Fail when alloy_config or alloy_env_file_vars.CONFIG_FILE is not defined
ansible.builtin.fail:
msg: Variable alloy_config is required!
when: alloy_config | length < 1
msg: Variable alloy_config or alloy_env_file_vars.CONFIG_FILE is required!
when:
- alloy_config | length < 1
- alloy_env_file_vars.CONFIG_FILE is not defined
- not alloy_uninstall

- name: Extract IP address and PORT from alloy_env_file_vars
when: alloy_env_file_vars.CUSTOM_ARGS is defined and alloy_env_file_vars.CUSTOM_ARGS | length > 0
Expand Down
2 changes: 2 additions & 0 deletions roles/alloy/templates/alloy.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Ansible Managed

{% if alloy_env_file_vars.CONFIG_FILE is not defined or alloy_env_file_vars.CONFIG_FILE | length < 1 %}
CONFIG_FILE="/etc/alloy/config.alloy"
{% endif %}
RESTART_ON_UPGRADE=true

{% for key, value in alloy_env_file_vars.items() %}
Expand Down

0 comments on commit 09c6fee

Please sign in to comment.