如何检查列表中是否有Ansible Rundeck中的项目

时间:2019-09-27 14:35:36

标签: ansible rundeck

目标:

  • 在具有模式的目录中查找文件并将它们放在列表中
  • 然后检查列表是否为空。如果不为空,则提取文件名。

代码:

- name: "Find the text files"
    find:
      paths: /directory/
      patterns: '*.txt'
    register: file_in_directory

  - name: Get the file name and its path
    set_fact:
      file_name_with_path: "{{ file_in_directory.files[0].path }}"
    when: "{{ file_in_directory.files[0].path }}" != ""

问题: when条件失败。

when: "{{ file_in_directory.files[0].path }}" != ""

请提出任何建议

2 个答案:

答案 0 :(得分:0)

我尝试了这个,对我有用。

when: file_in_directory.matched > 0

答案 1 :(得分:0)

或者您可以尝试:

when: file_in_directory.files | length > 0