Ansible 条件语句未正确评估

Ansible conditional statements not evaluating correctly

我有一个 ansible 剧本,其中包含几个任务,这些任务检查目录中是否有在今天创建的文件并将它们保存在 files 中。我正在对 files|length 进行比较,并根据长度是否为 0 打印出两条不同的消息。

代码如下:

  - name: Grabbing all of the files that were created today
    shell: find /home/user/empty_directory  -maxdepth 1  -daystart -ctime 0  -print
    register: files

  - debug: var=files.stdout

  - debug: msg="The directory isn't empty"
    when: files|length != 0

  - debug: msg="The directory is empty"
    when: files|length == 0

这是输出:

  TASK [debug]
 ok: [server] => {
 "changed": false, 
 "files.stdout": ""
 }

 TASK [debug] 
 ok: [server] => {
"changed": false, 
"msg": "The directory isn't empty"
 }

TASK [debug] 
skipping: [server] => {"changed": false, "skip_reason": "Conditional result was False", "skipped": true}

我是否犯了一个错误,导致条件判断不正确?因为基于输出 files 实际上是空的

我在没有 |length 的情况下尝试过,并与 files == ""files != "" 进行了比较,得到了相同的结果。

非常感谢任何建议。

您正在检查 files|length 而不是 files.stdout|length。请注意,files 是一个包含其他一些元素的字典,因此它绝对不是空的