Nelson G.
Nelson G.

Reputation: 5441

How to iterate through two dimensions list with Ansible?

I have a variable like that :

"files": {
    "results": [
        {
            "files": [
                {
                    "path": "/etc/file1.xml", 
                }, 
                {
                    "path": "/etc/file2.xml", 
                }
            ]
        },
        {
            "files": [
                {
                    "path": "/etc/file2.xml", 
                }
            ]
        }, 
        {
            "files": []
        }
    }
}

How can iterate through all paths ?

Upvotes: 1

Views: 1219

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68339

You don't need to iterate two dimensions to get all paths.
Use map filter to reduce your original list.

To get plain list of paths from your example:

- debug: msg="{{ files.results | map(attribute='files') | sum(start=[]) | map(attribute='path') | list }}"

Upvotes: 1

Related Questions