# if-elseif-if

**URL:** <https://forum.ansible.com/t/if-elseif-if/27462>\
**Category:** Ansible Project\
**Created:** [May 5, 2018, 7:25am UTC](https://forum.ansible.com/t/if-elseif-if/27462 "2018-05-05T07:25:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali.khawaja](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@ali.khawaja](https://forum.ansible.com/u/ali.khawaja)\
**Post date:** [May 5, 2018, 7:25am UTC](https://forum.ansible.com/t/if-elseif-if/27462/1 "2018-05-05T07:25:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Benny\_Kusman](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@Benny\_Kusman](https://forum.ansible.com/u/Benny_Kusman)\
**Post date:** [May 7, 2018, 2:26am UTC](https://forum.ansible.com/t/if-elseif-if/27462/2 "2018-05-07T02:26:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 7, 2018, 3:53pm UTC](https://forum.ansible.com/t/if-elseif-if/27462/3 "2018-05-07T15:53:28Z")

</div>

try

when: sp\_ver|int == 2010

or

when: sp\_ver == "2010"

---

<div class="post-metadata">

**Author:** ![Edmund\_Cheng](https://avatars.discourse-cdn.com/v4/letter/e/fbc32d/32.png) [@Edmund\_Cheng](https://forum.ansible.com/u/Edmund_Cheng)\
**Post date:** [May 8, 2018, 2:36am UTC](https://forum.ansible.com/t/if-elseif-if/27462/4 "2018-05-08T02:36:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ali.khawaja](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@ali.khawaja](https://forum.ansible.com/u/ali.khawaja)\
**Post date:** [May 8, 2018, 1:57pm UTC](https://forum.ansible.com/t/if-elseif-if/27462/5 "2018-05-08T13:57:02Z")

</div>

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.
