Changing variable value based on another variable value

17 views
Skip to first unread message

Yehuda Pinhas

unread,
Jan 7, 2020, 8:41:12 AM1/7/20
to Ansible Project
Hi guys!

This is my code:
---
  qos_policyqos-nested-new-10m
   when"{{ bandwidth_mb }} == '10'"
  qos_policyqos-nested-new-20m
   when"{{ bandwidth_mb }} == '20'"

#### Modify: ####
  bandwidth_mb: 10

I'm trying to create a condition for the qos_policy variable:
if bandwidth_mb = 10 then qos_policy = qos-nested-new-10m
if bandwidth_mb = 20 then qos_policy = qos-nested-new-20m

Just for clarification:
• bandwidth_mb & qos_policy are variables
• qos-nested-new-10m & qos-nested-new-20m are strings


The errors im receiving on all my previous attempts to do this code are:
1. invalid syntax
2. duplicate value for qos_policy applying only the last value (always qos-nested-new-20m)

I receive these errors seperatly, depending on how I change the code but its still not working like it should.



What am I doing wrong here?

Thanks in advance,
YP

Vladimir Botka

unread,
Jan 7, 2020, 9:34:47 AM1/7/20
to Yehuda Pinhas, ansible...@googlegroups.com
On Tue, 7 Jan 2020 05:41:12 -0800 (PST)
Yehuda Pinhas <yuda....@gmail.com> wrote:

> qos_policy: qos-nested-new-10m
> when: "{{ bandwidth_mb }} == '10'"
> qos_policy: qos-nested-new-20m
> when: "{{ bandwidth_mb }} == '20'"
>
> #### Modify: ####
> bandwidth_mb: 10
>
> *I'm trying to create a condition for the qos_policy variable:*
> if bandwidth_mb = 10 then qos_policy = qos-nested-new-10m
> if bandwidth_mb = 20 then qos_policy = qos-nested-new-20m

Concatenate the strings. For example

- hosts: localhost
vars:
bandwidth_mb: 10
tasks:
- set_fact:
qos_policy: "{{ 'qos-nested-new-' +
bandwidth_mb|string +
'm' }}"
- debug:
var: qos_policy

gives

ok: [localhost] => {
"qos_policy": "qos-nested-new-10m"
}

> *The errors im receiving on all my previous attempts to do this code are:*
> 1. invalid syntax
> when: "{{ bandwidth_mb }} == '10'"

The conditions are expanded by default. Try

- debug:
msg: Bandwidth is 10 MB
when: bandwidth_mb == 10

HTH,

-vlado

Yehuda Pinhas

unread,
Jan 8, 2020, 2:48:10 AM1/8/20
to Ansible Project
Thank you vladimir!
working now

בתאריך יום שלישי, 7 בינואר 2020 בשעה 16:34:47 UTC+2, מאת Vladimir Botka:
Reply all
Reply to author
Forward
0 new messages