I am trying to use ansible to deploy configuration files out to hundreds of machines in which different machines will have multiple iterations of specific configuration snippets. Specifically I am using the promtail log parser and different machines will have different log file locations to parse with different labels. Ideally I want to keep the ansible configuration pretty simply so I can just use pull requests to make changes to the various sections.
Initially I was going to use group_vars and have each log file location being defined in the group_var. Which works fine as long as I am only building a single log location. Once I need multiple log locations, it breaks as I will only have one value returned from group_vars.
To illustrate.
hosts:
LOGFILE1:
hosts:
app[15:16].qa2.example.com
LOGFILE2:
hosts
app[16:17].qa2.example.com
GROUP_VARS/LOGFILE1
GROUP_VARS/LOGFILE2
I could just possibly look to iterate through each group and then append the the output to the config file but I don't see a way to do that with the template function. Ideally I could just iterate through all of the log file locations but I'm not sure how to do that.
Or maybe I could use an external variable file and then use a conditional of some sort to determine which hosts get which configuration?
Same data in the group_vars...
file: /opt/tomcat/fxcts/logs/gxxss.log
comp: TX_Tomcat
app: TX
module: GXX
pipeline_regex: None
pipeline_vars:
- None
drop_expression: None
Multiline: None
Here is the jinja template
scrape_configs:
- job_name: {{ module }}
pipeline_stages:
- regex:
expression: {{ pipeline_regex }}
- labels:
{% for labels in pipeline_vars -%}
{{ labels }}:
{% endfor %}
{# This is a test #}
- timestamp:
source: date
format: 2006-01-01 15:00:00.000000
- drop:
expression: {{ drop_expression }}
- multiline:
firstline: ""
max_wait_time: 3s
static_configs:
static_configs:
- targets:
- localhost
labels:
app: {{ app }}
host: {{ ansible_hostname }}
component: {{ comp }}
__path__: {{ file }}
Here is a sample of an actual yaml config. As I said the different log locations can vary by host.
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://host:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
host: ${HOSTNAME}
__path__: /var/log/*log
- job_name: apps_ssi
static_configs:
- targets:
- localhost
labels:
job: ssi
host: ${HOSTNAME}
__path__: /opt/tomcat/ssi/logs/*log
- job_name: apps_fxcts
static_configs:
- targets:
- localhost
labels:
job: fxcts
host: ${HOSTNAME}
__path__: /opt/tomcat/fxcts/logs/*log
- job_name: journal
journal:
json: false
max_age: 12h
labels:
job: systemd-journal
host: ${HOSTNAME}
relabel_configs:
- source_labels: ['__journal__systemd_unit']
target_label: 'unit'