将循环值传递给Ansible调试消息时出现问题

将循环值传递给Ansible调试消息时出现问题,ansible,Ansible,我试图在循环迭代的帮助下,使用ansible调试模块打印路径中的文件名。但由于使用了_序列循环,因此面临错误 PFB剧本 --- - name: Find Module Playbook hosts: all gather_facts: no tasks: - name: Find files in tmp path. find: paths: /tmp patterns: "*.

我试图在循环迭代的帮助下,使用ansible调试模块打印路径中的文件名。但由于使用了_序列循环,因此面临错误

PFB剧本

---

- name: Find Module Playbook
  hosts: all
  gather_facts: no

  tasks:
        - name: Find files in tmp path.
          find:
                paths: /tmp
                patterns: "*.txt"
                recurse: yes

          register: files_match

        - name: No of Files found.
          debug:
                 msg: "Number of files matched is {{ files_match.matched }}"
#                msg: [ 
#                     "Number of files matched is {{ files_match.matched }}",
#                     "Files matching the patterns are {{ files_match.files[ item ].path }}"
#                     ]

        - name: Files Found.
#          debug:
#                 msg: "Files matching the patterns are {{ files_match.files[ item ].path }}"

          command: echo "{{ item }}"


#          with_sequence: start=0 end={{ files_match.matched }}

#          with_sequence: start=0 end={{ files_match.matched|int }}

          with_items:
                - 0
                - 1
                - 2
                - 3
                - 4
                - 5
错误消息:

任务[找到文件。] **********************************************************************************************************************************致命:[13.250.101.163]:失败!=>{“msg”:“该任务包括 带有未定义变量的选项。错误为:“列表对象”没有 属性u'0'\n\n错误似乎已出现 “/home/ansible_admin/ansible_Playbooks/Find.yml”:第24行第11列, 但根据确切的语法,可能会\n出现在文件的其他位置 问题。\n\n出现问题的行是:\n\n\n-name: 找到文件。\n^here\n“}若要重试,请使用:--limit @/主页/ansible\u管理员/ansible\u剧本/Find.retry

重演


13.250.101.163:正常=2更改=0无法访问=0失败=1


实际上,playbook中使用的所有三个循环对“command:”的作用完全相同,其中只有with_items循环使用“debug:msg”。我尝试了很多可能的方法,但问题仍然存在。此处是否有任何更正?

整数转换不在正确的位置。应该是:


{{files\u match.files[item | int].path}

整数转换位置不正确。应该是:


{{files\u match.files[item|int].path}

还为上述问题找到了一个替代解决方案,实际上根本不使用循环。在没有任何循环的情况下尝试下面的调试消息

msg:“与模式匹配的文件是{{Files_match.Files | map(attribute='path')| list}”


但是要想找到一个完美的解决方案,请使用前面的建议。

还为上述问题找到了一个替代解决方案,实际上根本没有使用循环。在没有任何循环的情况下尝试下面的调试消息

msg:“与模式匹配的文件是{{Files_match.Files | map(attribute='path')| list}”


但是要想找到一个完美的解决方案,请使用前面的建议。

建议的解决方案非常有效。由此我可以理解,“with_sequence”中的“item”值可能不是整数,在使用之前仍然需要进行类型转换。那么,“with_sequence”循环的输出“item”类型是什么呢。建议我的理解是否需要修改。建议的解决方案非常有效。由此我可以理解,“with_sequence”中的“item”值可能不是整数,在使用之前仍然需要进行类型转换。那么,“with_sequence”循环的输出“item”类型是什么呢。建议我的理解是否需要更正。