Feb-09-2022, 06:56 PM
Hello ,
I am trying to append in loop sub sections into a Yaml ,using Pyyaml ,the yaml is below , didn't have a success until now
The section i want to append is right after
The code i am trying as follows below
Please advice
Thanks
the section to append to Yaml file
I am trying to append in loop sub sections into a Yaml ,using Pyyaml ,the yaml is below , didn't have a success until now
The section i want to append is right after
Quote:scrape_configs:
The code i am trying as follows below
Please advice
Thanks
the section to append to Yaml file
- job_name: system
pipeline_stages:
- output:
source: message
action_on_failure: skip
static_configs:
- targets:
- localhost
labels:
job: LogsIssue
agent: promtail
__path__: C:\\Scripts\\LokiGrafana\\promtail\\log\\**------the yaml file#This is promtail-local-config.yaml
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: C:/Scripts/LokiGrafana/promtail/positions.yaml
clients:
- url: http://192.168.12.5:3100/loki/api/v1/push
scrape_configs:
- job_name: system
pipeline_stages:
- output:
source: message
action_on_failure: skip
static_configs:
- targets:
- localhost
labels:
job: LogsIssue
agent: promtail
__path__: C:\\Scripts\\LokiGrafana\\promtail\\log\\**the python codefrom glob import glob
import os
import yaml
cwd = os.getcwd()
def yaml_update(job_name, host_name, path_name,yaml_name):
prom_dict = dict_file ={'job_name': 'system', 'static_configs': [{'targets': ['localhost'], 'labels': {'job': 'varlogs', '__path__': '/var/log/*log', 'host': 'grafana'}}]}
prom_dict['jobname'] = job_name
prom_dict['static_configs'][0]['labels']['host'] = host_name
prom_dict['static_configs'][0]['labels']['__path__'] = path_name
with open(yaml_name,'r') as yamlfile:
cur_yaml = yaml.safe_load(yamlfile) # Note the safe_load
cur_yaml['scrape_configs'].update(prom_dict)
if cur_yaml:
with open(yaml_name,'w') as yamlfile:
yaml.safe_dump(cur_yaml, yamlfile) # Also note the safe_dump
print(prom_dict)
def listdirs(rootdir):
yaml_name = cwd + '\promtail\promtail-local-config.yaml'
for file in os.listdir(rootdir):
d = os.path.join(rootdir, file)
if os.path.isdir(d):
#path mame
path_name = d
#hostname extract
pos = d.rfind("@")
host_name= d[pos+1:]
print(host_name)
#job name extract
pos = d.rfind("\\")
job_name= d[pos+1:]
print(job_name)
#append yaml
yaml_update(job_name, host_name, path_name,yaml_name)
#print(d)
listdirs(d)
listdirs(cwd)
