1

How does one enforces that a "yes" or "no" answer will be converted into a boolean value when defining a vars_promp?

For instance. With this playbook:

---
- name: variable tests
  hosts: local
  gather_facts: false

  vars:
    answer1: yes

  vars_prompt:

    - name: "answer2"
      prompt: "Reply true"
      private: no

    - name: "answer3"
      prompt: "Reply yes"
      private: no

  tasks:

    - debug: msg="answer1 is true"
      when: answer1

    - debug: msg="answer2 is true"
      when: answer2

    - debug: msg="answer3 is true"
      when: answer3

The last task will fail since answer3 is a string with value "yes". It isn't converted into a boolean value as when specifying "true" (answer2).

Converting to a true or false boolean is already the behaviour when using vars (as in answer1).

Bernard
  • 16,149
  • 12
  • 63
  • 66
  • 2
    Some background: `answer1: yes` in YAML defines `answer1` as a Boolean; `vars_prompt` returns string values (for `answer2` and `answer3`). Then: a string `true` is considered a Boolean literal by Jinja2 and evaluates to true, string `yes` is considered a regular string. Lastly, when you use only a variable name in `when` condition, an Ansible double-templating kicks in and for `answer2` causes the literal to be passed to Jinja2, for `answer3` causes an error. – techraf Jan 07 '18 at 09:18
  • 3
    You can use `debug: var=answer1|type_debug` to verify the variable type. – techraf Jan 07 '18 at 09:29

0 Answers0