问题
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