if-elseif-if

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

my understanding is that, on the “when” statement, you dont need to specify " {{ or }} "

try

when: sp_ver|int == 2010

or

when: sp_ver == "2010"

As the guys have mentioned, try removing the {{}}, variables in the when condition is implicit i think…

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.