Ansible conditional statements not evaluating correctly

非 Y 不嫁゛ 提交于 2021-02-08 07:40:27

问题


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.

Here is the code:

  - 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

Heres the output:

  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}

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

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.


回答1:


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



来源:https://stackoverflow.com/questions/42700186/ansible-conditional-statements-not-evaluating-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!