# Conditional Variable Prompt using when

**URL:** <https://forum.ansible.com/t/conditional-variable-prompt-using-when/19405>\
**Category:** Ansible Project\
**Created:** [October 1, 2015, 3:27pm UTC](https://forum.ansible.com/t/conditional-variable-prompt-using-when/19405 "2015-10-01T15:27:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Kerr](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@Chris\_Kerr](https://forum.ansible.com/u/Chris_Kerr)\
**Post date:** [October 1, 2015, 3:27pm UTC](https://forum.ansible.com/t/conditional-variable-prompt-using-when/19405/1 "2015-10-01T15:27:55Z")

</div>

According to [http://docs.ansible.com/ansible/playbooks\_variables.html](http://docs.ansible.com/ansible/playbooks_variables.html)

" the `when` conditional can also be used with variables"

However here is my issue with it on ansible-1.9.2-1.

I’m setting a vars\_prompt for version number.  
When running through it, i’m specifying Version 11.

`

- name: “version\_no”  
prompt: “What version of oracle are we setting? 11 or 12. 11 is default”  
default: “11”  
private: no  
`

Then an associated variable called lib\_dir\_version based on the version entered.

`  
vars:

lib\_dir\_version: “11.2”  
when: “{{version\_no}} == 11”

lib\_dir\_version: “12.1”  
when: “{{version\_no}} == 12”  
`

However the above always only takes 12.1 as the return when i Echo Out.

`  
tasks:

- name: “Echo dir version”  
shell: echo “My lib dir version is {{ lib\_dir\_version }}.”  
`

FROM messages on host.

`args=echo "My lib dir version is 12.1." removes=None NO_LOG=None shell=True warn=True`

Why cant i figure this out?

Whats wrong with my syntax?

Chris

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 1, 2015, 11:23pm UTC](https://forum.ansible.com/t/conditional-variable-prompt-using-when/19405/2 "2015-10-01T23:23:07Z")

</div>

you are misunderstanding the docs, the use of variables IN the  
conditional 'when' not using when to make var assignment conditional,  
for that you can use filters:

vars:  
&nbsp;&nbsp;&nbsp;lib\_dir\_version: "{{ version\_no == 12|ternary(12, 11) }}"

---

<div class="post-metadata">

**Author:** ![Chris\_Kerr](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@Chris\_Kerr](https://forum.ansible.com/u/Chris_Kerr)\
**Post date:** [October 2, 2015, 7:46am UTC](https://forum.ansible.com/t/conditional-variable-prompt-using-when/19405/3 "2015-10-02T07:46:36Z")

</div>

Thanks for the response Brian

So far I have changed the syntax to the following: (Had to wrap the version\_no == 12 in ( ) to supress `False` passing through.

`lib_dir_version: "{{ (version_no == 12)| ternary('12','11') }}"`

If I enter 11 in my prompt - I get the expected echo of

`echo "My lib dir version is 11."`

However when I rerun the playbook, and specify 12 as the version number in my prompt, I still get Version 11 in the output.

`echo "My lib dir version is 11."`

Kind Regards  
Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Kerr](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@Chris\_Kerr](https://forum.ansible.com/u/Chris_Kerr)\
**Post date:** [October 2, 2015, 8:09am UTC](https://forum.ansible.com/t/conditional-variable-prompt-using-when/19405/4 "2015-10-02T08:09:34Z")

</div>

This seems to have sorted it!

`lib_dir_version: "{{ (version_no == '12')|ternary('12','11') }}"`

single quoted the value ‘12’

Brian Thanks for the heads up - Pointed me properly in the right direction

Thank you!  
Chris
