【问题标题】:Ansible error handling for status code using failed_when使用 failed_when 对状态代码进行 Ansible 错误处理
【发布时间】:2021-11-15 17:49:26
【问题描述】:

我正在尝试在 ansible 中处理 http 状态代码的错误。我的用例是检查状态代码 400(失败代码),它需要通过 2 个已知的错误条件。 就像内容在 400 响应中有特定的搜索字符串一样。 示例任务:

-name: create repo
uri:
    url:
    method: POST
    body_format:json
    .
    .
    status_code: 
        - 201 
        - 400
    register: value
    changed_when: value.status == 201
    failed_when: value.status == 400 and
                 value.content|default() is not search ("found duplicate value") 

上面的工作,但 期望另一个搜索字符串与或条件相同且 400 个类似。

-name: create repo
 uri:
    url:
    method: POST
    body_format:json
    .
    .
    status_code: 
        - 201 
        - 400
    register: value
    changed_when: value.status == 201
    failed_when: value.status == 400 and
                 ( value.content|default() is not search ("found 1st duplicate value") or value.content|default() is not search ("found 2nd duplicate value") )

如何处理条件 2 or's with the 'and'..

【问题讨论】:

    标签: error-handling ansible


    【解决方案1】:

    我认为你有括号问题,你必须添加简单的引号: 试试:

    failed_when: (value.status == 400) and
                 '(value.content|default() is not search ("found 1st duplicate value") or value.content|default() is not search ("found 2nd duplicate value"))'
    

    【讨论】:

    • 谢谢Frenchy ..像你说的那样尝试了括号,但它没有帮助我..同时,如果我尝试使用单个搜索字符串条件,它就可以工作
    • 我添加了简单引号,似乎是搜索或不是搜索使用简单引号
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    相关资源
    最近更新 更多