-1

I have the following:

- set_fact:
   test_string: "{{ htmlres.content | regex_search('test-([0-9]+)', '\\1') | first}}"

This retrieves 2 which is part of a string contained in the htmlres.content content which is test-2.

So now I'm trying to compare the result of that output and fail the execution if it is not 2, so I tried this:

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

However I've outputted the contents of test_string and I know for a fact that it is 2. Why is it failing?

I've tried adding | string and | int to the end of test_string since my first though is that it would be an issue with type comparison, but that didn't work either.

Thank you.

U880D
  • 1,017
  • 2
  • 12
  • 18
Ress
  • 45
  • 1
  • 2
  • 8

1 Answers1

4

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'
U880D
  • 1,017
  • 2
  • 12
  • 18
flowerysong
  • 901
  • 4
  • 6