Ansible条件语句无法正确评估 [英] Ansible conditional statements not evaluating correctly

查看:77
本文介绍了Ansible条件语句无法正确评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本有趣的剧本,其中包含一些任务,这些任务检查目录中是否有今天创建的文件,并将其保存在 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.

任何建议都将不胜感激.

Any suggestions are greatly appreciated.

推荐答案

您正在检查 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆