Requires Help ,using Python for accessing dictionary values from yaml file

Using Python script to access values from yaml dictionary & print some of the values as list .

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

Hi

Kindly explain where ansible comes into play in this story?

Hi Dick,

Thanks for replying .

Well said , i was working with Ansible to parse values from dictionary file .

working with the following ansible playbook

My Ansible code works fine , if i call dictionary values from file .

But requires guidance , how using the yaml file to pass as argument as INV file command line to execute playbook:

something like :

“ansible-playbook test.yml -e INV_FILE=latest123.yaml”

I think what you want to do to use cli is specify the path as your var name just like your example, and then in your code when you invoke the variable, use the file lookup:

  • set_fact:
    your_fact: “{{ lookup(‘file’, INV_VAR) }}”

I’m not clear if you will need to do something beyond that for it to naturally interpret the yaml object though there’s a chance you’ll still end up with a string.

Thanks Zolvaring ,

Took input from the file & now output is coming in string format with following command —

Now , Requires formatting of the output to a list .

“ansible-playbook test.yml -e INV_FILE=latest123.yaml”

test.yml

Hm okay I'm not sure that would work, but gojng back to your original example, have you tried passing the yaml file like this when you run the playbook?

"--extra-vars=@your_yaml_file.yml"

Reading I think that should work the same as running it "with_vars"

Thanks @Zolvaring

I tried your previous suggestion , ansible-playbook ansible_dictionary.yml -e INV_FILE=dictionaryFile.yamlBut unable to print key values in dictonary format , For example - Each key is product. Parse the each key and extract the “product_name”, “release_candidate” and “version“

ansible_dictionary.yml

Hey Pawan,

I got the laptop out and tried:
ansible-playbook testplay.yml -i localhost, -e “@testvars.yml

and got:

ERROR! Invalid extra vars data supplied. ‘@testvars.yml’ could not be made into a dictionary

After digging, I found:
https://github.com/ansible/ansible/issues/38415
https://github.com/ansible/ansible/pull/31433

The above issues (Recent as of Dec 2018) point out that YAML was never accepted as CLI args, though at one point the documentation erroneously claimed it was.

What I did was this: