Conditional Variable Prompt using when

759 views
Skip to first unread message

Chris Kerr

unread,
Oct 1, 2015, 11:27:55 AM10/1/15
to Ansible Project
According to 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






Brian Coca

unread,
Oct 1, 2015, 7:23:18 PM10/1/15
to Ansible Project
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:
lib_dir_version: "{{ version_no == 12|ternary(12, 11) }}"



--
Brian Coca

Chris Kerr

unread,
Oct 2, 2015, 3:46:36 AM10/2/15
to Ansible Project
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

Chris Kerr

unread,
Oct 2, 2015, 4:09:35 AM10/2/15
to Ansible Project
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

Reply all
Reply to author
Forward
0 new messages