Skip to content

Commit f9a9d32

Browse files
djooberleeCopilot
andauthored
update promtail formula (#725)
* update promtail formula * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0f3339b commit f9a9d32

2 files changed

Lines changed: 115 additions & 15 deletions

File tree

promtail/init.sls

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,55 @@ promtail_data_dirs:
2828
- group: 0
2929
- makedirs: True
3030
31-
{% if pillar["promtail"]["scrape_configs"] is defined %}
31+
{# ================================================================
32+
NEW PATH: config_global_part + config_scrape_parts (dict)
33+
- config_global_part: string — top of promtail.yaml (server, clients, limits…)
34+
- config_scrape_parts: dict — each key is a unique name, value is a YAML
35+
string with one or more `- job_name: …` blocks.
36+
Salt deep-merges dicts, so multiple pillar SLS files each add their
37+
own key and they all end up here.
38+
If config_scrape_parts is empty or absent, a warning is issued and Promtail will not be deployed.
39+
================================================================ #}
40+
{% set config_deployed = { 'is_ready': false } %}
41+
{% if "config_global_part" in pillar["promtail"] %}
42+
{% set scrape_fragments = [] %}
43+
{% if "config_scrape_parts" in pillar["promtail"] %}
44+
{% for part_name in pillar["promtail"]["config_scrape_parts"].keys() | sort %}
45+
{% set _ = scrape_fragments.append(pillar["promtail"]["config_scrape_parts"][part_name]) %}
46+
{% endfor %}
47+
{% endif %}
48+
{% if scrape_fragments | length == 0 %}
49+
50+
promtail_no_scrape_jobs:
51+
test.configurable_test_state:
52+
- name: no_scrape_jobs_attached
53+
- changes: False
54+
- result: True
55+
- warnings: |
56+
promtail: config_global_part is set but no config_scrape_parts jobs are attached.
57+
Promtail config will NOT be deployed until at least one job pillar is connected via top_sls.
58+
59+
{% else %}
60+
{% set merged_scrape_text = scrape_fragments | join('\n') %}
61+
{% set _ = config_deployed.update({'is_ready': true}) %}
62+
63+
promtail_config:
64+
file.managed:
65+
- name: /opt/promtail/etc/promtail.yaml
66+
- mode: 644
67+
- user: 0
68+
- group: 0
69+
- contents: |
70+
{{ pillar['promtail']['config_global_part'] | indent(8) }}
71+
scrape_configs:
72+
{{ merged_scrape_text | indent(8) }}
73+
{% endif %}
74+
75+
{# ================================================================
76+
LEGACY PATH 1: promtail.scrape_configs (uses config.jinja template)
77+
================================================================ #}
78+
{% elif pillar["promtail"]["scrape_configs"] is defined %}
79+
{% set _ = config_deployed.update({'is_ready': true}) %}
3280
promtail_config:
3381
file.managed:
3482
- name: /opt/promtail/etc/promtail.yaml
@@ -44,7 +92,12 @@ promtail_config:
4492
- labels:
4593
job: varlogs
4694
__path__: /var/log/*log
95+
96+
{# ================================================================
97+
LEGACY PATH 2: promtail.config (full config verbatim)
98+
================================================================ #}
4799
{% elif pillar["promtail"]["config"] is defined %}
100+
{% set _ = config_deployed.update({'is_ready': true}) %}
48101
{% if pillar["promtail"]["loki"] is defined or pillar["promtail"]["positions"] is defined %}
49102
when pillar contain "pillar.config" block:
50103
test.configurable_test_state:
@@ -54,18 +107,6 @@ when pillar contain "pillar.config" block:
54107
- warnings: |
55108
When pillar contain "promtail.config" block, the "promtail.positions", "promtail.loki" blocks are IGNORED
56109
{% endif %}
57-
{#
58-
promtail_config:
59-
file.serialize:
60-
- name: /opt/promtail/etc/promtail.yaml
61-
- user: 0
62-
- group: 0
63-
- mode: 644
64-
- serializer: yaml
65-
- dataset_pillar: promtail:config
66-
- serializer_opts:
67-
- sort_keys: False
68-
#}
69110
promtail_config:
70111
file.managed:
71112
- name: /opt/promtail/etc/promtail.yaml
@@ -76,7 +117,7 @@ promtail_config:
76117
{{ pillar['promtail']['config'] | indent(8) }}
77118
{% endif %}
78119
79-
{% if 'docker' in pillar['promtail'] %}
120+
{% if config_deployed['is_ready'] and 'docker' in pillar['promtail'] %}
80121
promtail_image:
81122
cmd.run:
82123
- name: docker pull {{ pillar['promtail']['docker']['image'] }}
@@ -98,7 +139,7 @@ promtail_container:
98139
- watch:
99140
- /opt/promtail/etc/promtail.yaml
100141
- command: -config.file=/etc/promtail/promtail.yaml
101-
{% else %}
142+
{% elif config_deployed['is_ready'] %}
102143
103144
promtail_binary_1:
104145
archive.extracted:
@@ -153,3 +194,4 @@ promtail_systemd_4:
153194
{% endif %}
154195
{% endif %}
155196
{% endif %}
197+

promtail/pillar.example

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,60 @@
1+
### NEW: config_global_part + config_scrape_parts (recommended for multi-pillar merge)
2+
###
3+
### Split promtail config into two pieces that live in SEPARATE pillar SLS files:
4+
### config_global_part — string: everything above scrape_configs (server, clients, limits…)
5+
### config_scrape_parts — dict: each key is a unique name, value is a YAML string
6+
### containing one or more `- job_name: …` blocks
7+
###
8+
### Salt deep-merges dicts, so when multiple pillar SLS files each add their own
9+
### key to config_scrape_parts, all keys end up in the compiled pillar.
10+
### The formula iterates over all values (sorted by key) and assembles them under
11+
### `scrape_configs:`.
12+
###
13+
### If config_scrape_parts is empty or absent, a default stub job is used (no errors).
14+
###
15+
### Targeting is done ONLY via top_sls — job pillar SLS files contain no targets.
16+
###
17+
### See pillar/promtail/examples/ for complete working examples.
18+
19+
# --- Global pillar (attach to '*' in top_sls) ---
20+
# promtail:
21+
# binary:
22+
# link: https://github.com/grafana/loki/releases/download/v2.7.3/promtail-linux-amd64.zip
23+
# config_global_part: |
24+
# server:
25+
# http_listen_port: 9080
26+
# grpc_listen_port: 0
27+
# positions:
28+
# filename: /tmp/positions.yaml
29+
# clients:
30+
# - url: https://loki.example.com/loki/api/v1/push
31+
# limits_config:
32+
# readline_rate_enabled: true
33+
# readline_rate_drop: false
34+
35+
# --- Job pillar (attach to matching servers in top_sls) ---
36+
# promtail:
37+
# config_scrape_parts:
38+
# nginx_access: |
39+
# - job_name: nginx_access_logs
40+
# static_configs:
41+
# - labels:
42+
# job: nginx
43+
# __path__: /var/log/nginx/*access.log
44+
45+
# --- Another job pillar (same server can get both) ---
46+
# promtail:
47+
# config_scrape_parts:
48+
# mysql_slowlog: |
49+
# - job_name: mysql_slowlog
50+
# static_configs:
51+
# - labels:
52+
# job: mysql_slowlog
53+
# __path__: /var/log/mysql/*slow.log
54+
55+
56+
### FULL CONFIG (single pillar, no merge)
57+
158
{%- set promtail_version = "2.7.3" -%}
259
{%- set host = grains['fqdn'] -%}
360
{%- set loki_server = "loki.example.com" -%}
@@ -95,3 +152,4 @@ promtail:
95152
job: app-1
96153
host: {{ host }}
97154
__path__: /var/log/app-1/*log
155+

0 commit comments

Comments
 (0)