Merge pull request #39 from corvus-migratorius/add-promtail

Add promtail
This commit is contained in:
Fogucoco
2025-12-17 18:23:33 +03:00
committed by GitHub
13 changed files with 274 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
- [mount_device](roles/mount_device/README.md)
- [nginx](roles/nginx/README.md)
- [prometheus](roles/prometheus/README.md)
- [promtail](roles/promtail/README.md)
- [rclone_yandex](roles/rclone_yandex/README.md)
- [rustdesk](roles/rustdesk/README.md)
- [sftp_share](roles/sftp_share/README.md)

View File

@@ -1,7 +1,7 @@
---
namespace: genlab
name: common
version: 0.18.0
version: 0.19.0
readme: README.md
authors:
- Alexander Gorelyshev (corvus-migratorius@proton.me)

44
roles/promtail/README.md Normal file
View File

@@ -0,0 +1,44 @@
promtail
=========
Installs promtail as systemd service.
Requirements
------------
Role Variables
--------------
(all optional)
`promtail_version`: version to install
`promtail_loki_server`: set loki server
`promtail_loki_port`: set loki port
`custom_server_config`: path to custom config witch replace all config with your own
`custom_scrape_configs`: path to custom scrape configs
Dependencies
------------
No
Example Playbook
----------------
- hosts: servers
roles:
- role: promtail
promtail_version: 2.7.3
License
-------
MIT
Author Information
------------------
Alexander Gorelyshev and Danilkin Danila (MIPT)
Genlab LLC
corvus-migratorius@proton.me

View File

@@ -0,0 +1,7 @@
---
promtail_version: 2.7.3
promtail_positions_path: /home/promtail/positions.yaml
promtail_http_port: 9080
promtail_loki_server: localhost
promtail_loki_port: 3100
promtail_add_var_logs: true

View File

@@ -0,0 +1,11 @@
---
- name: "Restart the Promtail daemon"
ansible.builtin.systemd:
name: promtail
state: restarted
- name: "Reload the Promtail daemon configuration"
ansible.builtin.systemd:
daemon_reload: true

View File

@@ -0,0 +1,14 @@
galaxy_info:
role_name: promtail
namespace: genlab
author: Danilkin Danila
description: Installs Promtail
company: Genlab LLC
license: MIT
min_ansible_version: "2.1"
galaxy_tags: []
dependencies: []

View File

@@ -0,0 +1,5 @@
---
- name: Converge
hosts: all
roles:
- role: genlab.common.promtail

View File

@@ -0,0 +1,24 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu
image: geerlingguy/docker-ubuntu2204-ansible:latest
pre_build_image: true
command: ${MOLECULE_DOCKER_COMMAND:-""}
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
cgroupns_mode: host
privileged: true
provisioner:
name: ansible
playbooks:
converge: converge.yml
verifier:
name: ansible
lint: |
set -e
yamllint .
ansible-lint .

View File

@@ -0,0 +1,20 @@
---
- name: Verify
hosts: all
gather_facts: false
tasks:
# https://github.com/ansible/molecule/issues/3587#issuecomment-1158650179
- name: "Include default vars"
ansible.builtin.include_vars:
dir: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/"
extensions: ['yml']
# kics-scan ignore-block - kics doesn't like http but it's localhost so it's not important
- name: "Sanity check the Promtail daemon"
retries: 3
delay: 1
register: promtail_this
failed_when: "promtail_this.content != 'Ready'"
ansible.builtin.uri:
url: "http://localhost:{{ promtail_http_port }}/ready"
return_content: true

View File

@@ -0,0 +1,107 @@
---
- name: "Install packages"
ansible.builtin.apt:
name: [acl, unzip]
state: present
update_cache: true
cache_valid_time: 3600
- name: "Create the 'promtail' group"
ansible.builtin.group:
name: promtail
state: present
- name: "Create the 'promtail' user"
ansible.builtin.user:
name: promtail
groups:
- promtail
- systemd-journal
- adm
state: present
system: true
- name: "Set facl to allow promtail user to read '/var/log' contents"
ansible.posix.acl:
path: /var/log
entity: promtail
etype: user
permissions: rX
state: present
- name: "Make promtail user owner of '{{ promtail_positions_path }}'"
changed_when: false
ansible.builtin.file:
path: "{{ promtail_positions_path }}"
owner: promtail
group: promtail
state: touch
mode: "0750"
- name: "Install the requested Promtail version"
block:
- name: "Check Promtail version"
ansible.builtin.command: /usr/bin/promtail --version
register: promtail_version_check
changed_when: false
- name: "Assert version correctness"
notify: "Restart the Promtail daemon"
ansible.builtin.assert:
that: "promtail_version in promtail_version_check.stdout"
success_msg: "Expected Promtail version available ({{ promtail_version }})"
fail_msg: "Expected version '{{ promtail_version }}'; available is '{{ promtail_version_check.stdout }}'"
rescue:
- name: "Install Promtail if its not present"
ansible.builtin.unarchive:
src: "https://github.com/grafana/loki/releases/download/v\
{{ promtail_version }}/promtail-linux-amd64.zip"
dest: /usr/bin/
remote_src: true
- name: "Rename and set permissions for the Promtail binary"
ansible.builtin.copy:
src: "/usr/bin/promtail-linux-amd64"
dest: "/usr/bin/promtail"
owner: root
group: root
mode: "0755"
remote_src: true
- name: "Cleanup the downloaded file"
ansible.builtin.file:
path: "/usr/bin/promtail-linux-amd64"
state: absent
- name: "Template the systemd unit file"
notify: "Reload the Promtail daemon configuration"
ansible.builtin.template:
src: promtail.service.j2
dest: /etc/systemd/system/promtail.service
owner: root
group: root
mode: "0644"
- name: "Template Promtail config file"
notify: "Restart the Promtail daemon"
ansible.builtin.template:
src: templates/promtail.yml.j2
dest: /usr/local/bin/config-promtail.yml
owner: root
group: root
mode: "0644"
- name: "Enable and start Promtail daemon"
ansible.builtin.systemd:
name: promtail
state: started
enabled: true

View File

@@ -0,0 +1,11 @@
[Unit]
Description=Promtail service
After=network.target
[Service]
Type=simple
User=promtail
ExecStart=/usr/bin/promtail -config.file /usr/local/bin/config-promtail.yml
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,28 @@
{% if custom_server_config is defined %}
{{ custom_server_config }}
{% else %}
server:
http_listen_port: {{ promtail_http_port }}
grpc_listen_port: 0
{% endif %}
positions:
filename: {{ promtail_positions_path }}
clients:
- url: "http://{{ promtail_loki_server }}:{{ promtail_loki_port }}/loki/api/v1/push"
scrape_configs:
{% if promtail_add_var_logs %}
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
__path__: /var/log/*log
host: {{ ansible_hostname }}
{% endif %}
{% if custom_scrape_configs is defined %}
{{ custom_scrape_configs }}
{% endif %}

View File

@@ -0,0 +1 @@
---