Simple Ansible comparison failing when comparing integers

Solution 1:

Unless jinja2_native is enabled, the result of templating is always a string. You need to take that into account in your comparison.

- name: Fail if test_string is not 2
  fail: msg="Incorrect string. Expected 2, but instead got {{ test_string }}"
  when: test_string | int != 2

or

- name: Fail if test_string is not 2
  fail: msg="Incorrect string. Expected 2, but instead got {{ test_string }}"
  when: test_string != '2'