Parsing a YAML file using Ansible

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

1 Like

Given the file data.yml

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

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

    msg: read folder1/file1
    msg: read folder2/file2
    msg: read folder2/file3
    msg: read folder3/file1
4 Likes

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