Evaluating return code in ansible conditional

As nikobelia pointed out in the comments, 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:

- name: register version check
  shell: cat /root/versions.js | grep -q {{VERSION}}
  register: current_version
  failed_when: current_version.rc == 2

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)