vars_prompt defaults not evaluating correctly with bool filter

59 views
Skip to first unread message

Aaron Mills

unread,
Feb 10, 2015, 1:48:28 PM2/10/15
to ansible...@googlegroups.com
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

    # Doesn't run as expected per http://docs.ansible.com/playbooks_variables.html
    - 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

Reply all
Reply to author
Forward
0 new messages