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

Prefix default variables #228

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 14 additions & 14 deletions roles/alloy/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
version: "1.0.0"
alloy_version: "1.1.1"

arch_mapping:

Check failure on line 3 in roles/alloy/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 alloy_ as a prefix. (vars: arch_mapping)
x86_64: amd64
aarch64: arm64
armv7l: armhf
i386: i386
ppc64le: ppc64le

arch: "{{ arch_mapping[ansible_architecture] | default('amd64') }}"
__alloy_arch: "{{ 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]
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
24 changes: 12 additions & 12 deletions roles/alloy/tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
- 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

- 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
4 changes: 2 additions & 2 deletions roles/alloy/tasks/ga-started.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
- name: Check system logs if Grafana Alloy is not started
when: not ansible_check_mode and _result.status != 200
block:
- name: Run journalctl

Check failure on line 18 in roles/alloy/tasks/ga-started.yml

View workflow job for this annotation

GitHub Actions / Perform Linting

command-instead-of-shell

Use shell only when shell functionality is required.
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
Expand All @@ -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."
16 changes: 8 additions & 8 deletions roles/alloy/tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"

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 }}
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

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 }}/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
4 changes: 2 additions & 2 deletions roles/alloy/tasks/service.yml
Original file line number Diff line number Diff line change
@@ -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
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 %}
Loading