Reputation: 23
Want to compare values with list for int and display msg values greater than 15
tasks:
- name: Create a List variable and print it
set_fact:
Continents: ["10","20"]
- name: set fatc
set_fact:
int_list: "{{ Continents|map('int')|list }}"
- debug:
msg: "{{greater than 15}}"
when: "{{int_list}}" > 15
Getting error as below:
The offending line appears to be:
msg: "{{ list }}"
when: "{{int_list}}" > 15
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
Expected Output:
greater than 15
Upvotes: 0
Views: 1206
Reputation: 384
You can achieve the same with two tasks. Change your list to integers. Make the debug task to loop through the Continents variable.
- hosts: localhost
tasks:
- name: Create a variable with a list of integers
set_fact:
Continents: [10, 20]
- debug:
msg: "greater than 15"
loop: "{{ Continents }}"
when: item > 15 ##item becomes each value of the Continents variable. So, 10, 20 and so on.
Gives:
skipping: [localhost] => (item=10)
ok: [localhost] => (item=20) => {
"msg": "greater than 15"
}
Upvotes: 0
Reputation: 311978
If your goal is to show only those integers in the list that are greater than 15, you can use the select
filter:
- hosts: localhost
gather_facts: false
tasks:
- name: Create a list of integers
set_fact:
int_list: [10, 20]
- name: Find integers greater than 15
debug:
msg: "{{ item }}"
loop: "{{ int_list | select('>', 15) }}"
The output of this playbook is:
PLAY [localhost] ***************************************************************
TASK [Create a list of integers] ***********************************************
ok: [localhost]
TASK [Find integers greater than 15] *******************************************
ok: [localhost] => (item=20) => {
"msg": 20
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Upvotes: 3