if-elseif-if

56 views
Skip to first unread message

Ali Khawaja

unread,
May 5, 2018, 3:25:58 AM5/5/18
to ansible-project@googlegroups.com <ansible-project@googlegroups.com>
i am trying to set a variable based on the value of a variable in the config file but unable to do so. 

here's my config file:

sql_ver: "2012" # available sql versions are: 2008 | 2012 | 2014 | 2016
sp_ver: "2010" # available sp versions are: 2007 | 2010 | 2013 | 2016

sp2k10: en_sharepoint_server_2010_with_service_pack_2_x64_dvd_4178583.iso
sp2k13: en_sharepoint_server_2013_with_sp1_x64_dvd_3823428.iso
sp2k16: en_sharepoint_server_2016_x64_dvd_8419458-003.iso

sql2k12: en_sql_server_2012_standard_edition_with_service_pack_3_x64_dvd_7286878.iso
sql2k14: en_sql_server_2014_standard_edition_with_service_pack_2_x64_dvd_8961564
sql2k16: en_sql_server_2016_standard_with_service_pack_1_x64_dvd_9540929

and my ansible file is:

- name: Software Version Selection
hosts: localhost
vars_files:
- testcfg.yml
gather_facts: false
tasks:
- debug:
msg: "{{sp_ver}}"

- set_fact:
sp : "{{sp2k10}}"
when: "{{sp_ver}} == 2010"

sp : "{{sp2k13}}"
when: "{{sp_ver}} == 2013"

sp : "{{sp2k16}}"
when: "{{sp_ver}} is match(2016)"

- debug:
msg: "{{sp}}"

i have tried many things:  "{{sp_ver}} == 2010" , "{{sp_ver}}" == 2010, "{{sp_ver}} is match(2016)"  but nothing worked.  i dont know why there is a syntax error for this:  "{{sp_ver}}" is match(2016)

basically what is want is for the selected sp_ver, the proper variable gets selected.

Thanks in advance
Ali

Sosys

unread,
May 6, 2018, 10:26:54 PM5/6/18
to ansible...@googlegroups.com
my understanding is that, on the "when" statement, you dont need to specify " {{ or }} "

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFnH2h-uMs8T1TADKJ1Sc%3DGy7%2BMoYQCoog%3DvUokZKJs-jHm28A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
May 7, 2018, 11:53:42 AM5/7/18
to Ansible Project
try

when: sp_ver|int == 2010

or

when: sp_ver == "2010"

----------
Brian Coca

Edmund Cheng

unread,
May 7, 2018, 10:36:33 PM5/7/18
to Ansible Project
As the guys have mentioned, try removing the {{}}, variables in the when condition is implicit i think..

Ali Khawaja

unread,
May 8, 2018, 9:57:16 AM5/8/18
to ansible...@googlegroups.com

I tried all the suggestions but it did not work. 

 

With the following in my config file:

sp_ver: 2016

and with following code:

sp = ‘abc’

When: sp_ver | int == 2016

 

sp = ‘def’

When: sp_ver  == “2016”

 

Both variations did not work.  Jordan mentioned on the irc channel a nice solution which was to create dictionary item and then use sp_ver as a lookup which worked very nicely.  But I am really interested for learning purposes why the above do not work.

 

Thanks again. 

 

 

 

 

Sent from Mail for Windows 10

--

You received this message because you are subscribed to the Google Groups "Ansible Project" group.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/73b68aea-8be8-4edb-b7be-e9f807695c71%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages