Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append data to Yaml section
#1
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
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 code

from 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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 220 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,232 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,286 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  import yaml error for python3 maiya 4 13,167 Jul-15-2020, 06:07 PM
Last Post: Gribouillis
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,610 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  PyTest >> Yaml parsed data to create api test request AmR 0 1,849 Apr-14-2020, 11:41 AM
Last Post: AmR
  Create an isolated section in bash? yxk 1 1,961 Feb-27-2020, 11:16 AM
Last Post: Larz60+
  Read Yaml configuration file in Python binhduonggttn 1 2,089 Feb-11-2020, 05:43 AM
Last Post: ndc85430
  Copy and append data from .dat file Makada 17 8,105 Jan-20-2020, 08:33 PM
Last Post: Makada
  txt-file: read and append missing data sufi 1 3,217 Dec-07-2019, 08:12 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020