-2
elasticsearch-configuration:
  url: localhost
  port: 9200
  user: elastic
  password: password
correlation:
  correlations:
    name: my-custom-name
    period: 5m
    id:
      - 550
      - 551
      - 552
      - 887
    name: my-custom-name2
    period: 1m
    id:
      - 6550
      - 7551
      - 8552

How can I read the yaml config file from python, and run the python with each variable (name,period id)? For each correlation, I want to get the variables set in python.

if __name__ == "__main__":
    try:
      
      name = "my-custom-name"
      period = 5m
      id = ["550", "551", "552", "887"]  

        if alert_id == id:
stoksoz
  • 15
  • 3
  • Does this answer your question? [How to parse/read a YAML file into a Python object?](https://stackoverflow.com/questions/6866600/how-to-parse-read-a-yaml-file-into-a-python-object) – ShapeOfMatter Jul 13 '20 at 11:50

1 Answers1

0

The pyyaml package makes this very easy, and simply reads it as a dict:

import yaml

def read_config(path):
    with open(path, "r") as stream:
        return yaml.FullLoader(stream).get_data()

cfg = read_config("path/to/it.yaml")
url = cfg['elasticsearch-configuration']['url']
TayTay
  • 6,882
  • 4
  • 44
  • 65