使用Ansible循环访问已注册的文件模块数据

使用Ansible循环访问已注册的文件模块数据,ansible,Ansible,我已经尝试在以及Stackoverflow和其他网站和博客上找到解决方案。 我还使用了已注册数据结构的调试输出,并确认在静态引用数据时可以访问该数据。最后,我使用嵌套循环来尝试解决这个问题 相关的Ansible重头戏: - name: Return the list of application folders win_find: paths: - C:\Applications\ patterns: [ 'app*' ] file_type: directo

我已经尝试在以及Stackoverflow和其他网站和博客上找到解决方案。 我还使用了已注册数据结构的调试输出,并确认在静态引用数据时可以访问该数据。最后,我使用嵌套循环来尝试解决这个问题

相关的Ansible重头戏:

- name: Return the list of application folders
  win_find:
    paths:
    - C:\Applications\
    patterns: [ 'app*' ]
    file_type: directory
  register: app_folders

- name: Return the list of rotated log files to purge
  win_find:
    paths: 
    - "{{ item.path }}\\logs"
    patterns: applog-\d{4}-\d{2}-\d{2}\.\d{2}
    use_regex: yes
  loop: "{{ app_folders.files }}"
  register: logs_to_purge

- name: Purge rotated log files
  win_file:
    path: "{{ item.path }}"
    state: absent
  #loop: "{{ logs_to_purge.results }}"
  #with_nested: 
  #- "{{ logs_to_purge.results }}"
  #- ['files']
结果是dict项目列表,一些项目是正常dict名称-值对,一些项目值是dict本身,一些项目值是其他正常dict项目名称-值对的列表

我尝试循环使用的数据可以表示为:

"logs_to_purge": {
    results:[
                { 
                    "name": value,
                    "name2": value2,
                    "name3": {    }
                    "files": [{
                                  "filevalue1": value,
                                  "filevalue2": value,
                                  "path": value_of_interest 
                              },
                              {
                                  "filevalue1": value,
                                  "filevalue2": value,
                                  "path": value_of_interest
                              }
                            ]

                  },
                  {
                      One more like above for every "app_folders"
                      with zero or more files matches.
                  }
            ]
因此,结果是一个目录列表。每个结果dict包含一个dict项文件,该文件具有dict列表作为值。每个dict都包含一个路径项,它是感兴趣的值


我在上面的清除循环日志文件Ansible Play中注释掉的最后四行是我花了很多时间都没有用的地方。

使用json\u查询。下面的任务

- debug:
    var: item
  loop: "{{ logs_to_purge.results|json_query('[*].files[*].path')|flatten }}"
给予


您应该检查JSON并修复它。
"item": "value_of_interest"
"item": "value_of_interest"