From 9bfc5a596b6b0bb8a241096294ac41eebf04e0e1 Mon Sep 17 00:00:00 2001 From: Kirby Date: Tue, 24 Feb 2026 10:35:08 +0000 Subject: [PATCH] ansible : add alloy role --- ansible/roles/alloy/defaults/main.yaml | 14 +++ ansible/roles/alloy/handlers/main.yaml | 10 +++ ansible/roles/alloy/tasks/main.yaml | 85 +++++++++++++++++++ ansible/roles/alloy/templates/config.j2 | 34 ++++++++ ansible/roles/alloy/templates/default.j2 | 8 ++ .../roles/alloy/templates/systemd_service.j2 | 19 +++++ 6 files changed, 170 insertions(+) create mode 100644 ansible/roles/alloy/defaults/main.yaml create mode 100644 ansible/roles/alloy/handlers/main.yaml create mode 100644 ansible/roles/alloy/tasks/main.yaml create mode 100644 ansible/roles/alloy/templates/config.j2 create mode 100644 ansible/roles/alloy/templates/default.j2 create mode 100644 ansible/roles/alloy/templates/systemd_service.j2 diff --git a/ansible/roles/alloy/defaults/main.yaml b/ansible/roles/alloy/defaults/main.yaml new file mode 100644 index 0000000..b516d03 --- /dev/null +++ b/ansible/roles/alloy/defaults/main.yaml @@ -0,0 +1,14 @@ +--- + +alloy_version: "1.13.2" +alloy_log_level: "info" +alloy_username: "alloy" +alloy_groupname: "alloy" +alloy_uid: "1001" +alloy_gid: "1001" +alloy_config_directory: "/etc/alloy" +alloy_config_file: "{{ alloy_config_directory }}/config.alloy" +alloy_working_directory: "/var/lib/alloy" +alloy_binary_dir: "/usr/local/bin" +alloy_run_args: "--stability.level=experimental --server.http.listen-addr='0.0.0.0:12345'" +alloy_restart_on_upgrade: true diff --git a/ansible/roles/alloy/handlers/main.yaml b/ansible/roles/alloy/handlers/main.yaml new file mode 100644 index 0000000..4af4864 --- /dev/null +++ b/ansible/roles/alloy/handlers/main.yaml @@ -0,0 +1,10 @@ +--- + +- name: Restart alloy + ansible.builtin.systemd_service: + name: "alloy.service" + state: restarted + +- name: Systemd reload + ansible.builtin.systemd_service: + daemon_reload: true diff --git a/ansible/roles/alloy/tasks/main.yaml b/ansible/roles/alloy/tasks/main.yaml new file mode 100644 index 0000000..92cc1e4 --- /dev/null +++ b/ansible/roles/alloy/tasks/main.yaml @@ -0,0 +1,85 @@ +--- + +- name: Download and install Alloy binary + ansible.builtin.unarchive: + src: "https://github.com/grafana/alloy/releases/download/v{{ alloy_version }}/alloy-linux-amd64.zip" + dest: "{{ alloy_binary_dir }}" + remote_src: true + tags: install + +- name: Rename binary + ansible.builtin.copy: + src: "{{ alloy_binary_dir }}/alloy-linux-amd64" + dest: "{{ alloy_binary_dir }}/alloy" + remote_src: true + mode: 0755 + tags: install + +- name: Create Alloy group + ansible.builtin.group: + name: "{{ alloy_groupname }}" + gid: "{{ alloy_gid }}" + tags: install + +- name: Create Alloy user + ansible.builtin.user: + name: "{{ alloy_username }}" + uid: "{{ alloy_uid }}" + group: "{{ alloy_groupname }}" + shell: "/bin/false" + tags: install + +- name: Ensure config directory exists + ansible.builtin.file: + path: "{{ alloy_config_directory }}" + owner: "{{ alloy_username }}" + group: "{{ alloy_groupname }}" + state: directory + mode: "0755" + tags: install,config + +- name: Ensure working directory exists + ansible.builtin.file: + path: "{{ alloy_working_directory }}" + state: directory + owner: "{{ alloy_username }}" + group: "{{ alloy_groupname }}" + mode: "0755" + tags: install + +- name: Deploy Alloy config + ansible.builtin.template: + src: "config.j2" + dest: "{{ alloy_config_directory }}/config.alloy" + owner: "{{ alloy_username }}" + group: "{{ alloy_groupname }}" + mode: "0644" + notify: Restart alloy + tags: install,config + +- name: Deploy Alloy default file + ansible.builtin.template: + src: "default.j2" + dest: "/etc/default/alloy" + owner: "root" + group: "root" + mode: 0644 + notify: Restart alloy + tags: install,config + +- name: Deploy Alloy systemd config + ansible.builtin.template: + src: "systemd_service.j2" + dest: "/etc/systemd/system/alloy.service" + owner: "root" + group: "root" + mode: 0644 + tags: install + +- name: Start and enable Alloy service + ansible.builtin.systemd_service: + name: "alloy.service" + state: started + enabled: true + daemon_reload: true + tags: install diff --git a/ansible/roles/alloy/templates/config.j2 b/ansible/roles/alloy/templates/config.j2 new file mode 100644 index 0000000..4654b98 --- /dev/null +++ b/ansible/roles/alloy/templates/config.j2 @@ -0,0 +1,34 @@ +otelcol.receiver.filelog "file" { + include = ["/in/*.log"] + operators = [{ + type = "regex_parser", + regex = `^(?P\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{2}:\d{2}) (?P.*)$`, + timestamp = { + parse_from = "attributes.timestamp", + layout = "%Y-%m-%dT%H:%M:%S%j", + }, + message = { + parse_from = "attributes.msg", + }, + },{ + type = "remove", + field = "attributes.timestamp", + }] + output { + logs = [otelcol.exporter.syslog.outsyslog.input] + } +} +otelcol.exporter.file "outfile" { + path = "/out/output.log" +} + +otelcol.exporter.syslog "outsyslog" { + endpoint = "slaithier-promax14" +} + +otelcol.exporter.debug "default" {} + +logging { + level = "{{ alloy_log_level }}" + format = "logfmt" +} diff --git a/ansible/roles/alloy/templates/default.j2 b/ansible/roles/alloy/templates/default.j2 new file mode 100644 index 0000000..b10ed34 --- /dev/null +++ b/ansible/roles/alloy/templates/default.j2 @@ -0,0 +1,8 @@ +# The configuration file holding the Grafana Alloy configuration. +CONFIG_FILE="{{ alloy_config_file }}" + +# User-defined arguments to pass to the run command. +CUSTOM_ARGS="{{ alloy_run_args }}" + +# Restart on system upgrade. Defaults to true. +RESTART_ON_UPGRADE={{ alloy_restart_on_upgrade }} diff --git a/ansible/roles/alloy/templates/systemd_service.j2 b/ansible/roles/alloy/templates/systemd_service.j2 new file mode 100644 index 0000000..2c3ef77 --- /dev/null +++ b/ansible/roles/alloy/templates/systemd_service.j2 @@ -0,0 +1,19 @@ +[Unit] +Description=Vendor-neutral programmable observability pipelines. +Documentation=https://grafana.com/docs/alloy/ +Wants=network-online.target +After=network-online.target + +[Service] +Restart=always +User={{ alloy_username }} +Group={{ alloy_groupname }} +Environment=HOSTNAME=%H +EnvironmentFile=/etc/default/alloy +WorkingDirectory={{ alloy_working_directory }} +ExecStart={{ alloy_binary_dir }}/alloy run $CUSTOM_ARGS --storage.path={{ alloy_working_directory }} $CONFIG_FILE +ExecReload=/usr/bin/env kill -HUP $MAINPID +TimeoutStopSec=20s + +[Install] +WantedBy=multi-user.target