2016-08-21 364 views
4

如果此問題已被回答,請提前道歉。我確實進行了廣泛搜索,找不到符合我的標準的現有解決方案。Ansible:執行failed_when:根據標準輸出的值執行異步任務

我試圖根據標準輸出的值對異步任務執行failed_when:

這是我的任務:

- name: RUN SOME TASK LOCALLY 
    command: <run_some_script_here>.sh 
      chdir=/task_folder/ 
    delegate_to: localhost 
    register: task_status 
    async: 3600 
    poll: 0 

這是我檢查任務狀態,並希望它時,指定的文本是在標準輸出失敗。

- name: CHECK TASK PROGRESS 
    async_status: jid={{ task_status.ansible_job_id }} 
    register: poll_status 
    until: poll_status.finished 
    retries: 100 
    failed_when: "'ERROR. TASK FAILED.' in poll_status.stdout" 

當我跑我提前面臨以下錯誤從Ansible

TASK [CHECK TASK PROGRESS] ************************************************* 
fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check ''ERROR. TASK FAILED.' in poll_status.stdout' failed. The error was: error while evaluating conditional ('ERROR. TASK FAILED.' in poll_status.stdout): Unable to look up a name or access an attribute in template string ({% if 'ERROR. TASK FAILED.' in poll_status.stdout %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterable"} 

由於上述劇本。

回答

5

由於未定義變量,您可能有助於避免模板崩潰。
變化fail_when:這樣的:

failed_when: "poll_status.stdout is defined and 'ERROR' in poll_status.stdout" 

如果作業未通過輪詢任務的第一次運行完成後,stdout尚未填充,因此不確定,致使模板引擎崩潰。

+0

謝謝。這工作:) –