Ansible:在基于stdout值的异步任务上执行failed_when :.

Oma*_*r E 6 ansible

如果之前已经回答过这个问题,请提前道歉.我确实进行了广泛的搜索,找不到符合我标准的现有解决方案.

我试图failed_when:基于来自stdout的值执行异步任务.

这是我的任务:

  - name: RUN SOME TASK LOCALLY
    command: <run_some_script_here>.sh
             chdir=/task_folder/
    delegate_to: localhost
    register: task_status
    async: 3600
    poll: 0
Run Code Online (Sandbox Code Playgroud)

这是我检查任务状态的地方,并且当指定的文本在stdout中时它希望它失败.

 - 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"
Run Code Online (Sandbox Code Playgroud)

当我运行上面的剧本时,我面临来自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"}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Kon*_*rov 8

由于未定义的变量,您可以帮助避免模板崩溃.
改变fail_when:如下:

failed_when: "poll_status.stdout is defined and 'ERROR' in poll_status.stdout"
Run Code Online (Sandbox Code Playgroud)

如果第一轮轮询任务未完成作业,stdout则尚未填充并因此未定义,从而导致模板引擎崩溃.