【问题标题】:How to avoid having to skip fatal task errors with ansible?如何避免使用 ansible 跳过致命任务错误?
【发布时间】:2016-12-30 20:02:40
【问题描述】:

在运行 ansible playbook 时,您经常会遇到任务(通常是 shell 或命令)预期会返回错误代码的用例。

到目前为止,解决方案是注册结果并添加 ignore_errors: true 并稍后确定它是否是真正的错误。

现在,这有一个问题:它会弄乱日志记录,因为您会看到类似的红色错误

fatal: ...
...ignoring

有没有办法避免这些,这样我们就不会在日志中出现虚假错误?

【问题讨论】:

标签: ansible


【解决方案1】:

failed_whenchanged_when是来帮你的:

- shell: echo good | grep bad
  register: res
  failed_when: false
  changed_when: false

尽管 shell 命令失败,但它始终是好的和绿色的。
您还可以根据注册变量定义复杂的failed_when 语句。

【讨论】:

    【解决方案2】:

    最好的方法是https://stackoverflow.com/a/40430875/1849837(使用changed_whenfailed_when)。请注意,它从 Ansible 1.4 开始就可以使用。

    对于较旧的 Ansible 版本,我看到(和使用)的唯一方法是根本不运行可能失败的任务。您可以为此使用条件 (when)。

    首先你需要检查条件并将检查结果分配给变量

    - name: "check if condition is met"
      command: some condition that populates result
      register: result
    
    - name: "some operation"
       command: operation that runs only if result is not failed.
      when: result|failed 
    

    这包括一些样板代码(您需要人工步骤来测试条件),但我认为没有更好的选择。我相信这比用fatal: ... ...ignoring 污染日志要好。

    【讨论】:

    • 这是一个可怕的黑客攻击,看看@Konstantin Suvorov 的回答。
    • 我同意你的回答更好。但是 failed_when 从 ansible 1.4 开始可用。我编辑了我和你的答案以指定这一点。
    猜你喜欢
    • 1970-01-01
    • 2017-10-04
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多