Ansible 任务失败,即使 ignore_errors

Ansible task fails, even with ignore_errors

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

这是任务:

- 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 应该忽略错误,对吗?

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

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

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