1

I am trying to assign a result of a string compare to a boolean variable in ansible (2.5.4) but no matter how I do it its False even though it should be True:

OS_ENV: "test"
IS_TEST: '{{ "OS_ENV" == "test"}}'
#IS_TEST: '{{ "OS_ENV" == "test"}}'
#IS_TEST: '{{ "OS_ENV" == "test" | bool}}'
#IS_TEST: "{{ OS_ENV == 'test' | bool }}"
#IS_TEST: ("{{OS_ENV}}" == 'test')
#IS_TEST: true
test_boolean: "{{'TEST_IS_TRUE' if IS_TEST else 'TEST_IS_FALSE'}}"

Task to print result:

- name: "test_boolean is {{ test_boolean }} when IS_TEST is {{ IS_TEST }}"
  debug:
    msg: "Message is:  test_boolean is {{ test_boolean }} when IS_TEST is {{ IS_TEST }}"

Which in all of the above cases gives:

   "msg": "Message is:  test_boolean is TEST_IS_FALSE when IS_TEST is False"

which is wrong, what am I missing?

1 Answer 1

2

The following expressions will always produce false:

  • IS_TEST: '{{ "OS_ENV" == "test"}}'
    

    String OS_ENV does not match string test.

  • IS_TEST: '{{ "OS_ENV" == "test" | bool}}'
    

    String OS_ENV does not match false (i.e., string test cast to Boolean, as | has precedence over ==).

  • IS_TEST: "{{ OS_ENV == 'test' | bool }}"
    

    Variable OS_ENV value does not match false.

  • IS_TEST: ("{{OS_ENV}}" == 'test')
    

    String {{OS_ENV}} does not match string test.


The expression comparing a variable value to a string is:

IS_TEST: '{{ OS_ENV == "test" }}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.