Ansible error on shell command returning zero


Ansible doesn’t seem to be able to handle the result ‘0’ for shell commands. This

Returns an error

“msg”: “non-zero return code”

But when I replace “dev” with “type”, which actually always occurs and gives a count of at least 1, then the command is successful and no error is thrown.

I also tried with command: instead of shell: – it doesn’t give an error, but then the command is also not executed.


since you want to run a sequence of commands that involve pipe, ansible states you should use shell and not command, as you are doing.

So, the problem is the fact that grep returns 1 (didnt find a match on the swapon output), and ansible considers this a failure. Since you are well sure there is no issue, just add a ignore_errors: true and be done with it.


if you want to narrow it down to return codes 0 and 1, instruct ansible to not consider failures those 2 rcs:

Leave a Reply