ansiblehomebrewhomebrew-cask

Ansible task fails, even with ignore_errors


So I'm new to Ansible, but I've been researching, and I can't find any articles that would explain this issue. I've created a role to install brew cask applications, but it keeps 'failing' while running a command to check if the application is already installed.

Here's the task:

- 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

So, from what I understand, it will run the command with an item from my list of applications, so the example command would be brew cask list | grep keybase, and then map that to installed_applications.

But when I run it, I end up seeing all of the applications failing (there not installed). Here's the error in verbose mode:

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": []
}

If I run the command manually I just get a blank output (as it should), but Ansible should be ignoring errors, right?


Solution

  • Your shell tasks are marked as failed, because grep return non-zero exit code when no match found.
    So you get rc: 1 and failed: true.

    But saying ignore_errors: yes you tell Ansible: keep going even if task failed.
    So the task is red and marked as failed, but subsequent tasks get executed.

    If you want the task to be green even if grep failed, use bash tricks or use failed_when condition. You can search for [ansible] grep on SO. For example.