Greetings,
In one of my playbooks, I prompt for a value that, if specified as a string, will later be used in another play. However, I can't seem to find a consistent way to evaluate the truthiness/falsiness of the var. Here's a sample playbook:
---
- name: Test playbook.
hosts: localhost
gather_facts: False
vars_prompt:
- name: "test_var"
prompt: "set a test var string"
private: no
default: ""
tasks:
# prints when test_var == ""
# doesn't print when test_var == "a string"
- debug: msg="I work as expected"
when: not test_var
- debug: msg="I will never run"
when: test_var | bool
- debug: msg="I will never run either"
when: "{{ test_var | bool }}"
# I will fail if you don't enter a string
- debug: msg="test_var is Truthy (no bool)"
when: test_var
I think I understand the last task is failing because I'm not explicitly testing "test_var". However, I don't understand why the two bool tests consistently get skipped. My understanding is that the Jinja "when" syntax should pretty much follow Python logic, yes? e.g.:
>>> bool("")
False
>>> bool("a string")
True
Is there something I'm missing with regard to testing the truthiness/falsiness of a string? This is Ansible 1.8.2.
Many thanks,
-Aaron