【问题标题】:Ansible task fails, even with ignore_errorsAnsible 任务失败,即使有 ignore_errors
【发布时间】:2017-03-03 19:55:19
【问题描述】:

所以我是 Ansible 的新手,但我一直在研究,找不到任何可以解释这个问题的文章。我创建了一个角色来安装 brew cask 应用程序,但它在运行命令以检查应用程序是否已安装时一直“失败”。

这是任务:

- name: "Check for installed casks (Applications)"
  shell: brew cask list | grep {{ item }}
  register: installed_applications
  with_items: "{{ apps }}"
  when: apps is defined
  ignore_errors: yes

因此,据我了解,它将使用我的应用程序列表中的项目运行命令,因此示例命令将是 brew cask list | grep keybase,然后将其映射到 installed_applications。

但是当我运行它时,我最终看到所有应用程序都失败了(没有安装)。这是详细模式下的错误:

failed: [localhost] (item=keybase) => {
    "changed": true,
    "cmd": "brew cask list | grep keybase",
    "delta": "0:00:00.792680",
    "end": "2017-03-03 19:41:05.500378",
    "failed": true,
    "invocation": {
        "module_args": {
            "_raw_params": "brew cask list | grep keybase",
            "_uses_shell": true,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "warn": false
        },
        "module_name": "command"
    },
    "item": "keybase",
    "rc": 1,
    "start": "2017-03-03 19:41:04.707698",
    "stderr": "",
    "stdout": "",
    "stdout_lines": [],
    "warnings": []
}

如果我手动运行命令,我只会得到一个空白输出(应该如此),但 Ansible 应该忽略错误,对吧?

【问题讨论】:

    标签: ansible homebrew homebrew-cask


    【解决方案1】:

    您的shell 任务被标记为失败,因为grep 在未找到匹配项时返回非零退出代码。
    所以你会得到rc: 1 和failed: true。

    但是说 ignore_errors: yes 你告诉 Ansible:即使任务失败也要继续进行。
    所以任务是红色的并标记为失败,但后续任务会被执行。

    如果您希望即使 grep 失败也让任务变为绿色,请使用 bash 技巧或使用 failed_when 条件。您可以在 SO 上搜索[ansible] grep。 For example.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多