首页 > 解决方案 > Failed_when module with OR

问题描述

I'm trying to handle errors in my playbook, precisely, I want to be able to ignore a specific error. The message that I want to "accept" is contains the phrase "already exists". In order to get that to work, I need an OR statement, as I also want to accept successful task execution.

[...]
register: command_result
failed_when: not ("already exists" in command_result.msg or "CALL" in command_result.statusmessage)

The problem is, the task is accepted when the error message consists "already exists", as I wished, however, "CALL" in statusmessage is not working - meaning the task fails even if the statusmessage is exactly that. There must be something wrong with my or statement. I've tried:

"'already exists' in command_result.msg or 'CALL' in command_result.statusmessage"

But that does not work as well.

What is important: when a task returns an error, command_result.statusmessage is empty - "command_result.statusmessage": "VARIABLE IS NOT DEFINED!: 'dict object' has no attribute 'statusmessage'". Could it be the reason for the following error?

"The conditional check '('already exists' not in command_result.msg) or ('CALL' in command_result.statusmessage)' failed. The error was: error while evaluating conditional (('already exists' not in command_result.msg) or ('CALL' in command_result.statusmessage)): Unable to look up a name or access an attribute in template string

I was hoping that it just parses the string "VARIABLE IS NOT DEFINED!". How can I resolve my issue then?

标签: ansible

解决方案


问:“command_result.statusmessage”:“变量未定义!:'dict object'没有属性'statusmessage'”

A:默认情况下,给属性添加一个空字符串statusmessage。例如

      failed_when: not (('already exists' in command_result.msg) or
                        ('CALL' in command_result.statusmessage|default('')))

推荐阅读