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

**URL:** <https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188>\
**Category:** Ansible Project\
**Created:** [September 2, 2019, 10:18pm UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188 "2019-09-02T22:18:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawan\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@Pawan\_Kumar1](https://forum.ansible.com/u/Pawan_Kumar1)\
**Post date:** [September 2, 2019, 10:18pm UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/1 "2019-09-02T22:18:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 2, 2019, 10:35pm UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/2 "2019-09-02T22:35:51Z")

</div>

Hi

Kindly explain where ansible comes into play in this story?

---

<div class="post-metadata">

**Author:** ![Pawan\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@Pawan\_Kumar1](https://forum.ansible.com/u/Pawan_Kumar1)\
**Post date:** [September 2, 2019, 11:01pm UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/3 "2019-09-02T23:01:37Z")

</div>

Hi Dick,

Thanks for replying .

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

working with the following ansible playbook

---

<div class="post-metadata">

**Author:** ![Pawan\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@Pawan\_Kumar1](https://forum.ansible.com/u/Pawan_Kumar1)\
**Post date:** [September 2, 2019, 11:09pm UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/4 "2019-09-02T23:09:16Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [September 3, 2019, 5:38am UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/5 "2019-09-03T05:38:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pawan\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@Pawan\_Kumar1](https://forum.ansible.com/u/Pawan_Kumar1)\
**Post date:** [September 3, 2019, 3:06pm UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/6 "2019-09-03T15:06:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [September 4, 2019, 5:15am UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/7 "2019-09-04T05:15:45Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![Pawan\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@Pawan\_Kumar1](https://forum.ansible.com/u/Pawan_Kumar1)\
**Post date:** [September 4, 2019, 6:17am UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/8 "2019-09-04T06:17:49Z")

</div>

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**

---

<div class="post-metadata">

**Author:** ![Zolvaring](https://avatars.discourse-cdn.com/v4/letter/z/ce73a5/32.png) [@Zolvaring](https://forum.ansible.com/u/Zolvaring)\
**Post date:** [September 4, 2019, 2:30pm UTC](https://forum.ansible.com/t/requires-help-using-python-for-accessing-dictionary-values-from-yaml-file/31188/9 "2019-09-04T14:30:52Z")

</div>

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/issues/38415)  
[https://github.com/ansible/ansible/pull/31433](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:
