Ansible 条件语句未正确评估 [英] Ansible conditional statements not evaluating correctly
问题描述
我有一个 ansible playbook,其中包含几个任务,用于检查今天创建的文件的目录并将它们保存在 files
中.我正在对 files|length
进行比较,并根据长度是否为 0 打印出两个不同的消息.
I have an ansible playbook with a couple of tasks that check a directory for files created on todays date and saves them in files
. I'm doing my comparison off of files|length
and print out two different messages depending if the length is 0 or not.
代码如下:
- 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
实际上是空的
Is there a mistake that I am making which is causing the conditionals to evaluate incorrectly? Because based off of the output files
is in fact empty
我已经在没有 |length
的情况下尝试过,并完成了对 files == ""
的比较 &files != ""
并得到相同的结果.
I've tried it without the |length
and done my comparison off of files == ""
& files != ""
and got the same result.
非常感谢任何建议.
推荐答案
您正在检查 files|length
而不是 files.stdout|length
.请注意,files
也是一个包含其他一些元素的字典,所以它绝对不是空的
You are checking files|length
instead of files.stdout|length
. Note that files
is a dictionary containing a few other elements as well, so it's definitely not empty
这篇关于Ansible 条件语句未正确评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!