add promtail role

This commit is contained in:
Ryan Cavicchioni 2022-08-30 07:50:35 -05:00
parent eae4e0120c
commit a948debbf8
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
10 changed files with 255 additions and 0 deletions

View File

@ -0,0 +1,51 @@
---
promtail_go_arch_map:
i386: '386'
x86_64: 'amd64'
promtail_go_arch: "{{ promtail_go_arch_map[ansible_architecture] | default('amd64') }}"
promtail_service_name: promtail.service
promtail_service_enabled: true
promtail_service_state: started
promtail_version_regex: ^promtail, version ([\d.]+)
promtail_github_project_url: https://github.com/grafana/loki
promtail_release_file: "promtail-{{ ansible_system | lower }}-{{ promtail_go_arch }}.zip"
promtail_release_url: "{{ promtail_github_project_url }}/releases/download/v{{ promtail_version }}/{{ promtail_release_file }}"
promtail_checksum_url: "{{ promtail_github_project_url }}/releases/download/v{{ promtail_version }}/SHA256SUMS"
promtail_download_path: "/tmp/{{ promtail_release_file }}"
promtail_unarchive_dest_path: /tmp
promtail_extracted_path: "{{ promtail_download_path | replace('.zip', '') }}"
promtail_user: promtail
promtail_user_state: present
promtail_user_shell: /usr/sbin/nologin
promtail_group: promtail
promtail_groups: adm
promtail_group_state: "{{ promtail_user_state | default('present') }}"
promtail_config_path: /etc/promtail.yaml
promtail_var_path: /var/lib/promtail
promtail_var_owner: "{{ promtail_user }}"
promtail_var_group: "{{ promtail_group }}"
promtail_var_mode: "0755"
promtail_bin_path: /usr/local/bin
promtail_auth_enabled: false
promtail_server:
http_listen_port: 9080
grpc_listen_port: 0
promtail_positions:
filename: "{{ promtail_var_path }}/positions.yaml"
promtail_clients:
- url: http://localhost:3100/loki/api/v1/push
# promtail_scrape_configs: []

View File

@ -0,0 +1,6 @@
---
- name: restart promtail
systemd:
name: promtail.service
daemon_reload: true
state: restarted

View File

@ -0,0 +1,49 @@
---
- name: create group
group:
name: "{{ promtail_group }}"
system: true
state: "{{ promtail_group_state | default('present') }}"
- name: create user
user:
name: "{{ promtail_user }}"
system: true
shell: "{{ promtail_user_shell }}"
group: "{{ promtail_group }}"
groups: "{{ promtail_groups }}"
createhome: false
home: "{{ promtail_var_path }}"
state: "{{ promtail_user_state | default('present') }}"
- name: configure
template:
src: promtail.yaml.j2
dest: "{{ promtail_config_path }}"
owner: root
group: root
mode: 0444
notify: restart promtail
- name: create var path
file:
path: "{{ promtail_var_path }}"
state: directory
owner: "{{ promtail_var_owner }}"
group: "{{ promtail_var_group }}"
mode: "{{ promtail_var_mode }}"
- name: configure systemd template
template:
src: "{{ promtail_service_name }}.j2"
dest: "/etc/systemd/system/{{ promtail_service_name }}"
owner: root
group: root
mode: 0444
notify: restart promtail
- name: manage service
service:
name: "{{ promtail_service_name }}"
enabled: "{{ promtail_service_enabled }}"
state: "{{ promtail_service_state }}"

View File

View File

@ -0,0 +1,29 @@
---
- block:
- name: download archive
get_url:
url: "{{ promtail_release_url }}"
dest: "{{ promtail_download_path }}"
checksum: "{{ promtail_checksum }}"
register: dl
until: dl is success
retries: 5
delay: 10
- name: extract archive
unarchive:
src: "{{ promtail_download_path }}"
dest: "{{ promtail_unarchive_dest_path }}"
creates: "{{ promtail_extracted_path }}/promtail"
remote_src: true
- name: install binaries
copy:
src: "{{ promtail_extracted_path }}"
dest: "{{ promtail_bin_path }}/promtail"
owner: root
group: root
mode: 0755
remote_src: true
notify: restart promtail
when: promtail_version != promtail_local_version

View File

@ -0,0 +1,30 @@
---
- name: gather os specific variables
include_vars: "{{ lookup('first_found', possible_files) }}"
vars:
possible_files:
files:
- "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml"
- "{{ ansible_distribution }}.yaml"
- "{{ ansible_os_family }}.yaml"
- "default.yaml"
paths:
- vars
- name: include os specific tasks
include_tasks: "{{ lookup('first_found', possible_files) }}"
vars:
possible_files:
files:
- "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yaml"
- "{{ ansible_distribution }}.yaml"
- "{{ ansible_os_family }}.yaml"
- "default.yaml"
paths:
- tasks
- include: pre.yaml
- include: install.yaml
- include: configure.yaml

View File

@ -0,0 +1,50 @@
---
- name: determine if installed
stat:
path: "{{ promtail_bin_path }}/promtail"
register: st
- name: set promtail_installed
set_fact:
promtail_installed: "{{ st.stat.exists | bool }}"
- block:
- name: determine latest version
uri:
url: https://api.github.com/repos/grafana/loki/releases/latest
return_content: true
body_format: json
register: _latest_version
until: _latest_version.status == 200
retries: 3
- name: set promtail_version
set_fact:
promtail_version: "{{ _latest_version.json['tag_name'] | regex_replace('^v', '') }}"
- block:
- name: determine installed version
command: "{{ promtail_bin_path }}/promtail --version"
register: _installed_version_string
changed_when: false
- name: set promtail_local_version
set_fact:
promtail_local_version: "{{ _installed_version_string.stdout | regex_search(promtail_version_regex, '\\1') | first }}"
when: promtail_installed
- name: set promtail_local_version to 0
set_fact:
promtail_local_version: "0"
when: not promtail_installed
- block:
- name: get checksums
set_fact:
_checksums: "{{ lookup('url', promtail_checksum_url, wantlist=True) }}"
- name: set promtail_checksum
set_fact:
promtail_checksum: "sha256:{{ item.split(' ') | first }}"
loop: "{{ _checksums }}"
when: "promtail_release_file in item"

View File

@ -0,0 +1,19 @@
{{ ansible_managed | comment }}
[Unit]
Description=Loki
After=network-online.target
[Service]
Type=simple
User={{ promtail_user }}
Group={{ promtail_group }}
ExecStart={{ promtail_bin_path }}/promtail \
-config.file {{ promtail_config_path }}
WorkingDirectory={{ promtail_var_path }}
Restart=always
RestartSec=1
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,21 @@
{{ ansible_managed | comment }}
---
{% if promtail_server is defined %}
server:
{{ promtail_server | to_nice_yaml(indent=2) | indent(2, False) }}
{% endif -%}
{% if promtail_positions is defined %}
positions:
{{ promtail_positions | to_nice_yaml(indent=2) | indent(2, False) }}
{% endif -%}
{% if promtail_clients is defined %}
clients:
{{ promtail_clients | to_nice_yaml(indent=2) | indent(2, False) }}
{% endif -%}
{% if promtail_scrape_configs is defined %}
scrape_configs:
{{ promtail_scrape_configs | to_nice_yaml(indent=2) | indent(2, False) }}
{% endif -%}

View File