Ansible:在基于stdout值的异步任务上执行failed_when: [英] Ansible: Perform a failed_when: on an async task based on a value from stdout

查看:559
本文介绍了Ansible:在基于stdout值的异步任务上执行failed_when:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据stdout中的值对异步任务执行failed_when:.

I am trying to perform a failed_when: on an async task based on a value from 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

这是我检查任务状态的地方,当指定的文本在标准输出中时,它会失败.

This is where I check on the task status and would like it to fail when the specified text is in 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"

当我运行上述剧本时,我会遇到来自Ansible的以下错误

When I run the above playbook I am faced with the following error from 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"}

推荐答案

您可能会帮助ansant避免由于未定义变量而导致模板崩溃.
像这样更改fail_when::

You may help ansible to avoid templating crash because of undefined variable.
Change fail_when: like this:

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

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

If job is not finished by the first run of polling task, stdout is not yet populated and thus undefined, causing templating engine to crash.

这篇关于Ansible:在基于stdout值的异步任务上执行failed_when:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆