# Problem with Conditional variable

**URL:** <https://forum.ansible.com/t/problem-with-conditional-variable/32039>\
**Category:** Ansible Project\
**Created:** [January 23, 2020, 2:22pm UTC](https://forum.ansible.com/t/problem-with-conditional-variable/32039 "2020-01-23T14:22:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [January 23, 2020, 2:22pm UTC](https://forum.ansible.com/t/problem-with-conditional-variable/32039/1 "2020-01-23T14:22:37Z")

</div>

**Hi!**  
**I want to make the variable qos\_policy set on conditional basis:**

**If bandwidth\_mb == ‘50’**  
**then qos\_policy == ‘qos-nested-50’**

**else**  
**qos\_policy == ‘qos-nested-new-50’**

**My code is this (not working):**

qos\_policy: “{{ ‘qos-nested-new-’ + bandwidth\_mb }}”  
qos\_policy: “{{ ‘qos-nested-’ + bandwidth\_mb }}”  
when:

- bandwidth\_mb == “50”

bandwidth\_mb: “some number”

**Error:**  
[ansible@Netauto-Dev new\_branch]$ ansible-playbook test\_new\_branch\_playbook.yml PLAY [TEST5\_TLV5] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* TASK [include\_vars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ok: [R-TEST5-TLV5] TASK [include\_role : new\_branch] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ERROR! Syntax Error while loading YAML. did not find expected key The error appears to be in ‘/etc/ansible/roles/new\_branch/tasks/configure\_som\_tlv5.yml’: line 8, column 7, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - qos\_policy: “{{ ‘qos-nested-’ + bandwidth\_mb }}” when: ^ here PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* R-TEST5-TLV5 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

**What is the problem?**

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 23, 2020, 2:33pm UTC](https://forum.ansible.com/t/problem-with-conditional-variable/32039/2 "2020-01-23T14:33:48Z")

</div>

> \*Hi!\*  
> \*I want to make the variable qos\_policy set on conditional basis:\*  
> \*  
> \*  
> \*If bandwidth\_mb == '50'\*  
> \* then qos\_policy == 'qos-nested-50'\*  
> \*  
> \*  
> \*else\*  
> \* qos\_policy == 'qos-nested-new-50'\*  
> \*  
> \*  
> \*  
> \*  
> \*My code is this (not working):\*  
> qos\_policy: "{{ 'qos-nested-new-' + bandwidth\_mb }}"  
> qos\_policy: "{{ 'qos-nested-' + bandwidth\_mb }}"  
> when:  
> - bandwidth\_mb == "50"
> 
> bandwidth\_mb: "some number"

Hello Yehuda,

variables don't have a when condition, but you can try:

qos\_policy: "{% if bandwidth\_mb == "50" %}{{ 'qos-nested-' + bandwidth\_mb }}{% else %}{{ 'qos-nested-new-' +  
bandwidth\_mb }}{% endif %}"

(untested, but that's the idea)

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 23, 2020, 3:21pm UTC](https://forum.ansible.com/t/problem-with-conditional-variable/32039/3 "2020-01-23T15:21:24Z")

</div>

The problem is the dash '-' in the name of the variable 'qos-nested-'.  
Quoting from "Creating valid variable names"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#creating-valid-variable-names](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#creating-valid-variable-names)

&nbsp;&nbsp;"Variable names should be letters, numbers, and underscores. Variables  
&nbsp;&nbsp;should always start with a letter."

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 23, 2020, 3:54pm UTC](https://forum.ansible.com/t/problem-with-conditional-variable/32039/4 "2020-01-23T15:54:02Z")

</div>

> \> There are two options for that string:  
> \> 1. qos-nested-50 will be set when bandwidth\_mb is 50.  
> \> 2. qos-nested-new-20. 20 for example will set the string to qos-nested-new-  
> \> when bandwidth\_mb is not 50.

It's possible to use the 'ternary' filter. For example

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;qos\_policy: "{{ 'qos-nested-' +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(bandwidth\_mb == '50')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ternary('50', 'new-20') }}"

HTH,

&nbsp;&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [January 26, 2020, 7:20am UTC](https://forum.ansible.com/t/problem-with-conditional-variable/32039/5 "2020-01-26T07:20:05Z")

</div>

Hi!

Both solutions given are not working for some reason…

Any other suggestions?

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [January 27, 2020, 9:22am UTC](https://forum.ansible.com/t/problem-with-conditional-variable/32039/6 "2020-01-27T09:22:20Z")

</div>

Hi guys!  
Solved.

Solution:  
qos\_policy: “{{ ‘qos-nested-50m’ if bandwidth\_mb == 50 else ‘qos-nested-new-’ ~ bandwidth\_mb ~ ‘m’ }}”

Thanks for all replys.  
Peace out.
