Python Forum

Full Version: Python Script for parsing dictionary values from yaml file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Requires Help ,using Python for accessing dictionary values from yaml file

Also , the yaml file will be passed as argument as INV file command line to execute playbook:

something like :

"ansible-playbook test.yml -e INV_FILE=latest123.yaml"


For Example - Parse the each key and extract the “product_name”, “release_candidate” and “version“ & Print results as list

The yaml file looks like



- releases:
- gotcha:
file_name: gtp.tar
md5: 11f6987275613ca3ecf832ea59c1
product_name: gotcha
product_number: GTP 8881
release_candidate: rc19
version: 19.0.0
- tp:
file_name: tpr.tar
md5: ebd7742ed8fff0472733c0cd8e
product_name: tp
product_number: TTP 8057
release_candidate: rc25
version: 25.0.0
- device_name:
file_name: dna.tar
md5: e464e69a07123de075778aa9b7
product_name: device_name
product_number: DNP 6746
release_candidate: rc25
version: 25.0.0
So what have you tried?
If you want someone to write it for you, post under jobs and include information about compensation.
What about PyYaml library?!
Tried the following with no luck


import os
import yaml

def yaml_loader(filepath):
    """Loads a yaml file """
    with open(filepath, "r") as file_descriptor:
        data = yaml.load(file_descriptor)
    return data


if __name__ == "__main__":
    filepath = "latest123.yaml"
    data = yaml_loader(filepath)
    print (data)

    items = data.get('releases')
    for item,name, item_value in items.iteritems():
        print (item_name, item_value)