2016-08-21 366 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

谢谢。这工作:) –