Problem with Conditional variable

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?

*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
          Racke

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

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

HTH,

  -vlado

> 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

     - set_fact:
         qos_policy: "{{ 'qos-nested-' +
                          (bandwidth_mb == '50')|
                          ternary('50', 'new-20') }}"

HTH,

   -vlado

Hi!

Both solutions given are not working for some reason…

Any other suggestions?

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.