Evaluating return code in ansible conditional


I’m working on automating a task which needs to append the latest version of software to a file. I don’t want it to do this multiple times for the same version.

It looks at the following example file:

the defaults main.yml would pass in something like


problem: the debug message is evaluating current_version.rc and showing me boolean values based on the grep commands output, but I can’t re-use this in the when conditional to determine if the task should be run.

Edit: the output:


When grep returns an exit code of 1 when it doesn’t match any lines. Ansible then interprets this (actually any status code other than 0 from a shell/command task) as an error and so promptly fails.

You can tell Ansible to ignore the response code from the shell/command task by using ignore_errors. Although with grep this will ignore actual errors (given by a return code of 2) so instead you might want to use failed_when like this:

Leave a Reply