Ansible Loop Over Custom Facts

ansible

I have the following structure in custom facts .

{  
   "ansible_local":{  
      "finance":{  
         "facts":{  
            "files":{  
               "file1":{  
                  "dest":"/tmp/dir1",
                  "path":"/etc/finance/file1"
               },
               "file2":{  
                  "dest":"/tmp/dir2",
                  "path":"/etc/finance/file2"
               }
            }
         }
      },
      "marketing":{  
         "facts":{  
            "files":{  
               "file1":{  
                  "dest":"/tmp/dir1",
                  "path":"/etc/finance/file1"
               },
               "file2":{  
                  "dest":"/tmp/dir2",
                  "path":"/etc/finance/file2"
               }
            }
         }
      }
   }
}

As well as a task that iterates over the items :

 tasks:                                                                                     
    - debug:                                                                                 
        msg: "src: {{ item.value.path}} dest: {{ item.value.dest }}"                         
       with_dict: "{{ ansible_local.finance.facts.files }}"

My question is , given this structure , Iam always iterating over finance . What if I want to iterate over finance and Marketing and potentially more items in this level assuming I don't know their name, same as done in the items undes files.

Thanks a lot for the help.

Best Answer

You can use JMESPath query:

- debug:
    msg: "src: {{ item.path }} dest: {{ item.dest }}"
  loop: "{{ ansible_local | json_query('[*.facts.files.*]') | flatten }}"