# Parsing a YAML file using Ansible

**URL:** <https://forum.ansible.com/t/parsing-a-yaml-file-using-ansible/44535>\
**Category:** Get Help\
**Tags:** yaml\
**Created:** [September 23, 2025, 1:30pm UTC](https://forum.ansible.com/t/parsing-a-yaml-file-using-ansible/44535 "2025-09-23T13:30:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmirShalom](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@AmirShalom](https://forum.ansible.com/u/AmirShalom)\
**Post date:** [September 23, 2025, 1:30pm UTC](https://forum.ansible.com/t/parsing-a-yaml-file-using-ansible/44535/1 "2025-09-23T13:30:04Z")

</div>

## Hi Ansible forum, Using ansible, Im trying to read a yaml file structured like this:

version: 111  
inventory:  
folder1:  
- file1: True  
- file2: False  
folder2:  
- file1: False  
- file2: True  
- file3: True  
folder3:  
- file1: True  
- file2: False  
…

I would like ansible to read the content of the below files (all the “enabled” files):  
folder1/file1  
folder2/file2  
folder2/file3  
folder3/file1

I couldn’t figure out the logic how to get it done. I’ll appreciate any help.  
Thank you

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 23, 2025, 2:43pm UTC](https://forum.ansible.com/t/parsing-a-yaml-file-using-ansible/44535/2 "2025-09-23T14:43:07Z")

</div>

Given the file _data.yml_

```yaml
version: 111
inventory:
  folder1: [{file1: true}, {file2: false}]
  folder2: [{file1: false}, {file2: true}, {file3: true}]
  folder3: [{file1: true}, {file2: false}]

```

Include the variables, convert the dictionary _inventory_ to a list, and iterate over the subelements

```yaml
    - include_vars:
        file: data.yml
        name: data

    - debug:
        msg: "read {{ item.0.key }}/{{ item.1.keys() | first }}"
      loop: "{{ data.inventory | dict2items | subelements('value') }}"
      when: item.1.values() | first

```

gives(abridged)

```yaml
    msg: read folder1/file1
    msg: read folder2/file2
    msg: read folder2/file3
    msg: read folder3/file1

```

---

<div class="post-metadata">

**Author:** ![AmirShalom](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@AmirShalom](https://forum.ansible.com/u/AmirShalom)\
**Post date:** [September 23, 2025, 3:36pm UTC](https://forum.ansible.com/t/parsing-a-yaml-file-using-ansible/44535/3 "2025-09-23T15:36:55Z")

</div>

This is working. I appreciate your help, thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 23, 2025, 3:37pm UTC](https://forum.ansible.com/t/parsing-a-yaml-file-using-ansible/44535/4 "2025-10-23T15:37:08Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
