1
我有一對夫婦的檢查目錄上今天的日期創建的文件並保存它們files
任務的ansible劇本。我正在比較files|length
,並根據長度是否爲0打印出兩條不同的消息。沒有評估Ansible條件語句正確
下面是代碼:
- 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 != ""
,得到了相同的結果。
任何建議,非常感謝。
你所檢查的'文件| length',而不是'files.stdout | length'。請注意,'文件'還包含一些其他變量,所以它絕對不是空的 – SztupY
@SztupY - 感謝您的幫助!你的建議奏效了。如果你把它作爲答案發布,我會投它一票 –